2011-06-01 53 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()方法。在这个方法中更新你的进度对话框。