我想了解在Android上使用AsyncTask
併爲此目的,我開發了這個簡單的代碼。AsyncTask not calling onProgressUpdate,onPostExecute
令人驚訝的是,doInbackground被正確調用,我看到日誌但其他兩個方法沒有被調用。
我錯過了什麼嗎?
感謝advancde
public class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
try {
Log.i("Thread","1");
Thread.sleep(1000);
Log.i("Thread","2");
Thread.sleep(1000);
Log.i("Thread","3");
Thread.sleep(1000);
Log.i("Thread","4");
Thread.sleep(1000);
Log.i("Thread","5");
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Void... progress) {
Log.i("Thread","onProgress");
}
protected void onPostExecute(Void... result) {
Log.i("Thread","onPosts");
}
}
[編輯]
有了這個代碼的所有作品的權利以外的onProgressUpdate
public class DownloadFilesTask extends AsyncTask<Void, Integer, String> {
public String doInBackground(Void... params) {
try {
int i = 0;
Log.i("Thread","1");
Thread.sleep(1000);
publishProgress(i++);
Log.i("Thread","2");
Thread.sleep(1000);
publishProgress(i++);
Log.i("Thread","3");
Thread.sleep(1000);
Log.i("Thread","4");
Thread.sleep(1000);
Log.i("Thread","5");
} catch (InterruptedException e) {
e.printStackTrace();
}
return "done";
}
public void onProgressUpdate(int progress) {
Log.i("Thread","onProgress " + progress);
}
public void onPostExecute(String result) {
Log.i("Thread","onPosts " + result);
}
在這裏,你可以看到我的logcat的截圖
http://stackoverflow.com/a/6450407/1124160看看這個。 – damian