AsyncTask的状态始终显示正在运行 [英] Asynctask status always showing running
问题描述
我要完成的第一个任务后,执行AsyncTask的。但在打印状态时,第一个任务是始终显示RUNNING.If执行这两个任务的并行只有较小的任务将被执行。我同时运行在活动的onCreate method.Any想法?
这是我的code样品
公共类测试扩展活动
{
ExecuteTask1任务1;
ExecuteTask2 TASK2;
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
任务1 =新ExecuteTask1();
task1.execute(标记);
的System.out.println(task1.getStatus());
如果(task1.getStatus()== AsyncTask.Status.FINISHED)
{
TASK2 =新ExecuteTask2();
task2.execute(标记);
}
}
}
在你的code,现在,你是不是给任务1的时间来完成。开始从任务1的 onPostExecute
方法TASK2。 (你必须修改code类ExecuteTask1为此工作。)
另外,有任务1回拨到你的活动(或信息发送给它或东西)的 onPostExecute
让你的活动可以开始TASK2。
I want to execute an asynctask after finishing the first task. But when printing the status the of the first task it always shows RUNNING.If execute both tasks parallel only smaller task will be executed. I am running both in activity oncreate method.Any idea?
here is my code sample
public class test extends Activity
{
ExecuteTask1 task1;
ExecuteTask2 task2;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
task1 = new ExecuteTask1();
task1.execute(token);
System.out.println(task1.getStatus());
if(task1.getStatus() ==AsyncTask.Status.FINISHED)
{
task2 = new ExecuteTask2();
task2.execute(token);
}
}
}
In your code right now, you aren't giving task1 time to finish. Start task2 from the onPostExecute
method of task1. (You'll have to modify the code in class ExecuteTask1 for this to work.)
Alternatively, have task1 call back to your activity (or post a message to it or something) in onPostExecute
so your activity can then start task2.
这篇关于AsyncTask的状态始终显示正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!