I have scheduled a PendingIntent
with AlarmManager
. When the AlarmManager
delivers the Intent
, I am able to getExtras()
all the data that I originally sent.
However, sometimes I get additional data that I want to pass with the Intent
, before the AlarmManager
fires. My thought was, I would just get the Intent
like I was going to cancel it, but then after cancelling it, update the extras and reschedule it with the AlarmManager
, like this:
Intent i=new Intent(this, MyReceiver.class);
Bundle b = i.getExtras();
PendingIntent pi=PendingIntent.getBroadcast(this, id,i, 0);
if (b == null) b = i.getExtras(); // in case I can't get it before calling getBroadcast
// now add a key to b, put it in a new intent, schedule it, and cancel the old one?
I'm calling getBroadcast()
with the same id as I schedule it with earlier. The only problem is, when I call getExtras()
it always returns null. This seems to be kind of the reverse of the problem I have seen frequently where an Intent
will be cached and that is not desired. In this case I actually want to get the cached Intent
and get the value for it, and since the Intent
is pending it should still be there.
How do I do this?
A couple of ideas I have had and/or tried. One revolves around the Intent.fillIn()
function, could I perhaps use this to indicate that it should (or should not) overwrite the extras so that I can retrieve the original ones.
Looking a bit further, I see that PendingIntent
has writeToParcel()
. Can I use this to get access to the extras of the attached Intent
? My attempt failed with readBundle: bad magic number
exception on readParcel:
PendingIntent pi=PendingIntent.getBroadcast(this, id,i, 0);
Parcel out = Parcel.obtain();
pi.writeToParcel(out, 0);
i.readFromParcel(out);
I suspect that maybe this is because I am outputting a PendingIntent
but trying to read it back as an Intent
.
The other idea is that I could call send() on the PendingIntent
to cause it to be immediately delivered, passing a new Intent
with a new key to add. Then in the handler I would have to iterate through all keys in the extras of the intent. It would then need to re-issue the PendingIntent
to the AlarmManager
if it wasn't time yet to do the actual processing.