Alarmmanager is a global timer, which is a system-level prompt service commonly used in Android that launches other components (including activity,service,broadcastreceiver) at a specified time or periodically.
Overview:
This class provides a way to access the system alarm service, allowing you to set up a certain point in the future to execute your application. When your alarm clock goes off (time to), an intent (Intent) that is registered on it 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 (if the alarm is ringing at a given time, you can choose whether to wake the device). If the alarm is turned off or restarted, the alarm will be cleared.
As long as the broadcast OnReceive () method is executing, the alarm Manager (Alarmmanager) holds a wake-up lock on the CPU, which is to ensure that the phone does not hibernate until the broadcast is processed, and once the OnReceive () returns, the alarm manager will release the wake-up lock. This means that as long as the OnReceive () method is complete, your phone may go into hibernation in some cases, and 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 situation, Your broadcastreceiver and services need 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 (pendingintentoperation)
Cancels the Alarmmanager timer service.
void
Set (int
Type, long triggerattime,pendingintentoperation)
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,pendingintentoperation)
Set a non-accurate recurring task.
void
setrepeating (int
Type, long triggerattime, long interval,pendingintentoperation)
Set up a recurring timer service.
void
SetTime (Long Millis)
Sets the system "wall" clock. Requires Android.permission.SET_TIME. Permissions.
void
Settimezone (Stringtimezone)
Sets the default time zone for the system. Requires Android.permission.SET_TIME_ZONE. Permissions.
Common Method Description:
There are three common methods of Alarmmanager:
(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:
? 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 also executes the component corresponding to the operation).
Delay is to put the system start Time systemclock.elapsedrealtime () in, the specific use of the code to see.
? Rtc
Specifies the device that initiates operation when the system calls the System.currenttimemillis () method, which is equal to Triggerattime (the broadcast is sent at the specified moment, but the device is not awakened). 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 the device that initiates operation when the system calls the System.currenttimemillis () method to return the equivalent of Triggerattime (the broadcast is sent and the device is awakened at the specified moment). 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: Is the operation of the alarm clock, such as sending a broadcast, giving hints and so on. Pendingintent is the encapsulation class for intent. It is important to note that if the alarm is implemented by starting the service, the Pendingintent object should be acquired using Pending.getservice (Context
C,int I,intentintent,int J) method, if the alarm is realized by broadcasting, the Pendingintent object should be acquired using Pendingintent.getbroadcast (Context C,inti, Intent
Intent,int j) method, if the alarm is implemented in an activity way, the Pendingintent object should be acquired using pendingintent.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, as opposed 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 the alarm, one day at a intervals
Interval_half_day: Set alarm, interval half day
Interval_fifteen_minutes: Set alarm, 15 minutes interval
Interval_half_hour: Set the alarm to be half an hour apart
Interval_hour: Set alarm, one hour interval
Alarmmanager of Android Development