Android Alarmmanager (Global Timer/alarm) Specify the length of time or perform an action in a periodic manner

Source: Internet
Author: User

The use mechanism of Alarmmanager is called the global timer, and some call it an alarm clock. Through the use of it, the personal feel called the global timer is more appropriate, in fact, its role and timer a bit similar. There are two similar usages: (1) Perform an action after a specified length of time (2) perform an operation periodically

Alarmmanager objects are used in conjunction with intent, you can start an activity on a timed basis, send a broadcast, or open a service.

The following code describes the use of two timing methods in detail:

(1) Perform an action after a specified length of time

//action: Send a broadcast, broadcast received after the toast prompt timed operation completedIntent Intent =NewIntent (Main. This, Alarmreceiver.class); Intent.setaction (" Short"); Pendingintent Sender=pendingintent.getbroadcast (Main. This, 0, intent, 0);//set a time after five seconds.Calendar 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 simplify it in the following way//Alarm.set (Alarmmanager.rtc_wakeup, System.currenttimemillis () +5*1000, sender);
//Note: Receiver remembers registering in Manifest.xml Public Static classAlarmreceiverextendsbroadcastreceiver{@Override Public voidOnReceive (Context context, Intent 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 (); }  }}

The Alarmmanager types are as follows:

ALARMMANAGER.RTC, the hardware alarm clock, does not wake up the phone (or other devices) hibernate, when the phone does not fire alarm when hibernating.

Alarmmanager.rtc_wakeup, hardware alarm clock, when the alarm is launched to wake up the phone hibernation;

Alarmmanager.elapsed_realtime, the real time passes the alarm clock, does not wake the phone to sleep, when the phone sleeps, does not emit the alarm clock.

Alarmmanager.elapsed_realtime_wakeup, the real time passes the alarm clock, when the alarm clock launches wakes up the phone to sleep;

The biggest difference between RTC alarms and Elapsed_realtime is that the former can trigger alarm events by modifying the phone's time, which passes through the real time, even in the dormant state, and the time is calculated.

(2) Periodic execution of an operation

Intent Intent =NewIntent (Main. This, Alarmreceiver.class); intent.setaction ( "Repeating"); Pendingintent Sender=pendingintent.getbroadcast (Main. This, 0, intent, 0);//Start TimeLongFirstime=Systemclock.elapsedrealtime (); Alarmmanager am=(Alarmmanager) Getsystemservice (alarm_service);//5 seconds, one cycle, no stop sending broadcasts.am.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firstime,5*1000, sender);

Alarmmanager's setrepeating () is equivalent to a timer's schedule (task,delay,peroid); a bit of a difference when the timer this method is to specify how long the delay starts after the periodic execution of the task;

Alarmmanager Cancellation: (It is important to note that the canceled intent must be in absolute agreement with the startup intent to support the cancellation Alarmmanager)

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

Android Alarmmanager (Global Timer/alarm) Specify the length of time or perform an action in a periodic manner

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.