Scheduling repeating alarms
As well as setting a one-off alarm, we have the option to schedule repeating alarms using setRepeating()
and setInexactRepeating()
. Both methods take an additional parameter that defines the interval in milliseconds at which to repeat the alarm. Generally, it is advisable to avoid setRepeating()
and always use setInexactRepeating()
, allowing the system to optimize device wake-ups and giving more consistent behavior on devices running different Android versions:
void setRepeating( int type, long triggerAtMillis, long intervalMillis, PendingIntent operation); void setInexactRepeating( int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
AlarmManager
provides some handy constants for typical repeat intervals:
AlarmManager.INTERVAL_FIFTEEN_MINUTES AlarmManager.INTERVAL_HALF_HOUR AlarmManager.INTERVAL_HOUR AlarmManager.INTERVAL_HALF_DAY AlarmManager.INTERVAL_DAY
Let's now build up an example that...