Android-based alarmmanager (Global Timer/alarm clock) indicates the scheduled duration or periodical execution of an operation.

Source: Internet
Author: User

 

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 );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.