2012-02-05 157 views
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。

难道是因为我发送了太多的广播来更新进度条?

+0

..... 100-200MB,这是疯狂的,但是,除了你为什么要使用广播接收器来做到这一点,的AsyncTask的publishProgess方法是专门设计用于从后台线程更新UI – triggs 2012-02-05 00:48:15

+0

我知道,但我已经看到市场上的很多游戏都做同样的事情,除此之外它只会在应用程序的首次发布时发布。我将广播进度作为广播发送,因为我在服务中使用AyncTask,然后将广播发送到活动,所以如果活动不存在(配置更改),服务将无论如何都会下载资源 – 2012-02-05 00:53:00

回答

1

我能想到的是ANR的唯一的事情是由于进度对话框禁用互动了这么久,尝试删除对话框,并使用不同的方法来通知进步的用户。

像这样

public interface Listener { 

    public abstract void onEvent(); 

} 

在广播接收机创建静态参考和setter

私有静态消息监听mListener = NULL;

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

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

然后实现监听器在活动

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 
} 
} 
相关问题