Handling alarms
Now that we know how to schedule alarms, let's take a look at what we can schedule with them.
Essentially, we can schedule anything that can be started with a PendingIntent
, which means we can use alarms to start Activities, Services, and BroadcastReceivers. To specify the target of our alarm, we need to use the static factory methods of PendingIntent
:
PendingIntent.getActivities(…) PendingIntent.getActivity(…) PendingIntent.getService(…) PendingIntent.getBroadcast(…)
In the following sections, we'll see how each type of PendingIntent
can be used with AlarmManager
.
Handling alarms with Activities
Starting an Activity
from an alarm is as simple as registering the alarm with a PendingIntent
created by invoking the static getActivity
method of PendingIntent
.
When the alarm is delivered, the Activity
will be started and brought to the foreground, displacing any app that was currently in use. Keep in mind that this is likely to surprise and perhaps annoy users!
When starting Activities...