0
我想我只是在製作「意大利麪代碼」。 Perhabs的一些專家可以幫助我爲我的解決方案編寫更好的代碼。以下僞代碼將顯示我的問題:Asynctask的代碼結構Android
我有一些運行時間很長的程序。此過程將顯示一個進度條。我使用Asynctasks來實現這一點。這一切工作正常,但我的代碼是不是維護了:
我希望把它簡單的代碼,而無需嵌套代碼:
public void onCreate(Bundle savedInstanceState) {
LongrunningTask1();
dosomething1();
LongrunningTask2();
dosomething2();
LongrunningTask3();
dosomething3();
LongrunningTask4();
dosomething4();
}
我的代碼以Asynctasks:
public void onCreate(Bundle savedInstanceState) {
new LongrunningTask1().execute("Task1");
}
class LongrunningTask1 extends AsyncTask<String, Void, boolean> {
private Exception exception;
protected RSSFeed doInBackground(String... urls) {
try {
Do;
return true;
} catch (Exception e) {
return null;
}
}
protected void onProgressUpdate(Void... values) {
updateprogressbar();
}
protected void onPostExecute(RSSFeed feed) {
dosomething1();
new LongrunningTask2().execute("Task2");
}
}
class LongrunningTask2 extends AsyncTask<String, Void, boolean> {
similar to LongrunningTask1
protected void onPostExecute(RSSFeed feed) {
dosomething2();
new LongrunningTask3().execute("Task3");
}
}
class LongrunningTask3 extends AsyncTask<String, Void, boolean> {
similar to LongrunningTask2
protected void onPostExecute(RSSFeed feed) {
dosomething3();
new LongrunningTask4().execute("Task4");
}
}
class LongrunningTask4 extends AsyncTask<String, Void, boolean> {
similar to LongrunningTask3
protected void onPostExecute(RSSFeed feed) {
dosomething4();
}
}
我想以避免任何嵌套調用。誰能幫我 ?
Regards
嗨。謝謝你的幫助。當然,在一個線程中執行所有東西會更容易,但是我不需要順序地執行線程。 ThreadPoolExecutor很適合其他情況。但在我的情況下,我真的很喜歡有一個簡單的解決方案,如果有的話。 – 2013-05-09 07:07:27