Alarm Controller demonstrates how to use Alarm events in Android applications. Its functions are similar to java. util. Timer and TimerTask. However, Alarm can execute a Schedule task at the specified time even after the current application exits.
AlarmManager is used to manage Alarm events and supports one-time execution or repeated execution. Like most Android services, AlarmManager uses getSystemService to obtain service objects:
[Java]
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
The task description type corresponding to TimerTask is PendingIntent. PendingIntent describes the Intent to be executed. PendingIntent does not provide constructors. You need to use the static function getActivity (Context, int, Intent, int ), getBroadcast (Context, int, Intent, int), getService (Context, int, Intent, int), or get the description of the Activity, Broadcast, and Service to be executed.
In this example, the descriptions of Broadcast OneShotAlarm and RepeatingAlarm are obtained, which correspond to the Broadcast events executed at a time and the Broadcast events executed at multiple times. They are defined as Broadcast Receiver in AndroidManifest. xml:
<Cycler android: name = ". app. OneShotAlarm" android: process = ": remote"/>
<Cycler android: name = ". app. RepeatingAlarm" android: process = ": remote"/>
The code for a Schedule single Alarm event is as follows:
[Java]
Intent intent = new Intent (AlarmController. this, OneShotAlarm. class );
PendingIntent sender = PendingIntent. getBroadcast (AlarmController. this,
0, intent, 0 );
// We want the alarm to go off 30 seconds from now.
Calendar calendar = Calendar. getInstance ();
Calendar. setTimeInMillis (System. currentTimeMillis ());
Calendar. add (Calendar. SECOND, 30 );
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
Am. set (AlarmManager. RTC_WAKEUP, calendar. getTimeInMillis (), sender );
Intent intent = new Intent (AlarmController. this, OneShotAlarm. class );
PendingIntent sender = PendingIntent. getBroadcast (AlarmController. this,
0, intent, 0 );
// We want the alarm to go off 30 seconds from now.
Calendar calendar = Calendar. getInstance ();
Calendar. setTimeInMillis (System. currentTimeMillis ());
Calendar. add (Calendar. SECOND, 30 );
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
Am. set (AlarmManager. RTC_WAKEUP, calendar. getTimeInMillis (), sender );
Sender is the description of the Intent sent to Broadcast Receiver er OneShotAlarm. When the specified time is reached (30 seconds in this example), AlarmManager sends a Broadcast Intent to OneShotAlarm. After OneShotAlarm receives the message, A message will be displayed on the screen using Toast. If you click "One Shot Alarm" multiple times, there will not be multiple Alarm events in Schedule. This is because Schedule is the same Sender object and the Scheduled event will be canceled in the next time.
The code for a duplicate Schedule event is as follows:
[Java]
Intent intent = new Intent (AlarmController. this, RepeatingAlarm. class );
PendingIntent sender = PendingIntent. getBroadcast (AlarmController. this,
0, intent, 0 );
// We want the alarm to go off 30 seconds from now.
Long firstTime = SystemClock. elapsedRealtime ();
FirstTime ++ = 15*1000;
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
Am. setRepeating (AlarmManager. ELAPSED_REALTIME_WAKEUP,
FirstTime, 15*1000, sender );
Intent intent = new Intent (AlarmController. this, RepeatingAlarm. class );
PendingIntent sender = PendingIntent. getBroadcast (AlarmController. this,
0, intent, 0 );
// We want the alarm to go off 30 seconds from now.
Long firstTime = SystemClock. elapsedRealtime ();
FirstTime ++ = 15*1000;
// Schedule the alarm!
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
Am. setRepeating (AlarmManager. ELAPSED_REALTIME_WAKEUP,
FirstTime, 15*1000, sender );
The above code sends a Broadcast event to RepeatingAlarm every 15 seconds. After receiving this event, RepeatingAlarm also displays a message on the screen.
For events with Schedule, you can call the cancel Method of AlarmManager to cancel the Alarm event of Schedule once or multiple times. The following code cancels multiple Alarm events.
[Java]
Intent intent = new Intent (AlarmController. this, RepeatingAlarm. class );
PendingIntent sender = PendingIntent. getBroadcast (AlarmController. this,
0, intent, 0 );
// And cancel the alarm.
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
Am. cancel (sender );
Intent intent = new Intent (AlarmController. this, RepeatingAlarm. class );
PendingIntent sender = PendingIntent. getBroadcast (AlarmController. this,
0, intent, 0); www.2cto.com
// And cancel the alarm.
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
Am. cancel (sender );
Note: If you do not have the Cancel Alarm event for multiple times, a message will be displayed on the screen every 15 seconds, even if you exit this example or start other applications, it will not be aborted until after Reboot.
Author: mapdigit