你只需要1的AsyncTask,你必須做的doInBackground()
所有5個電話,每次你完成一個呼叫publishProgress
通過例如完成的呼叫的號碼,然後,在年底做任何你在onPostExecute
需要。
一個簡單的方法:
private class ServiceCallTask extends AsyncTask<String, Integer, Void> {
protected void onPreExecute() {
//prepare whatever you need
myTextField.setText("starting calls");
}
protected Void doInBackground(String... params) {
//process params as you need and make the calls
doCall1();
publishProgress(1); //this calls onProgressUpdate(1)
doCall2();
publishProgress(2);
doCall3();
publishProgress(3);
doCall4();
publishProgress(4);
doCall5();
publishProgress(5);
return;
}
protected void onProgressUpdate(Integer... progress) {
//this runs in UI thread so its safe to modify the UI
myTextField.append("finished call " + progress);
}
protected void onPostExecute(Void unused) {
myTextField.append("all calls finished");
}
}
如果Web服務採用相同類型的參數爲5個調用,一個的AsyncTask可以用來創建5異步任務對象,並執行它們,並更新異步任務的TextView的執行後。 – 2011-01-19 09:16:23