异步任务状态总是显示正在运行 [英] Asynctask status always showing running

查看:57
本文介绍了异步任务状态总是显示正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在完成第一个任务后执行一个异步任务.但是在打印第一个任务的状态时,它总是显示 RUNNING.如果并行执行两个任务,只会执行较小的任务.我在活动 oncreate 方法中同时运行两者.知道吗?

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?

这是我的代码示例

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);
            }

     }
}

推荐答案

在您的代码中,您现在没有给 task1 时间来完成.从 task1 的 onPostExecute 方法启动 task2.(您必须修改 ExecuteTask1 类中的代码才能使其工作.)

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.)

或者,在 onPostExecute 中让 task1 回调您的活动(或向其发布消息或其他内容),以便您的活动可以启动 task2.

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.

这篇关于异步任务状态总是显示正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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