2

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!

Elltz
  • 10,730
  • 4
  • 31
  • 59
user3358519
  • 125
  • 1
  • 8

3 Answers3

1

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.

Thpramos
  • 441
  • 4
  • 15
0

A BroadcastReceiver receives an Intent. The variable can be contained in the Intent.

Here is a guide to creating a background service and passing the Intents. https://developer.android.com/training/run-background-service/create-service.html

Elltz
  • 10,730
  • 4
  • 31
  • 59
mattfred
  • 2,689
  • 1
  • 22
  • 38
0

It's probably not the best idea for your list of files to be stored and managed by the Activity itself. It may be safer for the list of data to be saved in some class that is separate from both your Activity and BroadcastReceiver -- perhaps some kind of IntentService that saves data to the filesystem. Then both your Activity and BroadcastReceiver can use the IntentService to manage the data.

Rob Szumlakowski
  • 947
  • 3
  • 8
  • 18