Setting Repeating Alarms

The Alarm Manager lets you set repeating alarms for situations requiring regularly scheduled events. Repeating alarms work in exactly the same way as the one-shot alarms described earlier, but will continue to trigger at a specified interval until canceled.

Because alarms are set outside your Application context they are perfect for scheduling regular updates or data lookups so that they don't require a Service to be constantly running in the background.

To set a repeating alarm, use the setRepeating or setlnexactRepeating method on the Alarm Manager, as shown in Listing 9-27. Both support an alarm type, an initial trigger time, and a Pending Intent to fire when the alarm triggers (as described in the previous section).

Use setRepeating when you need fine-grained control over the exact interval of your repeating alarm. The interval value passed in to this method lets you specify an exact interval for your alarm, down to the millisecond.

The setlnexactRepeating method is a powerful technique for reducing the battery drain associated with waking the device on a regular schedule to perform updates. Rather than specifying an exact interval, this method accepts one of the following Alarm Manager constants:

> INTERVAL_FIFTEEN_MINUTES

> INTERVAL_HALF_HOUR

> INTERVAL_HOUR

> INTERVAL_HALF_DAY

> INTERVAL_DAY

At run time Android will synchronize multiple inexact repeating alarms and trigger them simultaneously. This prevents each application from separately waking the device in a similar but non-overlapping period to perform an update or poll a network data source. By synchronizing these alarms the system is able to limit the impact of regularly repeating events on battery resources.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment