Search code examples
androidbroadcastreceiverdownload-manager

Send a variable from a "Activity" to "BroadcastReceiver."


I need to send a variable from an Activity to a BroadcastReceiver registered on Manifest.

My Scenario:

I have a file list that can be downloaded by user and when he clicks the app calls DownloadManager, and put a flag “PENDENT” in the database. This list is managed by an Activity

In my AndroidManifest I have registred a BroadcastReceiver with the filter android.intent.action.DOWNLOAD_COMPLETE and when my BroadcastReceiver was called i want change this register from "PENDENT" to “DOWNLOADED”.

Observation: Maybe my approach is not good and if this is the case, I accept sugestions.

Thanks in advance!


Solution

  • You should take a look at the downloadedId returned by the downloadManager at enqueue time. You can do something like:

    long downloadId = manager.enqueue(request);
    

    Then you could store the downloadId on your database and you would be able to get which file was downloaded by querying your data for the downloadedId that came on the IntentExtras of onReceive. You can grab the extra info using:

    Bundle extras = intent.getExtras();
    Long downloaded_id = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);
    

    Hope this helps.