Event handling for alarm alarm in Android

Source: Internet
Author: User

The previous blog post has been constantly sharing phone-related knowledge is also considered a knowledge note, but in the work will inevitably encounter some of the problems of other modules, so in solving these problems, it is convenient to record and share this knowledge. Some knowledge is difficult to understand when it is not known, and it is easy to find out. Part of the knowledge is also time-sensitive, such as the change of Android version, phone architecture changes and so on, so I hope that their notes can help some children's shoes, which is enough.

Reprint Please be sure to indicate the source: Http://blog.csdn.net/yihongyuelan

What if an alarm goes off in Android and the app needs to do something about it? First we need to receive the event and then consider whether to close (stop) or nap (snooze). The following is described in code Packages/apps/deskclock/src/com/android/deskclock/alarms/alarmservice.java:

+/A public action send by Alarmservice when the alarm have started.36 public static final String Alarm_alert_acti on = "Com.android.deskclock.ALARM_ALERT", 3738//A public action sent by Alarmservice when the ALARM have stopped for an Y reason.39 public static final String alarm_done_action = "Com.android.deskclock.ALARM_DONE";

By viewing the annotations, you know that the system provides two action for broadcast alarm events, including: Com.android.deskclock.ALARM_ALERT and Com.android.deskclock.ALARM_DONE, That is, when the alarm goes off, the Com.android.deskclock.ALARM_ALERT is triggered, and the Com.android.deskclock.ALARM_DONE is triggered when the alarm stops or naps. Just say you need to listen to a broadcast event and register a broadcast listener:

Intentfilter filter = new Intentfilter (); Filter.addaction ("Com.android.deskclock.ALARM_ALERT"); Filter.addaction (" Com.android.deskclock.ALARM_DONE "); Registerreceiver (mreceiver, filter);
The mreceiver here is the object of Broadcastreceiver, which handles events in OnReceive. When the alarm goes off, the interface that pops up is the Alarmactivity.java path located at: packages/apps/deskclock/src/com/android/deskclock/alarms/ Alarmactivity.java, which provides two important alarm operator interfaces in this class, stop and snooze, can be seen in the following description:
    Alarmactivity listens for this broadcast intent, so and other APPLICATIONS50    //Can snooze the alarm (after Alarm_alert_action and before alarm_done_action). Public    static final String alarm_snooze_action = " Com.android.deskclock.ALARM_SNOOZE "; 5253    //alarmactivity listens for this broadcast intent APPLICATIONS54    //can dismiss the alarm (after Alarm_alert_action and before Alarm_done_action).    Final String alarm_dismiss_action = "Com.android.deskclock.ALARM_DISMISS";
The method provides two broadcast received interfaces, respectively Com.android.deskclock.ALARM_SNOOZE and Com.android.deskclock.ALARM_DISMISS, which are used to snooze the latter for stop. You can see the handling of the action in Alarmactivity as follows:
184 Intentfilter filter = new Intentfilter (alarmservice.alarm_done_action); 185 Filter.addaction (ALARM_SNOOZE _action); 186 filter.addaction (alarm_dismiss_action); 187 Registerreceiver (mreceiver, filter); 118 Private B Roadcastreceiver mreceiver = new Broadcastreceiver () {119 @Override120 public void OnReceive (context context , Intent Intent) {121 String action = intent.getaction (); 122 log.v ("Alarmactivity-broadcast receiv  ER-"+ action", 123 if (Action.equals (alarm_snooze_action)) {124 SNOOZE (); if (Action.equals (alarm_dismiss_action)) {126 DISMISS (); 127} else if (Action.equals (Alarmservi Ce. alarm_done_action) {129} else {LOG.I ("Unknown broadcast in Ala Rmactivity: "+ action"; 131}132}133};
This means that you can control the stop and snooze of the alarms in your code by using the broadcast method:
private void Stopalarm () {    Intent Intent = new Intent ();    Intent.setaction ("Com.android.deskclock.ALARM_DISMISS");    Sendbroadcast (intent);}    private void Snoozealarm () {    Intent Intent = new Intent ();    Intent.setaction ("Com.android.deskclock.ALARM_SNOOZE");    Sendbroadcast (intent);}
Finally, the demo is attached, and when the alarm goes off, it is received and Stop/snooze, the code is as follows:
    private void Stopalarm () {        Intent Intent = new Intent ();      &NBS P Intent.setaction ("Com.android.deskclock.ALARM_DISMISS");        Sendbroadcast (Intent);   }        private void Snoozealarm () {        Intent Intent = new Inten T ();        intent.setaction ("Com.android.deskclock.ALARM_SNOOZE");        Sendbroadcast (Intent);   }    Handler mhandler = new Handler () {        @Override & nbsp       public void Handlemessage (Message msg) {            switch (msg.what) {                 case 0:                &NB Sp   LOG.I ("Seven", "Stop Alarm");                    stopalarm (); &nbs P       &NBSp           break;                Case 1:    &NBS P               LOG.I ("Seven", "snooze Alarm");                    Snoozealarm ();                    BREAK;&NBS P               default:                  &N Bsp break;           }       }   };      &N Bsp  private void Registeralarmreceiver () {        mreceiver = new Myreceiver ();        Intentfilter filter = new Intentfilter ();        filter.addaction ("Com.android.deskclock.ALARM_ ALERT ");        filter.addaction (" Com.android.deskclock.ALARM_DONE ");        RegisterreceiveR (mreceiver, filter);   }    class Myreceiver extends Broadcastreceiver {        @Override         public void onreceive (context context, Intent Intent) {        &N Bsp   if (Intent.getaction () equals ("Com.android.deskclock.ALARM_ALERT")) {            & nbsp   LOG.I ("Seven", "Alarm_alert");                Mhandler.sendemptymessagedelayed (0,15); Stop alarm               //mhandler.sendemptymessagedelayed (1,15); Snooze alarm           }else if (intent.getaction (). Equals (" Com.android.deskclock.ALARM_DONE ")) {                LOG.I (" Seven "," Alarm_done " );           }       }   }
There may be people here who are wondering why not calling Stopalarm and Snoozealarm directly but using Handler's Sendemptymessagedelayed method. If the direct use of the Stopalarm and Snoozealarm methods will have no effect, the minimum need to delay 15ms to be useful.

Finally add some English translation, so that foreign children's shoes can also get some help.

How to stop a alarm in Android

How to turn off the alarm clock on the droid

How to cancel the android alarm

Android Alarm not turning off programmatically

Automatically silence the sound volume of Android Phone programmatically

Event handling for alarm alarm in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.