0
嗨我目前正在開發一個應用程序,需要在第一次啓動應用程序時進行非常大的下載(100-200MB)。長時間運行ProgressDialog顯示應用程序沒有響應(ANR)對話框
我的第一個活動啓動一個服務,它有一個asynctask來做所有的下載。
顯示我下載的進度我做了以下內容:使用的AsyncTask的publishProgress
首先/ onProgressUpdate我發送一個廣播,以我的活動與當前進度:
@Override
protected Void doInBackground(Void... params) {
...
publishProgress(Integer.toString(completedFiles), current_filename, Integer.toString(file_progress));
...
}
@Override
protected void onProgressUpdate(String... progress) {
super.onProgressUpdate(progress);
progressBroadcast.putExtra(NOTIFY_DOWNLOAD_PROGRESS, progress);
sendOrderedBroadcast(progressBroadcast, null);
}
的我activty我有一個BroadcastReceiver,由於後一對夫婦O更新的進度
private class ProgressReceiver extends BroadcastReceiver{
private int totalFiles;
public ProgressReceiver(Context context) {
progressDialog = new ProgressDialog(context);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(ResourceDownloadService.NOTIFY_DOWNLOAD_START.equals(action)){
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.setTitle(getString(R.string.loading_res_dialog_title));
totalFiles = intent.getIntExtra(ResourceDownloadService.TOTAL_FILES, 0);
progressDialog.setMessage("Downloading Resources");
progressDialog.show();
}else if(ResourceDownloadService.NOTIFY_DOWNLOAD_END.equals(action)){
//Hide progress bar one we are done
progressDialog.dismiss();
startIntroActivity();
}else{
String[] progress_info = intent.getExtras().getStringArray(ResourceDownloadService.NOTIFY_DOWNLOAD_PROGRESS);
int completedFiles = Integer.parseInt(progress_info[0]);
String filename = progress_info[1];
int progress = Integer.parseInt(progress_info[2]);
progressDialog.setProgress(progress);
progressDialog.setMessage("Downloading "+filename +"\n"+completedFiles +" files downloaded of "+totalFiles);
}
}
}
所以我不知道我做錯了什麼在這裏, f秒進度條顯示我得到一個ANR。
難道是因爲我發送了太多的廣播來更新進度條?
..... 100-200MB,這是瘋狂的,但是,除了你爲什麼要使用廣播接收器來做到這一點,的AsyncTask的publishProgess方法是專門設計用於從後臺線程更新UI – triggs 2012-02-05 00:48:15
我知道,但我已經看到市場上的很多遊戲都做同樣的事情,除此之外它只會在應用程序的首次發佈時發佈。我將廣播進度作爲廣播發送,因爲我在服務中使用AyncTask,然後將廣播發送到活動,所以如果活動不存在(配置更改),服務將無論如何都會下載資源 – 2012-02-05 00:53:00