Some of the usage mechanisms of alarmmanager are called Global timers and some are called alarm clocks. Through its use, I personally think it is more appropriate to call a global timer. In fact, its role is similar to that of timer. There are two similar usage: (1) execute an operation after the specified duration (2) Periodically execute an operation
When the alarmmanager object is used with intent, you can enable an activity regularly, send a broadcast, or enable a service.
The followingCodeThe following describes the usage of the two timer methods in detail:
(1) perform an operation after the specified duration
// Operation: Send a broadcast. After receiving the broadcast, the toast prompts the scheduled operation to complete intent = new intent (main. this, alarmreceiver. class); intent. setaction ("short"); pendingintent sender = pendingintent. getbroadcast (main. this, 0, intent, 0); // set the time after a five-second calendar = calendar. getinstance (); Calendar. settimeinmillis (system. currenttimemillis (); Calendar. add (calendar. second, 5); alarmmanager alarm = (alarmmanager) getsystemservice (alarm_service); alarm. set (alarmmanager. rtc_wakeup, calendar. gettimeinmillis (), Sender); // or use the following method to simplify the process: // alarm. set (alarmmanager. rtc_wakeup, system. currenttimemillis () + Five * 1000, Sender); toast. maketext (main. this, "enable alarm in five seconds", toast. length_long ). show ();
// Note: the consumer remembers to register in manifest. xml.
Public static class alarmreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {// todo auto-generated method stubif (intent. getaction (). equals ("short") {toast. maketext (context, "short alarm", toast. length_long ). show ();} else {toast. maketext (context, "repeating alarm", toast. length_long ). show ();}}}
(2) Periodically execute an operation
Intent intent = new intent (main. this, alarmreceiver. class); intent. setaction ("repeating"); pendingintent sender = pendingintent. getbroadcast (main. this, 0, intent, 0); // start time long firstime = systemclock. elapsedrealtime (); alarmmanager AM = (alarmmanager) getsystemservice (alarm_service); // the broadcast is continuously sent for a period of 5 seconds. setrepeating (alarmmanager. elapsed_realtime_wakeup, firstime, 5*1000, Sender );
The setrepeating () of alarmmanager is equivalent to the Schedule (task, delay, peroid) of timer. In some differences, the timer method specifies the delay time.
Periodically execute tasks in the future;
Cancellation of alarmmanager: (it must be noted that the canceled intent must be absolutely consistent with the intent to be started before alarmmanager can be canceled)
Intent intent = new intent (main. this, alarmreceiver. class); intent. setaction ("repeating"); pendingintent sender = pendingintent. getbroadcast (main. this, 0, intent, 0); alarmmanager alarm = (alarmmanager) getsystemservice (alarm_service); alarm. cancel (sender );