onPostExecute在异步任务中的doInBackground之前执行 [英] onPostExecute executes before doInBackground in Asyncronous task
问题描述
搜索了很长一段时间后,我浏览了以下链接并了解了
http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)
但是我想对服务器做一些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_synctask
和write_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_syntask
或write_syntask
的onPostExecute()
中.
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屋!