Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 28  Background Services


Repeating alarms: not so exact


The setRepeating(...) method sets a repeating alarm, but that repetition is not exact. In other words,
Android reserves the right to move it around a little bit. As a result, 60 seconds is the lowest possible
interval time you can set for stock Android. (Other devices may elect to make this value higher.)


This is because alarms can really blow a hole in your phone’s battery management. Every time an
alarm fires, the device has to wake up and spin up an application. Many apps need to turn on the
phone’s radio to use the internet like PhotoGallery does, which results in even more battery usage.


If it were only your app, the exactness of the alarm would not matter. After all, if your alarm wakes up
every 15 minutes, and your app is the only 15-minute alarm running, then the phone will wake up and
turn on its radio four times an hour no matter how precise the alarm is.


If it were your app and nine other apps with exact 15-minute alarms, though, things change. Because
every alarm is exact, the device needs to wake up for each one. That means turning on the radio 40
times an hour instead of four times.


Inexactness means that Android is allowed to take the liberty of moving those alarms around, so that
they do not run exactly every 15 minutes. The result is that every 15 minutes, your device can wake up
and run all 10 of those 15-minute alarms at the same time. That would pull you back down to only four
wake-ups instead of 40, saving all kinds of battery in the process.


Some apps really do need exact alarms, of course. If that is your app, then you must use either
AlarmManager.setWindow(...) or AlarmManager.setExact(...), which allow you to set an exact alarm
to occur only once. The repeating part you have to handle yourself.


Time basis options


Another important decision is which time basis value to specify. There are two main options:
AlarmManager.ELAPSED_REALTIME and AlarmManager.RTC.


AlarmManager.ELAPSED_REALTIME uses the amount of time that has passed since the last boot of the
device (including sleep time) as the basis for interval calculations. ELAPSED_REALTIME is the best
choice for your alarm in PhotoGallery because it is based on the relative passage of time and thus does
not depend on clock time. (Also, the documentation recommends you use ELAPSED_REALTIME instead
of RTC if at all possible.)


AlarmManager.RTC uses clock time in terms of UTC. UTC should only be used for clock-basis
alarms. However, UTC does not respect locale, whereas the user’s idea of clock time includes locale.
Clock-basis alarms should respect locale somehow. This means you must implement your own locale
handling in conjunction with using the RTC time basis if you want to set a clock-time alarm. Otherwise,
use ELAPSED_REALTIME as the time basis.


If you use one of the time basis options outlined above, your alarm will not fire if the device is in
sleep mode (the screen is turned off), even if the prescribed interval has passed. If you need your
alarm to occur on a more precise interval or time, you can force the alarm to wake up the device
by using one of the following time basis constants: AlarmManager.ELAPSED_REALTIME_WAKEUP and
AlarmManager.RTC_WAKEUP. However, you should avoid using the wake-up options unless your alarm
absolutely must occur at a specific time.

Free download pdf