Three main steps: 1. Set the alarm time; 2. Receive alarm event broadcast; 3. Re-calculate and set the alarm time after reboot; 1. Set the alarm time (in milliseconds)
Private voidSetalarmtime (Context context,LongTimeinmillis) {Alarmmanager am=(Alarmmanager) Context.getsystemservice (Context.alarm_service); Intent Intent=NewIntent ("android.alarm.demo.action"); Pendingintent Sender=pendingintent.getbroadcast (Context,0, intent, pendingintent.flag_cancel_current); intInterval = -* +;//Alarm interval, set to 1 minutes to make a noise, in the 2nd step we will receive a broadcast every 1 minutesam.setrepeating (Alarmmanager.rtc_wakeup, Timeinmillis, Interval, sender)}
2. Receive Alarm event broadcasts
Public class Alarmreceiver extends Broadcastreceiver { publicvoid onreceive (context context, Intent Intent) { if ("Android.alarm.demo.action". Equals (Intent.getaction ())) { // The alarm time is set in the 1th step, where you can eject the alarm prompt and play the bell // you can continue to set the next alarm time; return ; } }}
Of course, receiver is required to be registered in the Manifest.xml:
<receiver android:name="alarmreceiver"> <intent-filter> < Action android:name="android.alarm.demo.action" /> </intent-filter></ Receiver>
3. Re-calculate and set the alarm time after reboot of course there is a bootreceiver:
Public class Bootreceiver extends Broadcastreceiver { publicvoid onreceive (context context, Intent Intent) { = intent.getaction (); if (Action.equals (intent.action_boot_completed)) { // recalculate alarm time, and adjust the first step to set the alarm time and alarm interval } }}
Of course, you also need to register:
<receiver android:name="bootreceiver"> <intent-filter> < Action android:name="android.intent.action.BOOT_COMPLETED" /> </ Intent-filter></receiver>
The principle of the alarm clock is actually so much, as to specific details such as alarm time storage and calculation, interface display and alarm prompt way, everyone's ideas will be different
Android Setup Alarm steps and Base code