逐个运行asynctasks

时间:2018-06-19 10:51:51

标签: android android-asynctask

我有3个asynctask来运行。

new getNewArrivalProducts().execute();
new getPromo().execute();
new getUserinfo().execute();

效果很好,但现在我想逐个运行它。所以我试试这个

final AsyncTask First  = new getNewArrivalProducts().execute();
final AsyncTask Second = new getPromo();
final AsyncTask Third = new getUserinfo();

     if(First.getStatus() == AsyncTask.Status.RUNNING){
                            inAnimation = new AlphaAnimation(0f, 1f);
                            inAnimation.setDuration(200);
                            progressBarHolder.setAnimation(inAnimation);
                            progressBarHolder.setVisibility(View.VISIBLE);
                        }

                        if(First.getStatus() == AsyncTask.Status.FINISHED && Second.getStatus() != AsyncTask.Status.RUNNING){
                            Second.execute();
                            if(Second.getStatus() == AsyncTask.Status.FINISHED  && Third.getStatus() != AsyncTask.Status.RUNNING){
                                Third.execute();
                                if(Third.getStatus() == AsyncTask.Status.FINISHED ){
                                    outAnimation = new AlphaAnimation(1f, 0f);
                                    progressBarHolder.setAnimation(outAnimation);
                                    progressBarHolder.setVisibility(View.INVISIBLE);
                                }
                            }
                        }

但是,我的脚本在上面。只有first正在运行而其他正在运行。我怎样才能让我的asynctasks由一个人运行?提前谢谢

1 个答案:

答案 0 :(得分:1)

尝试一下

  

相应地在后期执行方法中一次调用异步任务

首先调用

new getNewArrivalProducts().execute();

然后在后期执行调用中新建getPromo().execute();

public class getNewArrivalProducts extends AsyncTask<Void, Void, String> {

  // rest of the code here

    @Override
    protected void onPostExecute(String result) {
      new getPromo().execute();
    }
 }

public class getPromo extends AsyncTask<Void, Void, String> {

   // rest of the code here

    @Override
    protected void onPostExecute(String result) {
      new getUserinfo().execute();
    }
 }

public class getUserinfo extends AsyncTask<Void, Void, String> {

 // rest of the code here

    @Override
    protected void onPostExecute(String result) {

    }
 }