How to start a new Activity after several async tasks had finished

时间:2016-10-15 16:59:54

标签: java android android-asynctask

I have a big Problem:

I want to start a new Activity after several Async-Tasks had finished. My problem is that the Async-Tasks are dynamically started. Some time I have only one Async-Task, some time I have five Async-Tasks. And the even bigger problem is, that all are the same Async-Tasks, only with another URL. Because they're Download-Tasks.

Could anyone help?

Solution:

I created a counter that every available Update counts 1 higher. So if five Updates are available, the counter will be set to 5. Each finished Update and Unzip, the counter will be set 1 lower. If counter == 0 it would open the new Activity.

Thanks for all of your Answers.

3 个答案:

答案 0 :(得分:1)

Use a global variable counter assigned with number of AsynTasks,
On every onPostExecute decrement the counter

ON if(counter ==0) Start your new Activity

class A{
int counter =0;

public doJob(int jobCOunt){
    this.counter = jobCount
    new Job().execute();
}
class Job extends AsyncTask{
...
  protected void onPostExecute(Boolean success) {
    counter--;
    if(counter == 0){
        startActivity
    }
  }

}

}  

答案 1 :(得分:0)

Just curious to know can't we move loop of different URLs in doInBackground() for getting results? I think this can resolve your big problem and will also easy to manage single AsyncTask.

答案 2 :(得分:0)

我想在AsyncTask上运行每个Executor,然后等待结果应满足您的需求。熟悉执行者this topic可能会有所帮助。顺便说一句,如果是正确的实现,您已经应该在其上运行该任务以避免耗费时间的任务池。可以通过AsyncTask.executeOnExecutor(Executor exec, Params... params)方法在执行程序上执行异步任务 之后创建管理线程(强烈建议使用IntentService执行此操作并将所有任务管理逻辑移到那里),这将等待任务完成并发送事件以启动活动。如何等待执行程序中的所有任务完成?好吧,read this question。提供了比我能提供的更详细的答案。
我很确定我已经描述了你最好的方法。