简体   繁体   中英

Long running ProgressDialog shows Application not responding (ANR) dialog

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??

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

public interface Listener {

    public abstract void onEvent();

}

in the broadcast receiver create a static reference and setter

private static MessageListener mListener = null;

@Override
public void onReceive(Context context, Intent intent) {
    //stuff
    if(mListener != null)
         mListener.onEvent();   
}

public static void setListener(Listener l) {
    mListener = l;
}

then implement the listener in the Activity

class MyActivity implements Listener{

@Override
public void onResume(){
    //register the listener so that when the activity is visible it can receive updates
    BroadCastReceiver.setListener(this);
}

@Override
public void onPause(){
    //unregister the listener so the activity doesn't receive updates while in the background
    BroadCastReceiver.setListener(null);
}
@Override
public void onEvent(){
    //update the UI
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM