AlarmManager class in Android

Source: Internet
Author: User

I. Statement: Public class AlarmManager extends Object

Ii. Category structure:
Java. lang. Object
? Android. app. AlarmManager
Iii. Overview:This class provides a method that is close to the system alarm service, allowing you to set a future time point to execute your application. 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 your mobile phone may enter sleep after being processed during broadcast. If the receiver of your alarm broadcast 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.

Since API 19, the trigger of the alarm is not accurate: the operating system will use the alarm time to change, in order to reduce the wake-up and battery usage. New APIs support applications that require strict alarm trigger time. For more information, see:setWindow(int, long, long, PendingIntent)AndsetExact(int, long, PendingIntent)For versions earlier than API 19, refer to the previous processing methods. They are triggered exactly according to the request.

4. constants:

1. public static final int ELAPSED_REALTIME the alarm time is defined by SystemClock. elapsedRealtime. The alarm will 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 constant value is 3.

2. public static final int ELAPSED_REALTIME_WAKEUP the alarm time is defined by SystemClock. elapsedRealtime. The device is awakened when an alarm is triggered. The constant value is 2.

3. public static final long INTERVAL_DAY available inaccurate recurrence interval. PassSetInexactRepeating (int, long, long, PendingIntent) to identify. Run on or before API 19. Constant: 86400000

4. public static final long interval_deleteen_minutes available inaccurate recurrence interval. PassSetInexactRepeating (int, long, long, PendingIntent) to identify. Run on or before API 19. Constant: 900000

5. public static final long INTERVAL_HALF_DAY available inaccurate recurrence interval. PassSetInexactRepeating (int, long, long, PendingIntent) to identify. Run on or before API 19. Constant: 43200000

6. public static final long INTERVAL_HALF_HOUR available inaccurate recurrence interval. PassSetInexactRepeating (int, long, long, PendingIntent) to identify. Run on or before API 19. Constant: 1800000

7. public static final long INTERVAL_HOUR available inaccurate recurrence interval. PassSetInexactRepeating (int, long, long, PendingIntent) to identify. Run on or before API 19. Constant: 3600000

8. The public static final intRTC time is defined by System. currentTimeMillis. The alarm will 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 constant value is 1.

9. public static final intRTC_WAKEUP this time is defined by System. currentTimeMillis. The device is awakened when an alarm is triggered. The constant value is 0.

V. Method:

1. public voidcancel (PendingIntent operation) removes the alarm based on the matching Intent. Intent is encapsulated in operation. Any type of alarm will be canceled if this condition is met.

2. set an alarm in public voidset (int type, long triggerAtMillis, PendingIntent operation.

Note: Handler is easier to use for common time operations. If the same IntentSender alarm is set before, the previous first choice will be canceled. If the set time is earlier than the current time, the alarm will be triggered immediately.

Parameter: type: OneELAPSED_REALTIME,ELAPSED_REALTIME_WAKEUP,RTC, OrRTC_WAKEUP.

3. public voidsetExact (int type, long triggerAtMillis, PendingIntent operation) sets an alarm at a specified time, similarset(int, long, PendingIntent)However, this method does not allow the operating system to adjust the trigger time, and the alarm will be triggered at the request time as much as possible.

Note: This method is used only when the alarm time is required to be accurate. The application does not support this method unless necessary because it reduces the operating system's battery usage.

4、public voidsetInexactRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)

When you set a duplicate alarm, the trigger time is inaccurate. This method saves power than setRepeating (int, long, long, PendingIntent. Because the system can adjust the trigger time to avoid unnecessary device wakeup.

5. set public void setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) to set the repeated alarm.

6. Set the system wall clock in public void setTime (long millis. Android. permission. SET_TIME. permission is required.

7. public void setTimeZone (String timeZone) is used to set the default time zone of the system. android. permission. SET_TIME_ZONE. permission is required.

Parameter: supported by TimeZone.

8. public void setWindow (int type, long windowStartMillis, long windowLengthMillis, PendingIntent operation) sets an alarm to trigger at a given time window. Similarset(int, long, PendingIntent)This method allows the application to precisely control the operating system to adjust the trigger time of the alarm.

Parameters:

WindowStartMillis The earliest time, in milliseconds, that the alarm shocould be delivered, expressed in the appropriate clock's units (depending on the alarm type ).
WindowLengthMillis The length of the requested delivery window, in milliseconds. The alarm will be delivered no later than this has milliseconds afterwindowStartMillis. Note that this parameter isDuration,Not the timestamp of the end of the window.

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.