1, Alarmmanager, as the name implies, is a "reminder", is commonly used in Android system-level prompt service, can be achieved from a specified time, to a fixed interval of time to perform an operation, so often with the broadcast (broadcast), to implement alarm clock and other functions
2, there are three common methods of Alarmmanager:
(1) Set (int type,long starttime,pendingintent pi);
This method is used to set a one-time alarm, the first parameter represents the alarm type, the second parameter indicates the alarm execution times, and the third parameter represents the alarm response action.
(2) setrepeating (int type,long starttime,long intervaltime,pendingintent pi);
This method is used to set a repeating alarm, the first parameter represents the alarm type, the second parameter represents the first time the alarm is executed, the third parameter represents the interval between two executions of the alarm, and the third parameter represents the alarm response action.
(3) setinexactrepeating (int type,long starttime,long intervaltime,pendingintent pi);
This method is also used to set a repeating alarm, similar to the second method, although the interval between the two alarms is not fixed.
3, three methods of each parameter keyholder:
(1) INT type: Alarm types, commonly used with 5 values: Alarmmanager.elapsed_realtime, Alarmmanager.elapsed_realtime_wakeup, ALARMMANAGER.RTC, Alarmmanager.rtc_wakeup, Alarmmanager.power_off_wakeup.
Alarmmanager.elapsed_realtime means that the alarm clock is not available when the phone is asleep, and the alarm is used relative to the time (starting with the system boot) and the state value is 3;
Alarmmanager.elapsed_realtime_wakeup indicates that the alarm will wake up the system and perform the prompt function, the alarm clock also uses relative time, the status value is 2;
ALARMMANAGER.RTC indicates that the alarm is not available in sleep, and the alarm is used in absolute time, that is, the current system time, the status value is 1;
Alarmmanager.rtc_wakeup means that the alarm will wake up the system and perform the prompt function when the alarm is in the state of sleep, which has an absolute time and a state value of 0.
Alarmmanager.power_off_wakeup means that the alarm clock in the state of the hand machine can also be a normal prompt function, so it is one of the most used in 5 states, the alarm is also used absolute time, the status value of 4; But this state seems to be affected by the SDK version, Some editions are not supported;
(2) long startTime: The first time the alarm is executed, in milliseconds, the time can be customized, but the current time is generally used. It is important to note that this property is closely related to the first property (type), and if the alarm corresponding to the first parameter uses a relative time (Elapsed_realtime and Elapsed_realtime_wakeup), Then this property will have to use relative time (relative to the system boot time), such as the current time is expressed as: Systemclock.elapsedrealtime (); If the first parameter corresponds to an alarm with an absolute time (RTC, Rtc_wakeup, Power_ Off_wakeup), then this property will have to use absolute time, such as the current time is expressed as: System.currenttimemillis ().
(3) long intervaltime: For the latter two methods, this property exists, indicating the interval between two alarm executions, and in milliseconds.
(4) pendingintent pi: Is the operation of the alarm clock, such as sending a broadcast, give hints and so on. Pendingintent is the encapsulation class for intent. It should be noted that if the alarm is implemented by starting the service, the Pendingintent object should be acquired using the Pending.getservice (Context c,int i,intent intent,int j) method If the alarm is realized by broadcasting, the Pendingintent object should be acquired using Pendingintent.getbroadcast (Context c,int i,intent intent,int j) method. If the alarm is implemented in an activity way, the Pendingintent object should be acquired using the pendingintent.getactivity (Context c,int i,intent intent,int j) method. If these three methods are wrong, although not error, but do not see the alarm effect.
The use mechanism of Alarmmanager is called the global timer, and some call it an alarm clock. Through the use of it, the personal feel called the global timer is more appropriate, in fact, its role and timer a bit similar. There are two similar usages: (1) Perform an action after a specified length of time (2) perform an operation periodically
Alarmmanager objects are used in conjunction with intent, you can start an activity on a timed basis, send a broadcast, or open a service.
The following code describes the use of two timing methods in detail:
(1) Perform an action after a specified length of time
//action: Send a broadcast, broadcast received after the toast prompt timed operation completedIntent Intent =NewIntent (Main. This, Alarmreceiver.class); Intent.setaction (" Short"); Pendingintent Sender=pendingintent.getbroadcast (Main. This,0, Intent,0);//set a time after five seconds.Calendar calendar=calendar.getinstance (); Calendar.settimeinmillis (System.currenttimemillis ()); Calendar.add (Calendar.SECOND, 5); Alarmmanager Alarm=(Alarmmanager) Getsystemservice (alarm_service); ALARM.Set(Alarmmanager.rtc_wakeup, Calendar.gettimeinmillis (), sender);//or simplify it in the following way//Alarm.set (Alarmmanager.rtc_wakeup, System.currenttimemillis () +5*1000, sender);
//Note: Receiver remembers registering in Manifest.xml Public Static classAlarmreceiver extends broadcastreceiver{@Override Public voidOnReceive (Context context, Intent Intent) {//TODO auto-generated Method Stubif(Intent.getaction (). Equals (" Short") {toast.maketext (context,"Short Alarm", Toast.length_long). Show ();}Else{toast.maketext (context,"Repeating Alarm", Toast.length_long). Show (); } }}
The Alarmmanager types are as follows:
ALARMMANAGER.RTC, the hardware alarm clock, does not wake up the phone (or other devices) hibernate, when the phone does not fire alarm when hibernating.
Alarmmanager.rtc_wakeup, hardware alarm clock, when the alarm is launched to wake up the phone hibernation;
Alarmmanager.elapsed_realtime, the real time passes the alarm clock, does not wake the phone to sleep, when the phone sleeps, does not emit the alarm clock.
Alarmmanager.elapsed_realtime_wakeup, the real time passes the alarm clock, when the alarm clock launches wakes up the phone to sleep;
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.
(2) Periodic execution of an operation
Intent Intent =NewIntent (Main. This, Alarmreceiver.class); Intent.setaction ("repeating"); Pendingintent Sender=pendingintent.getbroadcast (Main. This,0, Intent,0);//Start TimeLongFirstime=Systemclock.elapsedrealtime (); Alarmmanager am=(Alarmmanager) Getsystemservice (alarm_service);//5 seconds, one cycle, no stop sending broadcasts.Am.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firstime,5* +, sender);
Alarmmanager's setrepeating () is equivalent to a timer's schedule (task,delay,peroid); a bit of a difference when the timer this method is to specify how long the delay starts after the periodic execution of the task;
Alarmmanager Cancellation: (It is important to note that the canceled intent must be in absolute agreement with the startup intent to support the cancellation Alarmmanager)
Intent Intent =new Intent (Main. this, alarmreceiver. class ); Intent.setaction ("repeating"); Pendingintent Sender=pendingintent.getbroadcast (Main. This 0 0 ); Alarmmanager Alarm=(Alarmmanager) Getsystemservice (Alarm_service); Alarm.cancel (sender);
Android Alarmmanager (Global Timer/alarm) Specify the length of time or perform an action in a periodic form