Recent re-investigation of mobile phone standby current is high, tracking to the Alarmmanager, it was related to the understanding.
The use mechanism of Alarmmanager is called the global timer, and some call it an alarm clock. By using it, it has a somewhat similar effect to the timer.
There are two similar uses:
(1) Perform an action after a specified length of time
(2) Periodic execution of an operation
In the Android system, the underlying system provides two types of clocks, soft clocks and hard clocks, the soft clock is what we often call the Timer, the hard clock is the RTC. While the system is functioning normally, the timer works to provide time service and alarm reminders, and when the system goes to sleep, the time service and alarm alarms are taken care of by RTC. For upper-level applications, we don't need to care if it's a timer or RTC to serve us because the framework layer of the Android system encapsulates the underlying details and provides the API uniformly. This API his name is called Alarmmanager. In the Android system is interesting is that the corresponding alarmmanage has a Alarmmanagerservie service program, the service program is really provide alarm service, it mainly maintains the application of the various types of alarm and set the alarm to be triggered in a timely manner to the alarm device ( In Android, Linux implements a device named "/dev/alarm") and listens to the alarm device, and once an alarm is triggered or an alarm event occurs, the Alarmmanagerservie service traverses the alarm list to find the appropriate registration alarm and broadcasts it. 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.
Common interface and
void Cancel (Pendingintent operation)//cancel a registered alarm that matches a parameter
void set (int type, long triggerattime, pendingintent operation)//register a new alarm
void setrepeating (int type, long triggerattime, long interval, pendingintent operation)//register a repeating type of alarm
void Settimezone (String timeZone)//Set time zone
5 Types of alarms:
Public static final int rtc_wakeup = 0;
The hardware alarm clock, when the alarm clock occurs, wakes the phone to sleep,//can wake the system, uses the same RTC type, and the system value is 0 (0x00000000).
Public static final int RTC = 1;
Hardware alarm clock, do not wake up the phone (or other devices) hibernate, when the phone is dormant, do not send 躰 alarm clock.
/* 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 elapsed_realtime_wakeup = 2;
The real time passes the alarm clock, when the alarm clock sends 躰 to wake up the phone to sleep;//can wake the system, use the same elapsed_realtime, the system value is 2 (0x00000002).
public static final int elapsed_realtime = 3;
/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). */
The biggest difference between RTC alarms and Elapsed_realtime is that the former can trigger alarm events by modifying the phone's time, which passes through the real time, even in the dormant state, and the time is calculated.
ADB Shell Dumpsys Alarm
This command allows you to view events that are placed in the scheduled queue
Rtc_wakeup #1: alarm{52c2ad84 type 0 Com.sina.weibo}
Type=0 when=+3m1s330ms repeatinterval=0 count=0
operation=pendingintent{529fa514:pendingintentrecord{52a7b220 Com.sina.weibo Broadcastintent}}
If we can see the alarm of one of these tweets, if we want to see his intent details,
You can use the following command.
ADB shell Dumpsys activity intents
And find the part where ID is 52a7b220.
* pendingintentrecord{52a7b220 Com.sina.weibo Broadcastintent}
uid=10073 Packagename=com.sina.weibo type=broadcastintent flags=0x0
requestcode=1383195892 Requestresolvedtype=null
Requestintent=act=alarmtaskschedule
Sent=true Canceled=false
REF:
http://blog.csdn.net/lee576/article/details/8073694/
http://blog.segmentfault.com/masaila/1190000000404684