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 with alarms, we will probably want to set Intent.FLAG_ACTIVITY_CLEAR_TOP
; so that if the application is already running, and our target Activity
is already on the back stack, the new intent will be delivered to the old Activity
and all the other activities on top of it will be closed:
Intent intent = new Intent(context, HomeActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pending = PendingIntent.getActivity( Context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Not all Activities are suited to being started with getActivity
. We...