Android development of AlarmManager usage details

  • 2020-06-03 08:14:40
  • OfStack

AlarmManager in Android is essentially a global timer, a system-level prompt service commonly used in Android to start other components (including Activity,Service,BroadcastReceiver) at specified times or periodically.

1. Overview:

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

As long as the broadcast's onReceive() method is running, the alarm manager (AlarmManager) holds an CPU waken lock. This is to ensure that the phone does not sleep until the broadcast is finished and 1 once onReceive() returns, the alarm manager will release the waken lock. This means that as long as OnReceive () method, your phone may in some cases into dormancy, if your alarm clock radio receiver call is Context startService (), the mobile phone may be before the requested service execution into dormancy, in order to prevent this kind of situation, your BroadcastReceiver and services need to implement a separate wake lock strategies to ensure mobile phones continue to run, until the service is available.

Note here: 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 right now. Using Handler is easier and more efficient for 1 - to - 1 - time operations.

2. Public method (Public Methods) :


void cancel(PendingIntent operation)

Cancel AlarmManager's scheduled service.


void set(int type, long triggerAtTime, PendingIntent operation)

Set to start the component specified by the operation parameter at triggerAtTime time. (This method is used to set the one-second alarm clock)


void setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)

Set 1 imprecise periodic task.


void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)

Set up a periodic service that executes periodically.


void setTime(long millis)

Set the system "wall" clock. android. permission. SET_TIME. permissions are required.


void setTimeZone(String timeZone)

Sets the default time zone for the system. android. permission. SET_TIME_ZONE. permission is required.

3. Description of common methods:
There are three common methods of AlarmManager:

(1)


set(int type,long startTime,PendingIntent pi)

This method is used to set a sex alarm clock.
The first parameter, int type, specifies the type of timing service and accepts the following values:

ELAPSED_REALTIME: After the specified delay, broadcast but does not wake the device (the alarm clock is not available while asleep). If the alarm is triggered during system sleep, it will not be passed on until the next device wakes up.

ELAPSED_REALTIME_WAKEUP: After the specified delay, broadcast and wake up the device (operation's counterpart is executed even after shutdown).
The delay is calculated by taking into account the time the system starts up.

RTC: Specifies the device to which operation is started when the system call method System.currentTimeMillis () returns a value equal to triggerAtTime (at the specified time, broadcast but not wake the device). If the alarm is triggered while the system is asleep, it will not be passed on until the next device wakes up (the alarm is not available while asleep).

RTC_WAKEUP: Specifies the device to which operation is started when the value returned by the system call method System.currentTimeMillis () is equal to triggerAtTime (at the specified time, broadcast and wake up the device). operation components are executed even when the system is shut down.

The second parameter represents the alarm execution time.

The third parameter PendingIntent pi represents the alarm response action:

PendingIntent pi: Is the action of the alarm clock, such as sending a broadcast, giving a prompt, etc. PendingIntent is the wrapper class for Intent. It should be noted that PendingIntent object should be acquired using Pending. getService(Context c,int i,Intentintent,int j) method if alarm prompt is realized by starting the service. If the alarm is broadcast, the PendingIntent object should be acquired using PendingIntent. getBroadcast(Context c,inti,Intent intent,int j) method. PendingIntent (Context c,inti,Intent intent,int j) should be used to obtain the PendingIntent. getActivity(Context c,inti,Intent intent,int j). If these three methods are used incorrectly, although the error will not be reported, but you will not see the alarm prompt effect.

(2)


setRepeating(int type,long startTime,long intervalTime,PendingIntent pi)

Set up a periodic service that executes periodically. The first parameter represents the alarm type, the second parameter represents the first execution time of the alarm, 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 the repeat alarm, similar to the second method, except that the interval between the two alarms is not fixed. It is relatively less power-efficient (ES157en-ES158en) because the system may combine several similar alarm clocks into one for execution, reducing the number of device wakeups. The third parameter, intervalTime, is the alarm interval with several built-in variables as follows:

INTERVAL_DAY: Set the alarm for one day
INTERVAL_HALF_DAY: Set the alarm half a day apart
INTERVAL_FIFTEEN_MINUTES: Set the alarm at 15-minute intervals
INTERVAL_HALF_HOUR: Set the alarm half an hour apart
INTERVAL_HOUR: Set the alarm at an hour interval


Related articles: