让AsyncTask的等待其他AsyncTasks [英] Make AsyncTask wait for other AsyncTasks

查看:120
本文介绍了让AsyncTask的等待其他AsyncTasks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AsyncTask的执行多个AsyncTasks(调用Web服务)。

I have an AsyncTask executing multiple AsyncTasks (calling a web service).

我正在寻找一种方法,使我的AsyncTask等待别人的AsyncTask回应

I'm looking for a way to make my AsyncTask waiting for others AsyncTask responses

要能够给主的AsyncTask响应

to be able to give "main" AsyncTask response

public class MainTask extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... urls) {
          for (int i=0 ; i<myObjects.size();i++) {
                MyObject m=myObjects.get(i);
                creationTask=new CreationTask();
                //init creationTask
                creationTask.execute(new String[] { URL });
          }
            //wait for creationTasks responses
            //return "success" if all succeeded
     }
}

任务由MainTask的推出

Task launched by MainTask

   public class CreationTask extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... urls) {
               //Creation request to web Service
              return result;
        }
        @Override
        protected void onPostExecute(String result) {
            if ("success".equals(result)) {
                //notify MainTask
            }
        }
     }

在此先感谢

推荐答案

您可以调用其他的 doInBackground 方法 AsyncTasks doInBackground 主之一。

You can call the doInBackground method of other AsyncTasks from the doInBackground of your main one.

public class MainAsyncTask<?, ?, ?>{
    doInBackground(){
        new OtherAsyncTask().doInBackground();
        new AnotherAsyncTask().doInBackground();
    }
}

或者,更好的,你拆code了一下,让分管 AsyncTasks 都没有。

这篇关于让AsyncTask的等待其他AsyncTasks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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