Difference between setRepeating and setInexactRepeating of AlarmManager
Asked Answered
V

5

33

What are the parameters of the following:

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent);

And of the following:

alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);

What is the difference and also how are the two different in terms of functionality?

Volkman answered 20/1, 2014 at 11:35 Comment(0)
P
39

Both examples schedule a repeating alarm that will send the given alarmIntent. On both cases, the first time it is sent will be immediate (calendar.getTimeInMillis() returns the current time). On both cases, the device will be woken up when the alarm needs to be sent (as evident by AlarmManager.RTC_WAKEUP).

There are two differences between these calls. The simpler one is that the intent will be sent every fifteen minutes on the first call, and every day on the second call (as you can see in the third parameter). The more complicated difference is the function call itself: setRepeating will schedule the first alarm for exactly every fifteen minutes; setInexactRepeating will schedule the second alarm for approximately every 24 hours, meaning it might deviate from that interval - with the advantage of consuming less power.

Do notice that this has changed in API 19, where these two calls are synonymous. See this guide, and this API documentation.

Primarily answered 20/1, 2014 at 12:22 Comment(4)
So if I target SDK 18 and kip my Max sdk 19, do I have to code for both separately?Volkman
Also if I am binding a service to be invoked after the alarm is fired, do I need to acquire a wake lock?Volkman
Whether to code for both cases separately is your decision, and based on your necessities. What I can tell you is that the behavior of setRepeating() matches the behavior of setInexactRepeating() only when targetSdkVersion is 19. For any lower targetSdkVersion, their behaviors differ.Primarily
In the context of the given alarmIntent, the device will stay awake. If you are to leave the context, the device might go back to sleep. So, for example, if your alarmIntent starts an activity, it's certain the device will be awake for the activity to handle the intent. But if you then want to start a service from your activity by calling startService(), then yes - you must take a wake lock to ensure that the service will start. See this for an example.Primarily
B
20

Decide how precise your alarm needs to be

Choosing the alarm type is often the first step in creating an alarm. A further distinction is how precise you need your alarm to be.

For most apps, setInexactRepeating() is the right choice. When you use this method, Android synchronizes multiple inexact repeating alarms and fires them at the same time. This reduces the drain on the battery.

For the rare app that has rigid time requirements as example, the alarm needs to fire precisely at 4:00 p.m. everyday then use setRepeating().

Reference: Decide how precise your alarm needs to be

Baalman answered 20/1, 2014 at 12:24 Comment(1)
very good explanation specially about setRepeating(). Thank you.Jimmyjimsonweed
E
9

To augment previous answers, there are a number of other best practices to consider when using repeating alarms, particularly inexact alarms requested using setInexactRepeating().

Alarm Type

  • Non-WAKEUP alarms are better than WAKEUP alarms from a power management perspective. Using the former your alarm may fire late, but it will still fire either when the device is woken by the user, or when another wakeup alarm fires. Using WAKEUP alarms will wake the device out of sleep, consuming additional battery and potentially causing other inexact alarms to fire that have been delayed that could otherwise have been delayed longer (reducing the batching power-saving benefits that inexact alarms provide).
  • Prefer alarms using the ELAPSED timebase rather than the RTC timebase. The former are more likely to have a more random distribution across devices than RTC alarms, which reduces the risk of network congestion and on the server if the alarm is triggering some sort of poll. Phones running Gingerbread (or older) suffer from a bug whereby RTC inexact alarms have a tendency to align closely to the real-time clock, e.g. approximately 30s past each quarter of an hour. ELAPSED alarms don't suffer from this bug on these earlier platform versions. Even if your alarm doesn't trigger any network activity, remember that if it is a wakeup alarm it may trigger other alarm non-wakeup intents that may hit the network.

Timebase

  • Be careful to specify the requested start time in the correct time domain for the alarm type. Failure to do this can result in alarms being set in the past (they fire right away) if setting an RTC alarm with an ELAPSED timebase or far in the future if setting an ELAPSED alarm using the RTC timebase. You can check what alarms an app has scheduled using dumpsys alarm via the adb shell.

Interval

  • Specifying an inexact alarm interval of anything other than the interval constants defined in the AlarmManager API is redundant on SDK <19: they will be scheduled as exact not inexact alarms, losing all the power-saving benefits that inexact alarms provide.

Edit: here's further explanation of the bug relating to gingerbread and honeycomb 3.0 devices: https://code.google.com/p/android/issues/detail?id=31550

Evenings answered 24/4, 2014 at 22:28 Comment(0)
P
4

setRepeating is more accurate and setInexactRepeating is for saving battery but no accurate , setInexactRepeating is good for maintenance in background for example and setRepeating is necessary for example for alarm clock .

Procne answered 16/2, 2015 at 16:16 Comment(0)
E
0

Use setInexactRepeating() is used when app is not seriously used to required for example wake up early in the morning . if Alarm wake up approximately that time there is no any life dangeous. like medicine pills app where highly critical patient use that app to reminde nurse or doctor staff the is compulsory to use setRepeating(). When you use setInexactRepeating() then Android synchronizes repeating alarms from multiple apps and fires them at the same time(once). This reduces the total number of times the system must wake the device. thus reducing drain on the battery. repeating alarms are inexact. Note that while setInexactRepeating() is an improvement over setRepeating() also it can still overwhelm a server if every instance of an app hits the server around the same time. Therefore, for network requests, add some randomness to your alarms.

Etz answered 28/4, 2020 at 13:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.