Android alarm manager timed alert clock Development

Source: Internet
Author: User

Alarm Manager manages the hardware clock.

Some time-related applications, such as calendars and alarms, need to use alarm Manager Services. The alarm manager function is relatively simple, and the relevant code is located in
Frameworks/base/CORE/JNI/Server/com_android_server_alarmmanagerservice.cpp
Frameworks/base/services/Java/COM/Android/Server/alarmmanagerservice. Java

I. Frameworks/base/CORE/JNI/Server/com_android_server_alarmmanagerservice.cpp
This part of the Code directly manages the hardware clock. The device name is/dev/alarm. This includes enabling the device, disabling the device, setting the time zone, setting the trigger time (timeout), and waiting for the clock to trigger.
Ii. Frameworks/base/services/Java/COM/Android/Server/alarmmanagerservice. Java
This part encapsulates the code in the directory, provides a Java interface up, interacts with the client (such as calendar), receives the request for clock settings from the client, and notifies the client when the clock is triggered.

Alarm is an independent method that triggers intent at a scheduled time.

 

Alarm is out of application scope, so they can be used to trigger application events or actions, even after the application is closed. Combined with broadcast referers, they can become particularly powerful. You can set alarm to start the application or execute actions, and the application does not need to be opened or active.

 

For example, you can use alarm to implement an alarm program, perform normal network queries, or schedule time-consuming or costly operations during off-peak hours.

 

The combination of the handler class and the timer and thread classes is a better choice for scheduled operations only within the application lifecycle. It allows android to better control system resources.

 

Alarm in Android remains active when the device is in sleep mode. It can be set to wake up the device. However, all alarm is canceled when the device is restarted.

 

Alarmmanager is used to process alarm operations and getsystemservice is used to obtain system services, as shown below:

 

Alarmmanager alarms = (alarmmanager) getsystemservice (context. alarm_service );

 

To create a new alarm, use the set method and specify an alarm type, trigger time, and intent to be called when alarm is triggered. If the set alarm occurs in the past, it will be triggered immediately.

 

There are four types of alarm. Your choice will determine what the time value you pass in the set method represents, which is a specific time or time passing:

 

❑ Rtc_wakeup

Wake up the device to trigger intent at the specified time (when alarm is set.

 

❑ Rtc

Triggers intent at an explicit time, but does not wake up the device.

 

Cancelapsed_realtime

After the device is started, if the elapsed time reaches the total time, intent is triggered, but the device is not awakened. The elapsed time includes any time the device sleeps. Note that the computing point of time elapsed is counted from the last time it was started.

 

Cancelapsed_realtime_wakeup

After the device is started, if you need to wake up the device and trigger intent after the total time elapsed.

 

The following is a demonstration of the alarm creation process:

 

Int alarmtype = alarmmanager. elapsed_realtime_wakeup;

Long timeorlengthofwait = 10000;

String alarm_action = "alarm_action ";

Intent intenttofire = new intent (alarm_action );

Pendingintent = pendingintent. getbroadcast (this, 0, intenttofire, 0 );

Alarms. Set (alarmtype, timeorlengthofwait, pendingintent );

 

When alarm arrives, the pendingintent you specified will be triggered. Set another alarm and use the same pendingintent to replace the existing alarm.

 

Cancel an alarm and call alarmmanager's Cancel Method to pass in the pendingintent you no longer want to be triggered, as shown in the following code:

 

Alarms. Cancel (pendingintent );

 

In the following code snippet, two alarm instances are set, and the first alarm instance is immediately canceled. The first alarm explicitly sets the device to wake up and send intent at a specific time. The second step is to set the duration to 30 minutes after the device is started. After the arrival time, if the device is sleeping, it will not wake up.

 

Alarmmanager alarms = (alarmmanager) getsystemservice (context. alarm_service );

 

String my_rtc_alarm = "my_rtc_alarm ";

String alarm_action = "my_elapsed_alarm ";

Pendingintent rtcintent = pendingintent. getbroadcast (this, 0, new intent (my_rtc_alarm), 1 );

Pendingintent elapsedintent = pendingintent. getbroadcast (this, 0, new intent (alarm_action), 1 );

 

// Wakeup and fire intent in 5 hours. (The comment may be incorrect)

Date T = new date ();

T. settime (Java. Lang. system. currenttimemillis () + 60*1000*5 );

Alarms. Set (alarmmanager. rtc_wakeup, T. gettime (), rtcintent );

 

// Fire intent in 30 mins if already awake.

Alarms. Set (alarmmanager. elapsed_realtime, 30*60*1000, elapsedintent );

 

// Cancel the first alarm.

Alarms. Cancel (rtcintent );

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.