onPostExecute在异步任务中的doInBackground之前执行 [英] onPostExecute executes before doInBackground in Asyncronous task

查看:398
本文介绍了onPostExecute在异步任务中的doInBackground之前执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索了很长一段时间后,我浏览了以下链接并了解了

http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)

AsyncTask永远不会执行onPostExecute

但是我想对服务器做一些API命中,并且在获取所有数据之后,我想从Splashscreen转到我的mainactivity,下面是我的代码. . .

class Startsyntask extends AsyncTask<Void, Void, Void> 
    {

         @Override
         protected Void doInBackground(Void... arg0) 
         { 
            raw_data = new File(Environment.getExternalStorageDirectory() + "/.MyFoldernew");
            if(raw_data.exists())
            {
                Log.e("directory exists", " already ");
                new read_syntask().execute();
            }
            else
            {
                Log.e("directory created", " newly ");
                raw_data.mkdirs();
                new write_syntask().execute();
            }
            return null;    
         }

         @Override
         protected void onPostExecute(Void unused) 
         {
             if( i == 6)
             {
                finish();
                startActivity(new Intent("com.sample.app.Tabbar"));
             }
         }
    }

以及代码read_synctaskwrite_synctask中的另一个异步任务与某些特定操作有关,在这些异步任务中,它在doinBackground之后调用onPostexecute.

实际上,它移到了Tabbar活动中,API命中继续,并且我在Tabbar活动中从服务器读取了数据.仅在完成doinBackground中的异步任务后,才应该调用onProgress.

解决方案

实际上,

new read_syntask().execute();

new write_syntask().execute();

都具有异步调用,它们不会等待完成任务并执行下一个执行步骤,因此您的执行将在onPostExecute()完成之前完成.

new write_syntask().execute();

.

您要做的是,

使用new read_syntask().execute().get();new write_syntask().execute().get();

现在AsyncTask中的get()方法将等待AsyncTask完成,然后将执行onPostExecute(). (但我认为它将阻塞UI线程.)

更新 :(最佳方法)

实际上,不需要Startsyntask AsyncTask,您可以直接在Activity方法中编写raw_data if-else条件,也可以编写用于启动Tabbar

finish();
startActivity(new Intent("com.sample.app.Tabbar"));

应位于read_syntaskwrite_syntaskonPostExecute()中.

After searching for a long , i went through the following links and understood ,

http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)

AsyncTask never executes onPostExecute

But i want to do some API hits to the server and after getting all the data , i want to move to my mainactivity from the Splashscreen , below is my code . . . .

class Startsyntask extends AsyncTask<Void, Void, Void> 
    {

         @Override
         protected Void doInBackground(Void... arg0) 
         { 
            raw_data = new File(Environment.getExternalStorageDirectory() + "/.MyFoldernew");
            if(raw_data.exists())
            {
                Log.e("directory exists", " already ");
                new read_syntask().execute();
            }
            else
            {
                Log.e("directory created", " newly ");
                raw_data.mkdirs();
                new write_syntask().execute();
            }
            return null;    
         }

         @Override
         protected void onPostExecute(Void unused) 
         {
             if( i == 6)
             {
                finish();
                startActivity(new Intent("com.sample.app.Tabbar"));
             }
         }
    }

and in the code read_synctask and write_synctask are another asyncronous tasks that has some specific operations to do with , in these Asyncronous tasks it is calling the onPostexecute after doinBackground .

Actually it moves to the Tabbar Activity and the API hits continues and i get data read from server in Tabbar activity . how could i implement that only after completing the asyncronous task in doinBackground , onProgress should be called .

解决方案

Actually,

new read_syntask().execute();

and

new write_syntask().execute();

both have Asynchronous call which don't wait for complete the task and perform the next execution step, so your execution comes to onPostExecute() before finishes both AsyncTask.

What you have to do is,

use new read_syntask().execute().get(); and new write_syntask().execute().get();

Now get() method from AsyncTask will wait for complete the AsyncTask and then onPostExecute() will execute. (But I think it will block the UI thread).

Update: (Best approach)

Actually, there is no need of Startsyntask AsyncTask, You can directly write the if-else conditions of raw_data in Activity's method and also your code for starting Tabbar

finish();
startActivity(new Intent("com.sample.app.Tabbar"));

should be in either read_syntask or write_syntask 's onPostExecute().

这篇关于onPostExecute在异步任务中的doInBackground之前执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