Corresponding to AlarmManage, there is an AlarmManagerServie service program which actually provides the alarm service, it mainly maintains various types of alarms registered by the application and sets the triggered alarms to the device (in the system, the device name implemented by linux is "/dev/alarm "), the AlarmManagerServie Service keeps listening to the alarm device. Once an alarm is triggered or an alarm event occurs, the AlarmManagerServie service will traverse the alarm list to find the corresponding registration alarm and send a broadcast. When the system starts, the service program is started by the System Service Program system_service and the system initializes the alarm device (/dev/alarm ). Of course, in the JAVA layer, AlarmManagerService and Linux
There is also a layer of encapsulation between the Alarm driver interfaces, that is, JNI.
AlarmManager separates applications from services, so that application developers do not need to care about specific services, but use them directly through AlarmManager. This may be the benefit of the customer/service model. AlarmManager and AlarmManagerServie communicate with each other through the Binder, which is a multi-to-one relationship.
In the android system, AlarmManage provides three interfaces and five types of alarm services.
Three interfaces:
View plaincopy to clipboardprint?
- // Cancel the registered alarm that matches the Parameter
- Void cancel (PendingIntent operation)
- // Register a new alarm.
- Void set (int type, long triggerAtTime, PendingIntent operation)
- // Register an alarm for repeated types
- Void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
- // Set the time zone
- Void setTimeZone (String timeZone)
Java code
- // Cancel the registered alarm that matches the Parameter
- Void cancel (PendingIntent operation)
- // Register a new alarm.
- Void set (int type, long triggerAtTime, PendingIntent operation)
- // Register an alarm for repeated types
- Void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
- // Set the time zone
- Void setTimeZone (String timeZone)
// Cancel the registered alert policy <br/> void cancel (PendingIntent operation) <br/> // register a new alarm <br/> void set (int type, long triggerAtTime, PendingIntent operation) <br/> // register an alert for a duplicate type <br/> void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation) <br/> // set the time zone <br/> void setTimeZone (String timeZone) <br/>
Five Alarm types
View plaincopy to clipboardprint?
- Public static final int ELAPSED_REALTIME
- // When the system is sleep, this type of alarm will not wake up the system. It is not passed until the system is wake up next time. The time used for this alarm is relative time, starting from the start of the system, including the sleep time. You can call systemclock. elapsedrealtime. The system value is 3 (0x00000003 ).
- Public static final int elapsed_realtime_wakeup
- // Wake up the system. The usage is the same as elapsed_realtime. The system value is 2 (0x00000002 ).
- Public static final int rtc
- // When the system is sleep, this type of alarm will not wake up the system. It is not passed until the system is wake up next time. The time used for this alarm is absolute time and UTC time. It can be obtained by calling system. currenttimemillis. The system value is 1 (0x00000001 ).
- Public static final int RTC_WAKEUP
- // Wake up the system. The usage is the same as that of RTC. The system value is 0 (0x00000000 ).
- Public static final int POWER_OFF_WAKEUP
- // Wake up the system. It is a Shutdown Alarm, that is, the device can also wake up the system when it is shut down, so we call it a Shutdown Alarm. The usage is the same as that of RTC. The system value is 4 (0x00000004 ).
Java code
- Public
- Static final int ELAPSED_REALTIME
- // When the system is sleep, this type of alarm will not wake up the system. It is not passed until the system is wake up next time. The time used for this alarm is relative time, which starts timing 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
- // Wake up the system. The usage is the same as ELAPSED_REALTIME. The system value is 2 (0x00000002 ).
- Public static final int RTC
- // When the system is sleep, this type of alarm will not wake up the system. It is not passed until the system is wake up next time. The time used for this alarm is the absolute time and the UTC time. You can call
- System. currentTimeMillis. The system value is 1 (0x00000001 ).
- Public static final int RTC_WAKEUP
- // Wake up the system. The usage is the same as that of RTC. The system value is 0 (0x00000000 ).
- Public static final int POWER_OFF_WAKEUP
- // Wake up the system. It is a Shutdown Alarm, that is, the device can also wake up the system when it is shut down, so we call it a Shutdown Alarm. The usage is the same as that of RTC. The system value is
- 4 (0x00000004 ).
Public <br/> static final int ELAPSED_REALTIME </p> <p> // when the system goes to sleep, this type of alarm will not wake up the system. It is not passed until the system is wake up next time. The time used for this alarm is relative time, starting from the start of the system, including the sleep time <br/>. You can call SystemClock. elapsedRealtime. The system value is 3 (0x00000003 ). <Br/> public static final int ELAPSED_REALTIME_WAKEUP <br/> // wake up the system. The usage is the same as that of ELAPSED_REALTIME. The system value is 2 (0x00000002 ). <Br/> public static final int RTC </p> <p> // when the system goes to sleep, this type of alarm will not wake up the system. It is not passed until the System is wake up next time. The time used for this alarm is absolute time and UTC time. It can be obtained by calling <br/> System. currentTimeMillis. The system value is 1 (0x00000001 ). <Br/> public static final int RTC_WAKEUP <br/> // wake up the system. The usage is the same as that of RTC. The system value is 0 (0x00000000 ). <Br/> Public static final int POWER_OFF_WAKEUP </p> <p> // wake up the system. It is a Shutdown Alarm, that is, the device can also wake up the system when it is shut down, so we call it a Shutdown Alarm. The usage is the same as that of RTC. The system value is <br/> 4 (0x00000004 ). <Br/>
Pay attention to 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 you compare the Intent to an order, pendingIntent is more like a person who places an order, because it is responsible for both sending the order and processing the order after it is sent. For example, after sending the order successfully, you must prepare the acceptance order goods, you must resend the email or cancel the order after failed sending. Developers can call getActivity (Context,
Int, Intent, int)
GetBroadcast (Context, int, Intent, int)
GetService (Context, int, Intent, int)
You can get a PendingIntent instance in three different ways.
GetBroadcast -- The PendingIntent obtained through this function will play a broadcast function, just like calling the Context. sendBroadcast () function. When the system uses it to send an intent, it uses the broadcast format, and the intent contains the corresponding intent receiving object. Of course, this object can be specified when PendingIntent is created, you can also use descriptions such as ACTION and CATEGORY to allow the system to automatically find the behavior processing object.
View plaincopy to clipboardprint?
- 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 );
Intent intent = new Intent (AlarmController. this, OneShotAlarm. class); <br/> PendingIntent sender = PendingIntent. getBroadcast (AlarmController. this, 0, intent, 0 );
GetActivity -- The PendingIntent obtained through this function can directly start a new activity, just like calling Context. startActivity (Intent) is the same. however, it is worth noting that this new Activity is no longer an existing Activity of the current process. Intent. FLAG_ACTIVITY_NEW_TASK must be used in Intent.
View plaincopy to clipboardprint?
- // The PendingIntent to launch our 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 our activity if the user selects this notification
- PendingIntent contentIntent = PendingIntent. getActivity (this, 0, new Intent (this, AlarmService. class), 0 );
// The PendingIntent to launch our activity if the user selects this notification <br/> PendingIntent contentIntent = PendingIntent. getActivity (this, 0, new Intent (this, AlarmService. class), 0 );
GetService -- The PengdingIntent obtained through this function can directly start a new Service, just like calling Context. startService.
View plaincopy to clipboardprint?
- // Create an IntentSender that will launch our service, to be scheduled
- // With the alarm manager.
- MAlarmSender = PendingIntent. getService (AlarmService. this,
- 0, new intent (alarmservice. This, alarmservice_service.class), 0 );