Search code examples
androidandroid-download-manager

Is it a good idea to unregister download receiver?


I am using download manager to download files, Just wanted to know what is the significance of registering/unregistering the receiver on onPause/onResume? The fill still keeps downloading in background regardless.

   protected void onResume() {
    super.onResume();
    // Register the receiver to receive an intent when download complete
    IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    registerReceiver(downloadReceiver, intentFilter);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    // Unregister the receiver
    unregisterReceiver(downloadReceiver);
}

Solution

  • For this kind of broadcast receiver, you may not want to. Unregistering in onPause is for broadcasts you don't care about if you're not on screen. For example, if you were a game that was registering for screen off notifications to pause timers- then you don't need to receive those notifications if you're not in the foreground. Downloading files- if you need to know when that file downloads (say to process it) then you wouldn't want to unregister. Of course in that case you may not want your activity to own the BroadcastReceiver, but some other part of your app that will stay resident longer.