This article describes the usage of the Android alarm service AlarmManager. Share it for your reference, as follows:
Corresponding to AlarmManage, there is an AlarmManagerServie service program, which truly provides alarm service. It mainly maintains various alarms registered by the application and sets the alarm that will be triggered to the alarm device in time (in the system, the device name of Linux implemented by Linux is "/dev/alarm"), and it keeps monitoring the alarm device. Once an alarm triggers or an alarm event occurs, the AlarmManagerServie service program will traverse the alarm list and find the corresponding registered alarm and send a broadcast. The service program is started by the system service program system_service and the alarm device (/dev/alarm) is initialized when the system is started. Of course, there is another layer of encapsulation between the AlarmManagerService in the JAVA layer and the Linux Alarm driver interface, that is JNI.
After AlarmManager separates applications and services, application developers do not need to care about specific services, but use this service directly through AlarmManager. This may be the benefit of the customer/service model. The AlarmManager and AlarmManagerServie communicate through Binder, and they have a many-to-one relationship.
In android system, AlarmManage provides 3 interfaces and 5 types of alarm services.
3 interfaces:
// Cancel the registered alarm matching the parametervoid cancel(PendingIntent operation) //Register a new alarmvoid set( int type, long triggerAtTime, PendingIntent operation) //Register a duplicate alarmvoid setRepeating( int type, long triggerAtTime, long interval, PendingIntent operation) //Set time zonevoid setTimeZone(String timeZone)
Java code:
// Cancel the registered alarm matching the parametervoid cancel(PendingIntent operation) //Register a new alarmvoid set(int type, long triggerAtTime, PendingIntent operation) //Register a duplicate alarmvoid setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) //Set time zonevoid setTimeZone(String timeZone)
5 alarm types
public static final int ELAPSED_REALTIME // This type of alarm will not wake the system when the system enters sleep. It is not passed until the system is awakened next time. The time taken for the alarm is relative time, which starts from the start of the system, including the sleep time, which can be obtained by calling (). The system value is 3 (0x00000003).public static final int ELAPSED_REALTIME_WAKEUP //Can wake up the system, the usage is the same as ELAPSED_REALTIME, the system value is 2 (0x000000002).public static final int RTC //When the system enters sleep state, this type of alarm will not wake the system. It is not passed until the system is awakened next time. The time used for the alarm is absolute time, and the time used is UTC time, which can be obtained by calling (). The system value is 1 (0x00000001).public static final int RTC_WAKEUP //Can wake up the system, the usage is the same as RTC type, and the system value is 0 (0x00000000).Public static final int POWER_OFF_WAKEUP //Can wake up the system, it is a shutdown alarm, which means that the device can also wake up the system when it is shut down, so we call it a shutdown alarm. The usage method is the same as RTC type, and the system value is 4 (0x00000004).
Java code:
public static final int ELAPSED_REALTIME //When the system enters sleep state, this type of alarm will not wake the system. It is not passed until the system is awakened next time. The time taken for the alarm is relative, and it is counted from the start of the system, including sleep.time,Can be called by()get。The system value is3 (0x00000003)。 public static final int ELAPSED_REALTIME_WAKEUP //Can wake up the system, the usage is the same as ELAPSED_REALTIME, the system value is 2 (0x000000002).public static final int RTC //When the system enters sleep state, this type of alarm will not wake the system. It is not passed until the system is awakened next time. The time used for the alarm is absolute, and the time used is UTC time, which can be called ()get。The system value is1 (0x00000001) 。 public static final int RTC_WAKEUP //Can wake up the system, the usage is the same as RTC type, and the system value is 0 (0x00000000).Public static final int POWER_OFF_WAKEUP //Can wake up the system, it is a shutdown alarm, which means that the device can also wake up the system when it is shut down, so we call it a shutdown alarm. The usage method is the same as RTC type, the system value is4(0x00000004)。
Note an important parameter PendingIntent. This PendingIntent can be said to be a further encapsulation of the Intent. It includes both the description of the Intent and the execution of the Intent behavior (this definition may not be strict). If the Intent is compared to an order, PendingIntent is more like an order placing an order, because it is responsible for sending the order and handling the order after the order is sent. For example, after the sending is successful, it is necessary to prepare to accept the order goods, and after the sending fails, it is necessary to resend or cancel the order. 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 play a broadcast function, just like calling the () function. When the system wants to send an intent through it, it must use the form of broadcast, and the corresponding intent receiving object will be included in the intent. Of course, we can specify this object when creating a PendingIntent, or we can automatically find the behavior processing object through descriptions such as ACTION and CATEGORY.
Intent intent = new Intent(AlarmController. this , OneShotAlarm. class ); PendingIntent sender = ( , 0 , intent, 0 );
Java code:
Intent intent = new Intent(, ); PendingIntent sender = (, 0, intent, 0);
getActivity - The PendingIntent obtained through this function can directly start a new activity, just like a call (Intent). However, it is worth noting that if this new activity is no longer the Activity that exists in the current process. We must use Intent.FLAG_ACTIVITY_NEW_TASK in the intent.
// The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = (this , 0 , new Intent( this , AlarmService. class ), 0 );
Java code:
// The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = (this, 0, new Intent(this, ), 0);
getService - The PengdingIntent obtained through this function can directly start a new service, just like calling().
// Create an IntentSender that will launch our service, to be scheduled // with the alarm manager. mAlarmSender = ( , 0 , new Intent(AlarmService. this , AlarmService_Service. class ), 0 );
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.