Android:AsyncTask,onPostExecute在doInBackground完成之前启动

Android:AsyncTask,onPostExecute在doInBackground完成之前启动,android,camera,android-asynctask,Android,Camera,Android Asynctask,几个小时前我遇到了这个问题()我把它当作解决了,但后来我意识到我也遇到了类似的问题 这是我的主要课程“游戏活动” 在myAsyncRunnable.fire()中,我有一个循环,它在gameActivity中更改了超过10倍的ImageView图像。我希望在更改最后一张图像时启动photoTask 这里,myAsyncRunnable中的代码扩展了AsyncTask @Override protected Void doInBackground(String... params) { fire(

几个小时前我遇到了这个问题()我把它当作解决了,但后来我意识到我也遇到了类似的问题

这是我的主要课程“游戏活动”

在myAsyncRunnable.fire()中,我有一个循环,它在gameActivity中更改了超过10倍的ImageView图像。我希望在更改最后一张图像时启动photoTask 这里,myAsyncRunnable中的代码扩展了AsyncTask

@Override
protected Void doInBackground(String... params) {
fire();
return null;
}

public void fire() {
        final ImageView image3 = (ImageView) gameactivity.findViewById(R.id.imageView3);
        final int drawables[] = new int[] {R.drawable.fire1,R.drawable.fire2,R.drawable.fire3,R.drawable.fire4,R.drawable.fire5,R.drawable.fire6,R.drawable.fire7,R.drawable.fire8,R.drawable.fire9,R.drawable.fire10,R.drawable.fire11,R.drawable.fire12,R.drawable.fire13,R.drawable.fire14,R.drawable.fire15,R.drawable.fire16};
        for (int i=0;i<drawables.length;i++) {
            final int j=i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    image3.setImageResource(drawables[j]);
                     Log.w("GAMEACTIVITY","image"+j);
                }
            };
            gameactivity.handler.postDelayed(runnable, 200*j);
        }
    }

@Override
protected void  onPostExecute(Void result)  {
    Log.w("GAMEACTIVITY","ONPOSTEXECUTE");
    gameactivity.doPhotoTask();
}
我的原木猫

02-11 16:03:32.920: W/GAMEACTIVITY(7750): actionup
02-11 16:03:33.010: W/GAMEACTIVITY(7750): image0
02-11 16:03:33.010: W/GAMEACTIVITY(7750): ONPOSTEXECUTE
02-11 16:03:33.030: E/QualcommCameraHardware(99): takePicture(479)
02-11 16:03:33.100: E/QualcommCameraHardware(99): rawsize = 460800 cbcr offset =307200
02-11 16:03:33.110: E/QualcommCameraHardware(99): takePicture: X
02-11 16:03:33.110: W/GAMEACTIVITY(7750): TAKEPICTURE
02-11 16:03:33.120: E/mm-camera(99): camera_ops_start, CAMERA_OPS_CAPTURE_AND_ENCODE mode 1
02-11 16:03:33.350: W/GAMEACTIVITY(7750): image1
02-11 16:03:33.490: W/GAMEACTIVITY(7750): image2
02-11 16:03:33.780: W/GAMEACTIVITY(7750): image3
02-11 16:03:33.880: W/GAMEACTIVITY(7750): image4
02-11 16:03:34.110: W/GAMEACTIVITY(7750): image5
02-11 16:03:34.250: W/GAMEACTIVITY(7750): image6
02-11 16:03:34.490: W/GAMEACTIVITY(7750): image7
02-11 16:03:34.680: W/GAMEACTIVITY(7750): image8
02-11 16:03:34.880: W/GAMEACTIVITY(7750): image9
02-11 16:03:35.050: W/GAMEACTIVITY(7750): image10
02-11 16:03:35.110: E/QualcommCameraHardware(99): receiveRawPicture: E
02-11 16:03:35.280: W/GAMEACTIVITY(7750): image11
02-11 16:03:35.440: W/GAMEACTIVITY(7750): image12
02-11 16:03:35.500: E/QualcommCameraHardware(99): address of Jpeg 0 encoded buf 1085800448 Jpeg Heap base 1085800448
02-11 16:03:35.500: W/GAMEACTIVITY(7750): FIRST
02-11 16:03:36.470: W/GAMEACTIVITY(7750): INTENT
02-11 16:03:36.580: W/GAMEACTIVITY(7750): image13
02-11 16:03:36.710: W/GAMEACTIVITY(7750): image14
02-11 16:03:36.800: W/GAMEACTIVITY(7750): image15
02-11 16:03:37.320: W/GAMEACTIVITY(7750): SURFACEDESTROYED

