In AndroidAlarmManager is essentially a global timer, a system-level prompt service commonly used in Android, which starts other components (including Activity, Service, BroadcastReceiver) at a specified time or periodically.
1. Overview:
This class provides a way to access system alarm services, allowing you to set it to execute your application at a certain point in the future. When your alarm clock rings (time is up), an intent registered on it will be broadcasted by the system and the target program will be automatically started if it is not running. The registered alarm clock will be retained even if the device is in sleep (if the alarm clock rings at a given time, you can choose whether to wake up the device). If the alarm is turned off or restarted, the alarm will be cleared.
As long as the broadcast's onReceive() method is being executed, the alarm manager will hold a CPU wake-up lock, which is to ensure that the phone will not sleep until the broadcast is processed. Once the onReceive() returns, the alarm manager will release the wake-up lock. This means that as long as the OnReceive() method is completed, your phone may go to hibernate in some cases. If your alarm broadcast receiver calls (), then the phone may go to hibernate before the requested service is executed. To prevent this, your BroadcastReceiver and service need to implement a separate wake-up lock policy to ensure that the phone continues to run until the service is available.
Note here:This class is suitable for situations where you want the application to execute at a specified point in the future, even if your application is not running now. For general time operations, it is easier and more efficient to use Handler.。
2. Public methods:
void cancel(PendingIntent operation)
Cancel the AlarmManager scheduled service.
void set(int type, long triggerAtTime, PendingIntent operation)
Sets the triggerAtTime time to start the component specified by the operation parameter. (This method is used to set a disposable alarm clock)
void setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
Set up an inaccurate periodic task.
void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
Set up a timed service that is periodically executed.
void setTime(long millis)
Set the system "wall" clock. Requires .SET_TIME. permission.
void setTimeZone(String timeZone)
Sets the default time zone of the system. Requires .SET_TIME_ZONE. permission.
3. Description of common methods:
There are three common methods for AlarmManager:
(1)
set(int type,long startTime,PendingIntent pi)
This method is used to set a disposable alarm clock.
The first parameter int type specifies the type of the timing service, which accepts the following values:
ELAPSED_REALTIME: After the specified delay is passed, the broadcast is sent, but the device is not awakened (the alarm clock is not available in sleep). If the alarm is triggered while the system is sleeping, it will not be passed until the next device wakes up.
ELAPSED_REALTIME_WAKEUP: After the specified delay is passed, the broadcast is sent and the device is awakened (the corresponding component of the operation will be executed even if the power is shut down).
Delay is to count the system startup time (), the specific usage depends on the code.
RTC: Specifies the device corresponding to the operation when the value returned by the system call() method is equal to triggerAtTime (at the specified time, the broadcast is sent, but the device is not awakened). If the alarm is triggered while the system is sleeping, it will not be passed until the next device wakes up (the alarm is not available in sleep).
RTC_WAKEUP: Specifies the device corresponding to the operation when the value returned by the system call() method is equal to triggerAtTime (at the specified time, the broadcast is sent and the device is awakened). Even if the system shuts down, the corresponding components of the operation will be executed.
The second parameter indicates the alarm execution time.
The third parameter PendingIntent pi represents the alarm response action:
PendingIntent pi: is the execution action of the alarm clock, such as sending a broadcast, giving a prompt, etc. PendingIntent is an encapsulation class of Intent. It should be noted that if the alarm prompt is implemented by starting the service, the PendingIntent object should be obtained by using the (Context c, int i, Intent, int j) method; if the alarm prompt is implemented by broadcast, the (Context c, inti, Intent intent, int j) method should be obtained by using the (Context c, inti, Intent intent, int j) method; if the alarm prompt is implemented by using the Activity method, the (Context c, inti, Intent intent, int j) method should be obtained by using the (Context c, inti, Intent intent, int j) method. If these three methods are used incorrectly, although there will be no error, the alarm clock prompt effect will not be seen.
(2)
setRepeating(int type,long startTime,long intervalTime,PendingIntent pi)
Set up a timed service that is periodically executed. The first parameter represents the alarm clock type, the second parameter represents the first execution time of the alarm clock, the third parameter represents the interval between the two executions of the alarm clock, and the third parameter represents the alarm clock response action.
(3)
setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi)
This method is also used to set a repeating alarm clock, similar to the second method, but the interval between the execution of the two alarm clocks is not fixed. It is relatively more power-efficient because the system may combine several similar alarm clocks into one to perform, reducing the number of wake-up times of the device. The third parameter intervalTime is the alarm interval, and several built-in variables are as follows:
INTERVAL_DAY: Set the alarm clock, one day apart
INTERVAL_HALF_DAY: Set the alarm clock, half a day interval
INTERVAL_FIFTEEN_MINUTES: Set the alarm clock, 15 minutes interval
INTERVAL_HALF_HOUR: Set the alarm clock, half an hour apart
INTERVAL_HOUR: Set the alarm clock, one hour apart