0

I am writing my first alarm program. The program sets a repeating alarm, which is expected to trigger every 200mseconds. But the point is, instead of that, the interval is almost 40 seconds!! And it seems that whatever interval time I set it doesn't really matter. So in reality it seems that after API 19 there is no way to make setRepeating() kind of an alarm to trigger more often, than ~40 seconds, right? Here is a snippet of the code:

if(alarmRepetition.equalsIgnoreCase(context.getString(R.string.alarm_once))){
      Intent backIntent = new Intent("Time to delete an Alarm kva-kva");
      backIntent.putExtra("Time to delete", alarmId);
      Calendar calendarNow = Calendar.getInstance();
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, alarmId, backIntent, 0);
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      alarmManager.setRepeating(AlarmManager.RTC, calendarNow.getTimeInMillis(), 200, pendingIntent);
}
  • Does this answer your question? [Is there an accurate repeating-alarm system for Android?](https://stackoverflow.com/questions/25948871/is-there-an-accurate-repeating-alarm-system-for-android) – Andre Classen Mar 24 '21 at 22:29
  • 1
    A repeating alarm every 200ms seems like an abuse of the system's resources. Is this a toy example to test, or are you actually trying to do something every 200ms? If so, AlarmManager is almost certainly the wrong tool. You probably want a foreground service that just runs things every 200ms in-process or something. – Ryan M Mar 25 '21 at 00:11
  • Hi Ryan! Of course it is a toy example! I definitely wouldn't need an alarm every 200ms in any real situation. – StanislavOssovsky Mar 25 '21 at 02:43
  • Hi Andre. I know that there are several options. But what I cannot get is why do we really need the third parameter in the setRepeating() function, if it really doesn't mean anything. Whether I set it to be 5 seconds, or a fraction of a second - still the alarm repeats in around 40 seconds minimum. – StanislavOssovsky Mar 25 '21 at 02:46

1 Answers1

2

As per the docs

as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above

AlarmManager will try to schedule alarms together to save battery life

tyczj
  • 71,600
  • 54
  • 194
  • 296
  • Does it mean that the third parameter in setRepeating() function will only play any role, when it is actually >40000ms? (because on average it seems that alarms are re-triggered every 40 sec minimum). It is strange... If there are such constraints why would the API still allow for much smaller values?.. So there is no way to make a repeating alarm repeat more often since API 19? – StanislavOssovsky Mar 25 '21 at 02:48
  • 1
    In your example it is unlikely it will ever get triggered on time because of the extremely short window between alarms but if you were to have say one an hour you could see it be triggered on time sometimes. IIRC there is a 15 minute window from the trigger time in which it can fire. "Why does the third parameter allow for such small values?", for backwards compatibility. To be able to create an alarm that fires exactly when you want it you have to do what the docs say and use `setExact`, when that fires you then create another alarm with setExact and you now have your repeating alarm. – tyczj Mar 25 '21 at 12:08
  • Got it! Thank you! – StanislavOssovsky Mar 25 '21 at 14:20