2013-12-18 186 views
4

我想了解在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的截圖

Log

+1

http://stackoverflow.com/a/6450407/1124160看看這個。 – damian

回答

5

方法簽名是錯誤的。它應該看起來像:

protected void onPostExecute(Void result) { 
    Log.i("Thread","onPosts"); 
} 

此外,我建議您使用@Override註釋。這樣,如果您試圖覆蓋錯誤的方法,您將收到編譯時錯誤。

編輯: 您發佈onProgressUpdate的簽名是錯誤的。它應該是:

protected void onProgressUpdate(Integer... progress){} 
+0

非常感謝您的答案和建議:)'onPostExecute'現在工作,但'保護無效onProgressUpdate(虛空進展)'沒有得到執行.. – Andoxko

+0

我不能在你的代碼中看到你調用publishProgress(); – Blackbelt

+0

是的,我把它寫成@ A.S。在他的回答中說,我看不到進展...... – Andoxko

1

AsyncTask擁有onProgressUpdate(Void ...)方法,您可以調用每個迭代或每次在doInBackground()中通過調用publishProgress()完成進度。

參考docs更多細節

所以應該看上去像個這樣的:

public class DownloadFilesTask extends AsyncTask<Void, Integer, String> { 

    protected 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"; 
    } 

    protected void onProgressUpdate(int progress) { 
     Log.i("Thread","onProgress " + progress); 
    } 

    private void onPostExecute(String result) { 
     Log.i("Thread","onPosts " + result); 
    } 
} 
+0

非常感謝你的答案和例子你的代碼工作正常,但保護無效onProgressUpdate(INT進展)'沒有得到執行..至少我沒有看到日誌.. – Andoxko

+0

你會得到所有線程日誌? –

+0

這是我的logCat的快照http://postimg.org/image/jql10fsgz/我做錯了什麼? 'onProgressUpdate'打印在另一個線程上?我怎樣才能看到它?再次感謝你 – Andoxko