Android Alarm Clock Implementation mechanism is very simple, only need to call the Alarmmanager.set () method to submit the alarm settings to the system, when the alarm time, the system will send the specified broadcast message according to our settings, we write a broadcast to receive messages to do the corresponding processing.
The first step is to set the alarm:
<span Style= "Color:rgb (51, 51, 51); font-family: ' Microsoft yahei ', Tahoma, Helvetica, SimSun, Sans-serif; font-size:14px; line-height:21px; ">private static String my_alarm_action =" android.alarm.test.action "; </span>
<span style= "White-space:pre" ></span>/** * Set Alarm * @param context * @param firsttriggertimemillis First trigger time (per millisecond) * @param periodtimemillis interval */private void Setalarmtime (context context, long firsttriggertimemillis, long Perio Dtimemillis) {Alarmmanager alarm = (Alarmmanager) context.getsystemservice (context.alarm_service); Intent Intent = new Intent (my_alarm_action); Pendingintent Sender = pendingintent.getbroadcast (context, 0, intent, pendingintent.flag_cancel_current); Alarm.setrepeating (Alarmmanager.rtc_wakeup, Firsttriggertimemillis, Periodtimemillis, sender);}
The second step is to write an interface to receive event broadcasts:
/** * Alarm Clock receive * @author Antony * @date 2014-7-23 */public static class Myalarmreceiver extends Broadcastreceiver {public void OnReceive (Context context, Intent Intent) { if (my_alarm_action.equals (Intent.getaction ())) { LOGCAT.E (" Receive an alarm message once! ");
<span style= "White-space:pre" ></span>//todo write the relevant handling return here;}}
Of course receiver is required to be registered in the Mainfest.xml, my registration information is as follows:
<receiver android:name= "Com.test.fee.mytest$myalarmreceiver" > <intent-filter> <action Android:name= "Android.alarm.zywl.action"/> </intent-filter> </receiver>
Careful may find my Myalarmreceiver class is with static keyword, and I registered the broadcast is added "$" symbol. The reason is Jiangzi, the receiver is written as MyTest's inner class for the code not to look so messy. If Broadcastreceiver is written as an inner class in manifest, it cannot be registered directly in the form as follows:
<receiver android:name= "Com.test.fee.MyAlarmReceiver" > <intent-filter> <action android: Name= "Android.alarm.zywl.action"/> </intent-filter> </receiver>
This throws an error message that cannot be found for the Myalarmreceiver class. That is to say Broadcastreceiver want to write as an inner class to add the static keyword, and in the registered path to add the $ symbol to identify clearly the inner class attribution.
The third step is to write a boot broadcast receiver:
The notation is similar to Myalarmreceiver, and can be used as an inner class or as a separate class to write. Call the set alarm method in the boot broadcast receiver.
This way, each time the phone restarts and the alarm is reset, the alarm will not be interrupted. Of course, I'm setting up an alarm that repeats at regular intervals, depending on the requirements.
The principle of the alarm clock is all these, as for the specific interface display and prompt way, everyone has different ideas, no longer say.
Use of Android Alarmmanager