The Alarmmanager usage of Android development _android

Source: Internet
Author: User

Android's Alarmmanager is essentially a global timer, a system-level hint that is commonly used on Android to start other components at specified times or periodically (including Activity,service, Broadcastreceiver).

First, overview:

This class provides a way to access the system alarm service, allowing you to set up a point in the future to execute your application. When your alarm goes off (time is up), an intent (Intent) registered on it will be broadcast by the system and automatically start the target program if it is not running. The registered alarm clock is preserved even if the device is in hibernation (if the alarm goes off at a given time, you can choose whether to wake the device). If the alarm clock is turned off or restarted, the alarm will be cleared.

As long as the OnReceive () method of the broadcast is in progress, the Alarm Clock Manager (Alarmmanager) holds a CPU wakeup lock, which is to ensure that the phone does not hibernate until the broadcast is processed, and the alarm manager will release the wake-up lock once OnReceive () returns. This means that as long as the OnReceive () method is complete, your cell phone may go into hibernation in some cases, and if your alarm clock radio receiver is calling Context.startservice (), then the phone may go into hibernation before the requested service is executed, in order to prevent this, Your broadcastreceiver and services need to implement a separate wakeup lock policy to ensure that the phone continues to run until the service is available.

Note here: This class applies to situations where you want your application to perform at a certain point in the future, even if your application is not running now. For general time operation, it is easier and more efficient to use handler .

Ii. Public methods (publicly Methods):

void Cancel (Pendingintent operation)

Cancel the Alarmmanager of the 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 clock)

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

Set up an inexact recurring task.

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

Set up a timed service that executes periodically.

void SetTime (Long Millis)

Set the system "wall" clock. Need Android.permission.SET_TIME. Permission.

void Settimezone (String timeZone)

Sets the default time zone for the system. Need Android.permission.SET_TIME_ZONE. Permission.

Three, commonly used method description:
There are three common methods for Alarmmanager:

(1)

Set (int type,long starttime,pendingintent pi)

This method is used to set up a one-time alarm.
The first parameter int type specifies the type of the timer service, which accepts the following values:

Elapsed_realtime: Sends the broadcast after the specified delay, but does not wake the device (the alarm clock is not available in sleep). If the alarm is triggered when the system sleeps, it will not be delivered until the next time the device wakes up.

Elapsed_realtime_wakeup: After the specified delay, send the broadcast and wake the device (even if the shutdown also executes the corresponding component of the operation).
delay is to put the system start Time systemclock.elapsedrealtime () , the specific use of the code.

RTC: Specifies the device that initiates the operation when the system calls the value returned by the System.currenttimemillis () method is equal to Triggerattime (sending a broadcast at a specified time, but not waking the device). If the alarm is triggered when the system sleeps, it will not be passed until the next time the device wakes up (the alarm clock is not available in sleep).

Rtc_wakeup: Specifies the device that initiates the operation when the system calls the value returned by the System.currenttimemillis () method is equal to Triggerattime (at the specified time, sends the broadcast and wakes the device). Even the system shutdown performs the component that operation corresponds to.

The second parameter represents the clock execution time.

The third parameter, Pendingintent Pi, indicates an alarm response action:

Pendingintent PI: It is the action of the alarm clock, such as sending a broadcast, giving a hint and so on. The pendingintent is a intent encapsulation class. It is important to note that if the alarm is implemented by starting the service, the acquisition of the Pendingintent object should be based on the Pending.getservice (context C,int I,intentintent,int j) method If the alarm is realized by radio, the acquisition of the Pendingintent object should adopt the Pendingintent.getbroadcast (context c,inti,intent Intent,int j) method. If an activity is used to implement the alarm clock, the pendingintent.getactivity (context c,inti,intent Intent,int J) method should be used to obtain the Pendingintent object. If these three methods are wrong, although not the error, but do not see the alarm effect.

(2)

setrepeating (int type,long starttime,long intervaltime,pendingintent pi)

Set up a timed service that executes periodically. The first parameter represents the alarm type, the second parameter represents the first execution time of the alarm clock, the third parameter indicates the interval between the two times the alarm is performed, and the third parameter indicates the alarm response action.

(3)

setinexactrepeating (int type,long starttime,long intervaltime,pendingintent pi)

This method is also used to set up a repeat alarm, similar to the second method, although the interval of two alarm clocks is not fixed. It is relatively more power-saving (power-efficient) because the system may combine several similar alarms into one to perform, reducing the number of times the device wakes up. The third parameter intervaltime the alarm interval, with several built-in variables as follows:

Interval_day: Set alarm clock, one day interval
Interval_half_day: Set alarm clock, half day interval
Interval_fifteen_minutes: Set alarm clock, 15 minutes interval
Interval_half_hour: Set the alarm clock, half an hour interval
Interval_hour: Set alarm clock, one hours interval

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.