2011-06-01 75 views
0

我創建了一個AsyncTask來執行我的後臺操作。用戶界面顯示ProgressDialog。我將Context傳遞給AsyncTask的構造函數。後臺操作和ProgressDialog

當我想操作ProgressDialog文本AsyncTask它錯誤,因爲ProgressDialog框由另一個線程創建。

關於如何使AsyncTaskProgressDialog箱子通信的提示或想法,或創建ProgressDialog箱子的更好方法?

回答

1

您不應該從AsyncTaskdoInBackground函數與UI進行通信。改用onProgressUpdate函數。

private class BackgrounTask extends AsyncTask<Integer, Integer, Integer> { 

    protected Integer doInBackground(Integer... count){ 
     int max = count[0]; 
     for (int i = 0; i < count; i++) { 
     publishProgress(i, max); 
     } 
     return max; 
    } 

    protected void onProgressUpdate(Integer... progress) { 
     int cur = progress[0]; 
     int max = progress[1]; 

     //update your progress dialog here 
    } 

    protected void onPostExecute(Integer result) { 
     //process result 
    } 
} 
+0

得到它的工作,謝謝你的問題編輯和awnser! – 2011-06-02 00:16:00

0

使用AsyncTask.publishProgress()方法並覆蓋AsyncTask.onProgressUpdate()方法。在這個方法中更新你的進度對話框。