What is Alarmmanager? Alarmmanager is a system-level cue service commonly used in Android that broadcasts a specified intent for us at specific times. The simple thing is that we set a time, and then at that time, Alarmmanager broadcast a intent we set for us, usually we use pendingintent,pendingintent can be understood as intent package, The simple thing is to add a specified action on the intent. When using intent, we also need to perform startactivity, StartService, or sendbroadcast to make intent useful. And Pendingintent's words are to include this action inside.
define a Pendingintent objectpendingintent pi = pendingintent.getbroadcast (this,0,intent,0);
Common APIs for Alarmmanager1, 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.
Detailed parameters:int typeThe type of alarm clock, commonly used has 5 values: Alarmmanager.elapsed_realtime, Alarmmanager.elapsed_realtime_wakeup, ALARMMANAGER.RTC, Alarmmanager.rtc_wakeup, Alarmmanager.power_off_wakeup.
Larmmanager.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;
Long StartTimeThe 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_r ealtime_wakeup), then this property will have to use relative time (relative to System start-up time), such as the current time is expressed as: Systemclock.elapsedrealtime (); If the first parameter corresponds to an alarm that uses 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 ().
Long IntervalTimeFor the latter two methods, this property exists, indicating the interval between two alarm executions, and in milliseconds.
pendingintent Pi
An alarm is bound to perform actions such as sending a broadcast, giving 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.
Write a simple demo
public class Mainactivity extends Activity { @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Create the Intent object with the action Elitor_clock, with the additional information string "You should soy sauce" Intent Intent = new Intent ("Elitor_clock"); Intent.putextra ("msg", "You should soy sauce"); Defines a Pendingintent object, Pendingintent.getbroadcast contains the action of Sendbroadcast. //That is, send the action as "Elitor_clock" intent pendingintent pi = pendingintent.getbroadcast (this, 0, intent, 0); Alarmmanager object, note that this is not a new object, Alarmmanager is a system-level service alarmmanager am = (alarmmanager) getsystemservice (alarm_ SERVICE); Set the alarm to start at the current time, Pendingintent object Pi Every 5s, note the relationship of the first parameter to the second parameter //5 seconds after sending the broadcast via the Pendingintent Pi object Am.setrepeating (Alarmmanager.rtc_wakeup, System.currenttimemillis (), 5 *, pi);} }
Broadcast recipient: Myreceiver.java
public class Myreceiver extends Broadcastreceiver { @Override public void OnReceive (context context, Intent Intent) { //TODO auto-generated method stub log.d ("MyTag", "Onclock .........."); String msg = Intent.getstringextra ("msg"); Toast.maketext (Context, MSG, toast.length_short). Show (); }}
The manifest file is as follows: Androidmainfest.xml
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.alarmdemo "> <application android:allowbackup=" true "android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <act Ivity android:name= ". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER "/> </intent-filter> </activity> <receiver android:name=". Myreceiver "> <intent-filter> <action android:name=" Elitor_clock "/> &L T;/intent-filter> </receiver> </application></manifest>
Operation Result:
Download Demo please poke
Android Alarmmanager Alarm Clock implementation