试试这小零钱,它可能对你有帮助

Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    image3.setImageResource(drawables[j]);
                     Log.w("GAMEACTIVITY","image"+j);
            gameactivity.handler.postDelayed(this, 200*j);
                }
            };
          gameactivity.handler.postDelayed(runnable, 10);

因此,问题是您正在为将来的执行对将来的可运行程序进行计时,然后从doInBackground返回—这会立即激活onPostExecute函数


您的问题的解决方案不是使用OnPostExecute,而是使用您希望在所有其他线程之后运行的代码来计时另一个Runnable,因为它们都在同一个线程上运行,这应该可以解决您的问题。

我有一个问题。当我写“gameactivity.handler.postDelayed(runnable,200*j);”时,runnable是在执行行后200*j毫秒内以doBackground方法运行,还是在外部运行?看看你的照片。它将在gameactivity线程上运行(不在后台运行,如果您正在更新ui,这很好)
02-11 16:03:32.920: W/GAMEACTIVITY(7750): actionup
02-11 16:03:33.010: W/GAMEACTIVITY(7750): image0
02-11 16:03:33.010: W/GAMEACTIVITY(7750): ONPOSTEXECUTE
02-11 16:03:33.030: E/QualcommCameraHardware(99): takePicture(479)
02-11 16:03:33.100: E/QualcommCameraHardware(99): rawsize = 460800 cbcr offset =307200
02-11 16:03:33.110: E/QualcommCameraHardware(99): takePicture: X
02-11 16:03:33.110: W/GAMEACTIVITY(7750): TAKEPICTURE
02-11 16:03:33.120: E/mm-camera(99): camera_ops_start, CAMERA_OPS_CAPTURE_AND_ENCODE mode 1
02-11 16:03:33.350: W/GAMEACTIVITY(7750): image1
02-11 16:03:33.490: W/GAMEACTIVITY(7750): image2
02-11 16:03:33.780: W/GAMEACTIVITY(7750): image3
02-11 16:03:33.880: W/GAMEACTIVITY(7750): image4
02-11 16:03:34.110: W/GAMEACTIVITY(7750): image5
02-11 16:03:34.250: W/GAMEACTIVITY(7750): image6
02-11 16:03:34.490: W/GAMEACTIVITY(7750): image7
02-11 16:03:34.680: W/GAMEACTIVITY(7750): image8
02-11 16:03:34.880: W/GAMEACTIVITY(7750): image9
02-11 16:03:35.050: W/GAMEACTIVITY(7750): image10
02-11 16:03:35.110: E/QualcommCameraHardware(99): receiveRawPicture: E
02-11 16:03:35.280: W/GAMEACTIVITY(7750): image11
02-11 16:03:35.440: W/GAMEACTIVITY(7750): image12
02-11 16:03:35.500: E/QualcommCameraHardware(99): address of Jpeg 0 encoded buf 1085800448 Jpeg Heap base 1085800448
02-11 16:03:35.500: W/GAMEACTIVITY(7750): FIRST
02-11 16:03:36.470: W/GAMEACTIVITY(7750): INTENT
02-11 16:03:36.580: W/GAMEACTIVITY(7750): image13
02-11 16:03:36.710: W/GAMEACTIVITY(7750): image14
02-11 16:03:36.800: W/GAMEACTIVITY(7750): image15
02-11 16:03:37.320: W/GAMEACTIVITY(7750): SURFACEDESTROYED
Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    image3.setImageResource(drawables[j]);
                     Log.w("GAMEACTIVITY","image"+j);
            gameactivity.handler.postDelayed(this, 200*j);
                }
            };
          gameactivity.handler.postDelayed(runnable, 10);