Alarmmanager of Android Development

Source: Internet
Author: User

Alarmmanager The essence is a global timer that is Android a system-level prompt service that starts other components at a specified time or periodically, including Activity,service,broadcastreceiver ).

Overview:

          This class provides a way to access the system alarm service, Allows you to set up a certain point in the future to execute your application. When your alarm clock goes off (time to), an intent is registered on it (Intent) will be broadcast by the system and then automatically start the target program if it is not running. Registered alarms are retained even if the device is in hibernation ( )

          As long as the broadcast onreceive () method is executing, this alarm clock manager (Alarmmanager) cpu wake-up lock, this is to ensure that the phone does not hibernate until the broadcast is processed, once the onreceive () Returns, then the alarm manager will release the wake-up lock. This means that as long as onreceive () method done, your phone may go into hibernation in some cases, If your alarm broadcast receiver calls context.startservice () , Then the phone may go into hibernation before the requested service executes, in order to prevent this, your broadcastreceiver and the service needs to implement a separate wake-up lock policy to ensure that the phone continues to run until the service is available.

Note: This class is for situations where you want your application to execute at a specified point in the future, even if your application is not running now. For general time operations, the use of Handler is easier and more efficient.

Public method:

Public Methods

void

Cancel (pendingintent operation)

Cancels the Alarmmanager timer service.

void

Set (int type, long triggerattime, pendingintent operation)

Sets the component specified by the operation parameter to start at triggerattime time. (This method is used to set a one-time alarm)

void

setinexactrepeating (int type, long triggerattime, long interval, pendingintent operation)

Set a non-accurate recurring task.

void

setrepeating (int type, long triggerattime, long interval, pendingintent operation)

Set up a recurring timer service.

void

settime (Long Millis)

Sets the system "wall" clock. Requires Android.permission.SET_TIME. Permissions.

void

Settimezone (String timeZone)

Sets the default time zone for the system. Requires Android.permission.SET_TIME_ZONE. Permissions.

Common Method Description:

Alarmmanager There are three common methods:

(1)set (int type,long startTime,pendingintent pi)

This method is used to set a one-time alarm.

The first argument int type Specifies the type of the timer service, which accepts the following values:

? Elapsed_realtime

after the specified delay, the broadcast is sent, but the device is not awakened (the alarm is not available in the sleep state). If the alarm is triggered when the system sleeps, it will not be passed until the next time the device wakes up.  

? Elapsed_realtime_wakeup

after the specified delay, the broadcast is sent and the device is awakened (even if the shutdown is performed Operation the corresponding component) .

Delay is the time to start the system systemclock.elapsedrealtime () count it in, see the code for specific usage.  

? RTC

specifies when the system calls System.currenttimemillis () method returns the value associated with the Triggerattime Start when equal Operation The corresponding device (at the specified moment, sends the broadcast, but does not wake the device). If the alarm is triggered when the system sleeps, it will not be delivered until the next time the device wakes up (the alarm is not available in sleep).

? Rtc_wakeup

specifies when the system calls System.currenttimemillis () method returns the value associated with the Triggerattime Start when equal Operation The corresponding device (at the specified moment, send the broadcast, and wake the device). the components corresponding to operation are executed even if the system shuts down.

The second parameter indicates the alarm execution time.

The third parameter, pendingintent pi , indicates the alarm response action:

pendingintent Pi: It is the operation of the alarm clock, such as sending a broadcast, giving hints, etc. pendingintentis aIntentEncapsulation class. It is important to note that if you implement an alarm prompt by starting the service,pendingintentobject acquisition should be based on thePending.getservice (Context c,int i,intentintent,int j)method, if the alarm is realized by broadcasting,pendingintentobject acquisition should be based on thependingintent.getbroadcast (Context c,inti,intent intent,int j)If the method is usedActivitythe way to implement alarm alerts,pendingintentobject acquisition should be based on thependingintent.getactivity (Context c,inti,intent intent,int j)method. If these three methods are wrong, although not error, but do not see the alarm effect.

(2)setrepeating (int type,long startTime,long intervaltime, pendingintent Pi)

Set up a recurring timer service. The first parameter represents the alarm type, the second parameter represents the alarm first execution time, the third parameter represents the interval between two executions of the alarm, and the third parameter represents the alarm response action.

(3)setinexactrepeating(int type,long startTime,Long IntervalTime,pendingintent pi)

This method is also used to set a repeating alarm, similar to the second method, although the interval between the two alarms is not fixed. It is relatively more power-saving (power-efficient), because the system may combine several similar alarms into one to execute, reducing the number of wake-up devices. The third parameter, IntervalTime, is the alarm interval, with several variables built in as follows:

Interval_day : set an alarm, one day at a intervals

Interval_half_day : Set the alarm, half a day apart

interval_fifteen_minutes : set Alarm, Interval the minutes

Interval_half_hour : Set the alarm to be half an hour apart

Interval_hour : set an alarm, one hour interval

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.