Okay I found out what was happening.  It was actually two things:

1) Was not editing the data used inside a PendingIntent.
Changing the flag in the PendingIntent.getService(...) method to
FLAG_UPDATE_CURRENT solved this.  However this led to the second
problem.

2) A new PendingIntent is not always created with each call to
PendingIntent.getService(...).
If the arguments to that call match then the method will return an
existing PendingIntent that matches to be used.  I think Android is
trying to be smart about reusing resources.  The only argument the
method doesn't check when deciding to reuse an existing PendingIntent,
or provide a new one, is the Intent itself.

This posed a problem for me, because my activity needed to send
multiple PendingIntents, each using an Intent with the same Class but
different extended data for the Intent.  All of the PendingIntents
needed to be active at the same time.  For example, one PendingIntent
needed to go off 8 minutes from now, while another PendingIntent
needed to go off 10 min from now.  If the first PendingIntent was
already sent to the AlarmManager, once I tried to make a new
PendingIntent for the 10 min delay, it would grab the Existing
PendingIntent, and change it instead, essentially now only providing
one Active PendingIntent.

Unfortunately as of right now there are no flags you can pass to a
PendingIntent that will force the method to provide a brand-new
Pending Intent, i.e. don't try to match the arguments to an existing
one and return that one.  However I found a "hack" solution right
now.  The int requestCode parameter used in the
PendingIntent.getService(..) is not used right now according to the
documentation, however, it is still checked by the method to decide if
there is a matching already existing PendingIntent out there.  So my
hack solution is to just provide a new dummy requestCode every time I
call the PendingIntent.getService(...), which creates a mismatch in
parameters, it doesn't find an existing PendingIntent with that
requestCode, and gives me a new one.  My app is now doing exactly what
I want.  This is a hack though, because if the requestCode is ever
used in the future, this might mess things up.

I propose that in the future the PendingIntent.getService(...) method
should have a flag that forces a new pending intent to be created,
e.g. FLAG_CREATE_NEW.  The FLAG_ONE_SHOT doesn't quite do this, since
if the first PendingIntent hasn't actually been used yet, and you try
to create another one to be used later, it will match and grab the
first and modify it, effectively getting rid of the first logical
PendingIntent that would have been used.

Jason

On Sep 26, 6:59 pm, "Jason B." <jason.ba...@gmail.com> wrote:
> Thanks Lee.  I'll give that a shot and post the results.
>
> On Sep 26, 5:14 am, Lee <lee.wil...@googlemail.com> wrote:
>
> > Try using setData() to store your message instead of extras: that
> > forces the Intent to be seen as a new one. Or you can keep using
> > extras and
> > create some other unique token every time for setData().
>
> > Lee
>
> > p.s. also see the flags you can use at the end of the getService call
> > (read
> > the PendingIntent docs). There's something like
> > 'FLAG_REPLACE_CURRENT'.
>
> > On Sep 25, 9:39 pm, "Jason B." <jason.ba...@gmail.com> wrote:
>
> > > Hi,
>
> > > I'm creating an intent that I'm pushing to the AlarmManager so that a
> > > service will be run at a later time.  I have a single string value I
> > > need to pass with the intent so the service knows what to "do" when
> > > it's onStart() is called.
>
> > > Here is the code that sets up my intent and pushes it to the
> > > AlarmManager
>
> > > Intent myintent = new Intent();
> > > myintent.setClass(mContext, RemindMeLater_Service.class);
> > > myintent.putExtra("ReminderText", mEditText_Reminder.getText().toString
> > > ());
> > > mPendingIntent = PendingIntent.getService(this, 0, myintent, 0);
>
> > > // Use the AlarmManager to schedule the PendingIntent
> > > mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime,
> > > mPendingIntent);
>
> > > Notice the line:
>
> > > myintent.putExtra("ReminderText", mEditText_Reminder.getText().toString
> > > ());
>
> > > Essentially I'm trying to "change" the extended data referenced by the
> > > same name in the putExtra call.  Note also that the "myintent" Intent
> > > is freshly created everytime this block of code executes, so it should
> > > be a new instantiated Intent object.  The text that is put into the
> > > extended data comes from an EditText view on my activity.
>
> > > The code in the service that is trying to pull the string out is:
>
> > >  public void onStart(Intent intent, int startId) {
>
> > >         // Increment mReminderId to ensure each notification is unique to
> > > this context
> > >         String reminderText = intent.getStringExtra("ReminderText");
>
> > >         //do stuff with the "reminderText" we just got out of the intent
> > >        // ...........
> > >     }
>
> > > Seems pretty simple, and logical.  So here comes the problem.  This
> > > works ONCE.  The first time the intent is created and the extended
> > > data is added with .putExtra(  ), when the AlarmManager goes off and
> > > starts the service, the remidnerText is exactly what I put in.
> > > However, subsequent calls to this code, creating the intent, adding a
> > > new string using .putExtra( ), the AlarmManager goes off, service gets
> > > onStart() called, now the reminderText still has the same value from
> > > the first call.  It keeps holding onto the same value for ever until I
> > > uninstall the .apk and reinstall (i.e. Rebuild ->Run through eclipse
> > > on the emulator or a real device).
>
> > > I've tried putting breakpoints right at this line and verified the new
> > > string being passed into putExtra() has changed:
>
> > > myintent.putExtra("ReminderText", mEditText_Reminder.getText().toString
> > > ());
>
> > > And then a breakpoint at the service line and verified that it still
> > > has the previous value:
>
> > > String reminderText = intent.getStringExtra("ReminderText");
>
> > > I'm a little lost at what to do next.  Maybe the bundle that is
> > > created is when I call putExtra() is being cached?  I've tried doing
> > > System.gc() right before I create my new intent each time and still no
> > > luck.  Thanks for any insight, I hope I've provided enough detail.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to