0

doInBackground() は問題なく動作します. Looper.loop() の後のコードは動作しません。Looper.Loop() の後にログが印刷されず、onPostExceute() が実行されません。メソッド 1、2、3 が実行されるまで待つ必要があります。Looper.prepare() を使用しない場合、method1 で例外が発生します。

@Override
    protected Void doInBackground(Void... params) {
        try {

                if (Looper.myLooper() == null)
                    Looper.prepare();               
                method1();
                method2();
                method3();
                Looper.loop();

                Log.d(TAG,"after loop()");
            } else {
                method4(); //inside asyn task
            }

            Log.d(TAG,"doInBackground end");

        }catch (Exception e) {
            Log.d(TAG,"doInBackground exception "+e);
        }


        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        try {


            Log.d(TAG, "onPostExecute");



            //............

        }catch (Exception e){
            Log.d(TAG,"onPostExecute end");
        }
    }


    @Override
    protected void onPreExecute() {
        //.....
    }
4

1 に答える 1