2012-02-05 10 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ため、私はカップルの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); 
     } 
    } 

} 

は、だから私は、私がここで間違ってやっているのかわからないプログレスバーを更新BroadcastReceiverを持っています進行状況バーが表示されている秒数です。私はANRを取得します。

プログレスバーを更新するためにブロードキャストを送信しすぎている可能性がありますか?

+0

..... 100〜200メガバイトのリスナーを実装し、それは非常識だが、それはさておき、なぜあなたがこれを行うには放送受信機を使用している、AsyncTaskのpublishProgess方法でしたバックグラウンドスレッドからUIを更新するために特別に設計されました – triggs

+0

私は知っていますが、市場で多くのゲームが同じことを見てきましたが、それだけでアプリの最初の起動時になります。私はブロードキャストとして進捗状況を送信しています。サービス内でAyncTaskを使用していて、アクティビティにブロードキャストを送信しているため、アクティビティが存在しない場合(設定変更)、サービスはリソースをとにかくダウンロードします –

答えて

1

私が考えることができるのは、ANRが進捗ダイアログの操作を長時間無効にしているためです。ダイアログを削除し、別の方法を使用して進捗状況をユーザーに通知してください。放送受信機におけるこの

public interface Listener { 

    public abstract void onEvent(); 

} 

何かが静的基準とセッターを作成

プライベート静的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; 
} 

が、その後の活動に

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 
} 
} 
関連する問題