Android Alarm Service Alarmmanager usage In-depth analysis _android

Source: Internet
Author: User
Tags set time

This example describes the Android alarm service Alarmmanager usage. Share to everyone for your reference, specific as follows:

The corresponding alarmmanage has a Alarmmanagerservie service program, which is true to provide alarm service, it mainly maintains the application registration of various types of alarm and timely settings will trigger the alarm alarm device (in the system, the Linux implementation of the device named "/dev/alarm"), and has been listening to alarm devices, once the alarm trigger or alarm event occurs, the Alarmmanagerservie service program will traverse the alarm list to find the appropriate registration alarm and broadcast. The service program is started by the system service program System_service and initializes the alarm device (/DEV/ALARM) at system startup. Of course, there is a layer of encapsulation between the Java layer's alarmmanagerservice and the Linux alarm driver interface, which is JNI.

Alarmmanager the application with the service, so that the application developers do not care about the specific services, but directly through the Alarmmanager to use the service. This may be the benefit of the customer/service model. Between Alarmmanager and Alarmmanagerservie is through the binder to communicate, they are a many-to-many relationship.

In the Android system, the Alarmmanage provides 3 interface 5 types of alarm services.

3 Interfaces:

Cancels the registered alarm
void Cancel (Pendingintent operation) that matches the parameter
//registers a new alarm
void set (int type, long triggerattime, Pendingintent operation)
//Register a duplicate type of alarm
void setrepeating (int type, long triggerattime, long interval, Pendingintent operation)
//Set time zone
void Settimezone (String timeZone)

Java code:

Cancels the registered alarm
void Cancel (Pendingintent operation) that matches the parameter
 //registers a new alarm
void set (int type, long Triggerattime, pendingintent operation)
 //register a repeating type of alarm
void setrepeating (int type, long triggerattime, long Interval, pendingintent operation)
 //Set time zone
void Settimezone (String timeZone)

5 Alarm Types

public static final int elapsed_realtime
//When the system goes to sleep, this type of alarm does not wake the system. Until the system is awakened the next time it is passed, the alarm time is relative, from the start of the system after the timer, including sleep time, can be obtained by calling Systemclock.elapsedrealtime (). The system value is 3 (0x00000003). The public
static final int elapsed_realtime_wakeup
//Can wake the system, using the same elapsed_realtime, the system value is 2 (0x00000002). Public
static final int RTC
//When the system goes to sleep, this type of alarm does not wake the system. Until the system is awakened the next time it is passed, the alarm is spent in absolute time, in UTC time, and can be obtained by calling System.currenttimemillis (). The system value is 1 (0x00000001). Public
static final int rtc_wakeup
//Wake up system, usage same RTC type, System value 0 (0x00000000). Public
static final int power_off_wakeup
//Wake up system, it is a shutdown alarm, that is, the device in the shutdown state can also wake the system, so we call it shutdown alarm. Use method with RTC type, System value is 4 (0x00000004).

Java code:

 The public static final int elapsed_realtime//When the system goes to sleep, this type of alarm does not wake the system. Until the system is awakened the next time it is passed, the alarm time is relative, from the start of the system after the timer, including sleep time, can be obtained by calling Systemclock.elapsedrealtime ().
The system value is 3 (0x00000003).
The public static final int elapsed_realtime_wakeup//can wake the system, using the same elapsed_realtime, the system value is 2 (0x00000002). public static final int RTC//When the system goes to sleep, this type of alarm does not wake the system. Until the system is awakened the next time it is passed, the alarm is spent in absolute time, in UTC time, and can be obtained by calling System.currenttimemillis ().
The system value is 1 (0x00000001).
public static final int rtc_wakeup//Wake up system, usage same RTC type, System value 0 (0x00000000). public static final int power_off_wakeup//Wake up system, it is a shutdown alarm, that is, the device in the shutdown state can also wake the system, so we call it shutdown alarm.

Use method with RTC type, System value is 4 (0x00000004). 

Note An important parameter pendingintent. This pendingintent can be said to be a further encapsulation of intent, which includes both the intent description and the execution of the Intent Act (which may be less stringent), and if intent is likened to an order, Pendingintent more like a person who orders, because it is responsible for the delivery of orders, but also responsible for the processing of orders sent, such as after the successful delivery to prepare acceptance order goods, send the failure to resend is canceled orders and other operations. Developers can call
getactivity (context, int, Intent, int)
Getbroadcast (context, int, Intent, int)
GetService (context, int, Intent, int)

Three different ways to get a pendingintent instance.

getbroadcast--the pendingintent obtained through this function will act as a broadcast function, just as you would call the Context.sendbroadcast () function. When the system uses it to send a intent to the form of broadcast, and in the intent will contain the corresponding intent receive objects, of course, this object we can create the pendingintent when specified, can also be through the action and category to allow the system to automatically find the behavior processing object.

Intent Intent = new Intent (Alarmcontroller. This, Oneshotalarm. Class);
Pendingintent sender = Pendingintent.getbroadcast (alarmcontroller.this, 0, intent, 0);

Java code:

Intent Intent = new Intent (alarmcontroller.this, oneshotalarm.class);
Pendingintent sender = Pendingintent.getbroadcast (alarmcontroller.this, 0, intent, 0);

getactivity--the pendingintent obtained through this function can start a new activity directly, just like calling Context.startactivity (Intent) , but it is worth noting that this new activity is no longer an activity that exists in the current process. We must use Intent.flag_activity_new_task in the intent.

The pendingintent to launch we activity if the user selects this notification
pendingintent contentintent = Pendin Gintent.getactivity (this, 0, new Intent (This, Alarmservice. Class), 0);

Java code:

The pendingintent to launch we activity if the user selects this notification
pendingintent contentintent = Pendin Gintent.getactivity (this, 0, the new Intent (this, alarmservice.class), 0);

getservice--the pengdingintent obtained through this function can start a new service directly, just like calling Context.startservice ().

Create a intentsender that would launch our service, to be scheduled
//with the alarm manager.
Malarmsender = Pendingintent.getservice (Alarmservice.this,
 0, New Intent (Alarmservice. This, alarmservice_ Service. Class), 0);

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android debugging techniques and common problems solution summary", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary

I hope this article will help you with the Android program.

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.