0

I've got a service that I run every 30 minutes via AlarmManager.RTC. If the device is asleep long enough for the service to be called twice (and thus have both of them be rescsheduled), when it wakes up, it fires off the service twice at once.

The desired behavior is for the AlarmManager to just fire off one Intent when it wakes up. How can I accomplish this?

galaxy infinite
  • 143
  • 3
  • 9

2 Answers2

2

Try this:

PendingIntent.FLAG_UPDATE_CURRENT
eeerahul
  • 1,629
  • 4
  • 27
  • 38
Baka
  • 33
  • 5
0

Have you cancel the old one of Alarm Manager. If no means, just cancel the old one with PendingIntent

Have a look at this Commonsware Example

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • How do I 'get' the old `PendingIntent`, and how do I cancel it? I want to make sure I preserve the existing scheduling of the intents, ie I dont want to cancel future intents. – galaxy infinite Jan 31 '12 at 15:38
  • @galaxyinfinite If two `PendingIntent`s were created in the exact same way, it's treated as the same one for `cancel()` and so on. See the docs: http://developer.android.com/reference/android/app/PendingIntent.html – Izkata Jan 31 '12 at 16:50
  • @izkata Where would I do the cancelling? From within the service that it has started? Like I said, then I'd need to reschedule my `PendingIntent`, which seems like a hack. Additionally, are the alarms scheduled in parallel? Then I wouldn't have a guarantee that it would do anything at all. – galaxy infinite Jan 31 '12 at 18:37
  • @galaxyinfinite See http://stackoverflow.com/questions/5707656/what-happens-when-i-start-an-alarm-twice - if the `PendingIntent`s are defined the same, they won't be scheduled twice. – Izkata Jan 31 '12 at 19:02
  • @izkata, I read that as well, but I'm not getting that behavior. Even with `FLAG_CANCEL_CURRENT` my intent gets scheduled again as described. – galaxy infinite Feb 01 '12 at 13:12