AlarmManager for Android Development

Source: Internet
Author: User

AlarmManager for Android Development

AlarmManager is essentially a global timer, a system-level prompt Service commonly used in Android. It starts 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 to execute your application at a certain time point in the future. When your alarm goes off (Time is up), an Intent 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 sleep state (you can choose whether to wake up the device at a given time ). If the alarm is disabled or restarted, the alarm is cleared.

As long as the broadcast onReceive () method is being executed, the alarm Manager (AlarmManager) will hold a CPU wake-up lock to ensure that the phone will not sleep until the broadcast is completed, once onReceive () then the alarm manager will release the wake-up lock. This means that as long as the OnReceive () method is complete, your mobile phone may enter sleep in some cases. If your alarm notification Receiver calls Context. startService (), the mobile phone may enter sleep before the requested service is executed. To prevent this situation, your BroadcastReceiver and service need to implement a separate wake-up lock policy to ensure that the mobile phone continues to run until the service is available.

Note: This class applies to scenarios where you want your application to be executed at a specific time point in the future, even if your application is not running now. Handler is easier and more efficient for general time operations.

Public method:

Public Methods

Void

Cancel (PendingIntent operation)

Cancel the scheduled service of AlarmManager.

Void

Set (int type, long triggerAtTime, PendingIntent operation)

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

Void

SetInexactRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)

Set an inaccurate periodic task.

Void

SetRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)

Set a scheduled service for periodic execution.

Void

SetTime (long millis)

Set the system "wall" clock. Android. permission. SET_TIME. permission is required.

Void

SetTimeZone (String timeZone)

Set the default time zone of the system. Android. permission. SET_TIME_ZONE. permission is required.

Common Methods:

There are three common AlarmManager methods:

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

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

The first parameter, int type, specifies the type of the scheduled service. The parameter accepts the following values:

? ELAPSED_REALTIME

After the specified delay, the system sends a broadcast but does not wake up the device (the alarm clock is unavailable during sleep ). If the alarm is triggered during system sleep, it will not be passed until the next device wakes up.

? ELAPSED_REALTIME_WAKEUP

After the specified delay, send the broadcast and wake up the device (the operation component is executed even if the device is shut down ).

Latency refers to the system startup time SystemClock. elapsedRealtime (). The specific usage depends on the code.

? RTC

Specify the device that starts operation when the value returned by the System call System. currentTimeMillis () method is equal to triggerAtTime (send broadcast at the specified time, but do not wake up the device ). If the alarm is triggered during system sleep, it will not be passed until the next device wakes up (the alarm is unavailable during sleep ).

? RTC_WAKEUP

Specify the device that starts operation when the value returned by the System call System. currentTimeMillis () method is equal to triggerAtTime (send broadcast and wake up the device at the specified time ). The operation component is executed even if the system is shut down.

The second parameter indicates the time when the alarm is executed.

The third parameter PendingIntent pi indicates the alarm response action:

PendingIntent pi: indicates the execution of an alarm, for example, sending a broadcast or giving a prompt. PendingIntent is the encapsulation class of Intent. It should be noted that if the alarm is triggered by starting the service, the PendingIntent object should be obtained using Pending. getService (Context c, int I, Intentintent, int j) method; if the alarm is triggered through broadcast, the PendingIntent object should be obtained using PendingIntent. getBroadcast (Context c, inti, Intent intent, int j) method; if the alarm is triggered by an Activity, the PendingIntent object should be obtained by PendingIntent. getActivity (Context c, inti, Intent intent, int j) method. If these three methods are used incorrectly, although no error is reported, the alarm is not displayed.

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

Set a scheduled service for periodic execution. The first parameter indicates the alarm type, the second parameter indicates the first execution time of the alarm, the third parameter indicates the interval between two executions of the alarm, 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 repeated alarms, similar to the second method, but the interval between the two alarms is not fixed. It is relatively more power-efficient because the system may merge several similar alarms into one to reduce the number of device wake-up times. The third parameter intervalTime is the alarm interval. the built-in variables are as follows:

INTERVAL_DAY: Set the alarm interval for one day.

INTERVAL_HALF_DAY: set an alarm, with an interval of half a day

Interval_effecteen_minutes: sets the alarm time interval of 15 minutes.

INTERVAL_HALF_HOUR: set an alarm, with an interval of half an hour.

INTERVAL_HOUR: set an alarm time interval of one hour.

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.