长时间运行的 ProgressDialog 显示应用程序无响应 (ANR) 对话框
您好,我目前正在开发一个应用程序,在首次启动该应用程序时需要非常大的下载(100-200MB)。
我的第一个活动启动一个服务,该服务有一个异步任务来完成所有下载。
为了显示我的下载进度,我正在执行以下操作:
首先使用 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);
}
在我的活动上,我有一个更新进度条的 BroadcastReceiver
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);
}
}
}
所以我不确定我在这里做错了什么,因为进度条显示几秒钟后我收到了 ANR。
难道是因为我发送了太多广播来更新进度条?
Hi I'm currently developing an app that requires a very large download (100-200MB) on the first launch of the application.
My first activity starts a service, which has an asynctask to do all the downloading.
To show the progress of my download I'm doing the following:
First using AsyncTask's publishProgress/onProgressUpdate I send a broadcast to my activity with the current progress:
@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);
}
The on my activty I have a BroadcastReceiver that updates the progressbar
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);
}
}
}
So I'm not sure of what I'm doing wrong here, because after a couple of seconds that the progress bar is showing I get an ANR.
Could it be because I'm sending too much broadcasts to update the progress bar??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我唯一能想到的是 ANR 是由于进度对话框长时间禁用交互造成的,请尝试删除对话框并使用不同的方法来通知用户进度。
像这样的事情
在广播接收器中创建一个静态引用和setter
private static MessageListener mListener = null;
然后在Activity中实现监听器
The only thing I can think of is the ANR is due to the progress dialog disabling interaction for so long, try removing the dialog and using a different method to notify the user of progress.
Something like this
in the broadcast receiver create a static reference and setter
private static MessageListener mListener = null;
then implement the listener in the Activity