tags:

views:

558

answers:

3

Hi

I have an application, which sets an alarm using AlarmManager, which starts another Activity when it goes off. The AlarmManager takes a PendingIntent and spawns a BroadcastReceiver class when the specified time comes. I'm wondering whether there is any way that I can pass arguments to this BroadcastReceiver through the Intent object which goes into PendingIntent?

Basically what I'd like to do is something like this:

Intent my_intent = new Intent(this, BroadcastService.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, my_intent, 0);

my_intent.putExtra("arg1", arg1);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000), pendingIntent);

and I'd like to be able to retrieve arg1 in the BroadcastReceiver's onReceive(Context, Intent) method. I figured that the local variable my_intent would be the second parameter passed on to onReceive by the PendingIntent, but apparently that's not quite right. Is it possible to pass parameters between an Activity and a BroadcastReceiver in this fashion (using Intent.putExtra()) or should I use a ContentProvider instead?

Thanks!

Iva

A: 

Yes, I think it is possible to pass any data of basic Java type and Serializable/Parceable types in the extras of an Intent wrapped around a PendingIntent and then retrieve them using the Intent instance passed to the onReceive of the Broadcastreceiver. Your approach looks okay to me.

What is the problem/error that you are getting? Is "arg1" instance Serializable?

Samuh
I'm not getting an error; I'm simply not getting a bundle with the intent that is passed to my BroadcastReceiver's onReceive(Context, Intent) method; I'm assuming that the intent passed to my BroadcastReceiver is the same intent I create (and populate) in my Activity. In this case, any argument I want to pass is indeed Serializable.
Can you show us your code that has/does putExtra(..) and getXXXExtra(..)?
Samuh
It turns out I just needed to put the putExtra(...) calls before creating the PendingIntent. Thanks for your help!
Are you going to accept any answer or at least going to vote up on any?
Samuh
A: 

I have an application, which sets an alarm using AlarmManager, which starts another Activity when it goes off.

That is bad form. Do not pop up activities unannounced like this without a very good reason (e.g., an incoming phone call). What if the user is in the middle of doing something, like TXTing or playing a game or trying to tap numbers for a phone menu?

Is it possible to pass parameters between an Activity and a BroadcastReceiver in this fashion (using Intent.putExtra())

Yes. However, bear in mind that you will want to use PendingIntent.FLAG_UPDATE_CURRENT when you create your PendingIntent, to ensure that any new extras you supply on the Intent actually get used.

CommonsWare
Ah yes, I misspoke; I'm using the Alarm as an avenue to invoke a particular activity at a particular time, and when the alarm goes off, I create a notification which the user can then drop down whenever convenient and only then invoke the activity, so that I don't disturb whatever they happen to be doing. Thanks for your answer!
Hmm, adding the PendingIntent.FLAG_UPDATE_CURRENT as a flag when getting the PendingIntent doesn't seem to change anything; I'm still not getting the extras from the intent in my original Activity to the intent passed to the onReceive
Are you calling putExtra() before creating the PendingIntent? I don't know when PendingIntent serializes the Intent, but if it does it right away, putExtra() calls after creating the PendingIntent would have no effect.
CommonsWare
I'm calling putExtra() after creating the PendingIntent. I'm populating all of the extras inside of an OnClickListener, and thus they're may not be ready when the PendingIntent is first created. Perhaps I would be better off using a ContentProvider
You're right! Populating the intent before wrapping it in a PendingIntent worked. Thanks for your help! :)
A: 

I had a similar problem, but I was already populating the Intent first before wrapping it in a PendingIntent. But the answer to my problem was, as pointed out above, that I needed to use the PendingIntent.FLAG_UPDATE_CURRENT flag. Once I set the flag, it worked! I hope this helps others. -Jeff

Jeff