如何让一个asynctask在另一个之后开始?

时间:2014-05-20 11:29:53

标签: android android-asynctask

我有两个asynctask每个调用一个函数来解析一些数据...我想asynctask启动后asynctasknew完成我怎么能这样做???这是我的代码..

    send.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 AsyncCallWS task = new AsyncCallWS();
                try{
                    Intent newintent = getIntent();
                    mixlist=newintent.getStringArrayListExtra("listmix");
                    Log.e("listmix",mixlist+"");
                    for(int i=0;i<=mixlist.size();i++){
                        if(i==mixlist.size()){
                            Log.d("states","finished");
                             Item_Name="0";

                                Item_Price="0";

                                Item_Quantity="0";

                                Total_Price="0";

                                Customer_Name=name.getText().toString();
                                Log.e("customer_name",Customer_Name);
                                Customer_Number=mobile.getText().toString();
                                Customer_Address=addressnew.getText().toString();

                                //Call execute 

                                task.execute(); 
                        }

                        else{
                    Item_Name=mixlist.get(i);
                    i++;
                    Item_Price=mixlist.get(i);
                    i++;
                    Item_Quantity=mixlist.get(i);
                    i++;
                    Total_Price=mixlist.get(i);

                    Customer_Name="0";
                    Customer_Number="0";
                    Customer_Address="0";

    //              AsyncCallWSnew tasknew = new AsyncCallWSnew();
                    //Call execute 
                    AsyncCallWSnew tasknew = new AsyncCallWSnew();
                    tasknew.execute();
                        }
                    }
                }
                catch(Exception e){
                    e.printStackTrace();
                }

            }
        });


         } 

         private class AsyncCallWS extends AsyncTask<Void, Void, Void> {



                protected void onPostExecute(Void result) {
                    //Make Progress Bar invisible

                    Toast.makeText(getApplicationContext(), "order has been sent + item price", Toast.LENGTH_LONG).show();
                    Intent intObj = new Intent(PersonalInfo.this,MainActivity.class);
                    startActivity(intObj);
                    //Error status is false
                }


                //Make Progress Bar visible
                protected void onPreExecute() {

                }


                @Override
                protected Void doInBackground(Void... params) {
                    // TODO Auto-generated method stub
                     loginStatus = WebService.invokeLoginWS(Item_Name,Item_Price,Item_Quantity, Total_Price, Customer_Name,  

                   Customer_Number, Customer_Address,"InsertData");
                    return null;
                }


            }   

         private class AsyncCallWSnew extends AsyncTask<Void, Void, Void> {



                protected void onPostExecute(Void result) {
                    //Make Progress Bar invisible

                    Toast.makeText(getApplicationContext(), "order has been sent", Toast.LENGTH_LONG).show();
                    Intent intObj = new Intent(PersonalInfo.this,MainActivity.class);
                    startActivity(intObj);
                    //Error status is false
                }


                //Make Progress Bar visible
                protected void onPreExecute() {

                }


                @Override
                protected Void doInBackground(Void... params) {
                    // TODO Auto-generated method stub
                     loginStatus = WebService.invokeLoginWS(Item_Name,Item_Price,Item_Quantity, Total_Price, Customer_Name,  

                Customer_Number, Customer_Address,"InsertData");
                    return null;
                }


            }   
    }

当我进行调试时,我的代码运行得很好..但是在正常运行中......它不能帮助我吗?

2 个答案:

答案 0 :(得分:3)

基本上有两种可能性:

答案 1 :(得分:0)

您好您可以使用AsyncTask executeonExecutor方法启动异步任务。但它需要最低API版本11.请参考以下代码。

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
     new YourFirstTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,params...);
     new YourSecondTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,params...);
  }else{
    new YourFirstTask().execute(params...);
    new YourSecondTask().execute(params...);
  }

对于较低版本,您可以直接致电。自动系统将逐个处理。