The corresponding alarmmanage has a Alarmmanagerservie service program, the service program is really provide alarm service, it mainly maintains the application registration of the various types of alarm and timely set up the impending alarm to alarm device (in the system, the Linux implementation of the device named "/dev/alarm"), and has been monitoring the alarm device, and once an alarm is triggered or an alarm event occurs, the Alarmmanagerservie service will traverse the alarm list to find the appropriate registration alarm and issue a broadcast. The service program starts and initializes the alarm device (/DEV/ALARM) at system boot time by the system service program System_service. 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 from the service, the application developer does not care about the specific service, but uses the service directly through the Alarmmanager. This may be the benefit of the customer/service model. Alarmmanager and Alarmmanagerservie are communicated through binder, and they are a many-to-one relationship.
In the Android system, Alarmmanage provides 3 interface 5 types of alarm services.
3 Interfaces:
View plain Copy to clipboard print?
- Cancels a registered alarm that matches a parameter
- void Cancel (Pendingintent operation)
- //Register 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) /c1>
- //Set time zone
- void Settimezone (String timeZone)
Java code
- Cancels a registered alarm that matches a parameter
- void Cancel (Pendingintent operation)
- //Register 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
View plain Copy to clipboard print?
- Public static final int elapsed_realtime
- This type of alarm does not wake the system when the system goes to sleep. Until the next time the system wakes up to pass it, the time spent in the alarm is relative to the time it starts, including the sleep time, which can be obtained by calling Systemclock.elapsedrealtime (). The system value is 3 (0x00000003).
- public static final int elapsed_realtime_wakeup
- //Can wake up the system, use the same elapsed_realtime, the system value is 2 (0x00000002).
- public static final int RTC
- This type of alarm does not wake the system when the system goes to sleep. It is not delivered until the next time the system wakes up, the time spent in the alarm is absolute, and the time spent is UTC time, which can be obtained by calling System.currenttimemillis (). The system value is 1 (0x00000001).
- public static final int rtc_wakeup
- //Can wake up the system using the same RTC type with a system value of 0 (0x00000000).
- Public static final int power_off_wakeup
- //Can wake up the 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. Using the same method as the RTC type, the system value is 4 (0x00000004).
Java code
- Public
- static final int elapsed_realtime
- This type of alarm does not wake the system when the system goes to sleep. It is not delivered until the next time the system wakes up, and the time spent in the alarm is relative to the time it starts after the system starts, including sleep
- Time, which can be obtained by calling Systemclock.elapsedrealtime (). The system value is 3 (0x00000003).
- public static final int elapsed_realtime_wakeup
- //Can wake up the system, use the same elapsed_realtime, the system value is 2 (0x00000002).
- public static final int RTC
- This type of alarm does not wake the system when the system goes to sleep. Until the next time the system wakes up to pass it, the time spent in the alarm is absolute, and the time spent is UTC time, which can be called by the
- System.currenttimemillis () obtained. The system value is 1 (0x00000001).
- public static final int rtc_wakeup
- //Can wake up the system using the same RTC type with a system value of 0 (0x00000000).
- Public static final int power_off_wakeup
- Can wake up the 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. Using the same method as the RTC type, the system value is
- 4 (0x00000004).
Note An important parameter, pendingintent. This pendingintent can be said to be intent further encapsulation, he contains both the intent description and the execution of the Intent Act (this definition may be less stringent), if intent is likened to an order, Pendingintent more like a person to place an order, because it is responsible for sending orders, but also responsible for the processing of orders sent, such as after the successful delivery of goods to prepare acceptance orders, send failed to re-return 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 an instance of pendingintent.
getbroadcast--the pendingintent obtained by this function will play a broadcast function, just like calling the Context.sendbroadcast () function. When the system through it to send a intent to use the form of broadcast, and in the intent will contain the corresponding intent receive object, of course, this object can be specified when creating pendingintent, or through the action and category descriptions let the system automatically find the behavior of the object processing.
View plain Copy to clipboard print?
- 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 by this function can start the new activity directly, just like calling Context.startactivity (Intent) But it's worth noting that this new activity is no longer the activity of the current process. We must use Intent.flag_activity_new_task in the intent.
View plain Copy to clipboard print?
- The pendingintent to launch we activity if the user selects this notification
- Pendingintent contentintent = pendingintent.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 = pendingintent.getactivity (this, 0, new Intent (This, Alarmservice. Class), 0);
getservice--the pengdingintent obtained by this function can start the new service directly, just like calling Context.startservice ().
View plain Copy to clipboard print?
- Create an 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);
Learn more about Android's Alarmmanager