代码之家  ›  专栏  ›  技术社区  ›  YVS1102

逐个运行异步任务

  •  1
  • YVS1102  · 技术社区  · 8 年前

    我有3个异步任务要运行。

    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 在跑,其他人不在跑。如何使异步任务由一个运行?提前谢谢

    1 回复  |  直到 8 年前
        1
  •  1
  •   Quick learner    8 年前

    试试这个

    相应地在post execute方法中逐个调用asyntask

    先说这个

    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) {
    
        }
     }
    
    推荐文章