14

I'm Jumping trough hoops (well, it's not that complicated ofcourse) to avoid starting an alarm twice. The basic code goes like this:

AlarmManager mgr=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(this, MyService.class);
PendingIntent pi=PendingIntent.getService(this, 0, i, 0);
mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);

Would it matter if I would actually run this code everytime my app starts? I'm not seeing any ill effects when calling this about 10 times as an overkill-experiment, but I can't find any reference as to if this is coincidence or expected behavior.

If it is not especially expected, it feels 'wrong'. It might get me in to trouble later if the behavior of the AlarmManager changes.

Nanne
  • 64,065
  • 16
  • 119
  • 163

1 Answers1

13

Since the cancel method for AlarmManager is fine with a 'similar' Intent to cancel the alarm, we can say that the platform recognizes the intent given the class name. Hence calling this repeatedly shouldn't be a problem since the platform will know that for such a pending intent an alarm already exists.

Here is a post that talks something similar.

Community
  • 1
  • 1
advantej
  • 20,155
  • 4
  • 34
  • 39
  • Hmm, thanks! I see from that question that the `Pendingintent` is indeed the same (as the docs say). But would calling `setInexactRepeating` with the same `PendingIntent` twice also be ... non-repeating? – Nanne Apr 18 '11 at 19:40
  • I can't find any reference, but currently at least starting an alarm fo the same `PendingIntent` does not make it trigger more then once. I think i'll just top checking if I;ve allready set it. Much easier that way. Thanks – Nanne Apr 18 '11 at 20:31
  • 1
    Is there also a possibility that on calling `setInexactRepeating` multiple times, the alarm timer would be reset? Like if the interval was 15 minutes and 7 minutes had passed, then would a second call reset the interval time to 0? – aandis Oct 23 '15 at 15:42