Event handling for Alarm clock Alarm in Android
The previous blog posts have been constantly sharing Phone-related knowledge, which is also a knowledge note. However, it is inevitable that you will encounter problems in other modules during your work, therefore, when solving these problems, we can easily record and share these knowledge. Some knowledge is hard to understand, and it is easy to understand. At the same time, some knowledge is time-sensitive, such as the change of Android versions and the change of Phone architecture. Therefore, it is enough to hope that your notes can help some children's shoes.
Reprint please be sure to indicate the source: http://blog.csdn.net/yihongyuelan
In Android, what should I do if the application needs to do something about it when the alarm is triggered? First, we need to receive the event, and then consider whether to stop or snooze it ). The following descriptions are available in the Code packages/apps/DeskClock/src/com/android/deskclock/alarms/AlarmService. java:
35 // A public action send by AlarmService when the alarm has started.36 public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";3738 // A public action sent by AlarmService when the alarm has stopped for any reason.39 public static final String ALARM_DONE_ACTION = "com.android.deskclock.ALARM_DONE";
You can see from the comments that the system provides two actions to broadcast alarm events, including: com. android. clock. ALARM_ALERT and com. android. clock. ALARM_DONE, that is, the com. android. clock. ALARM_ALERT, which triggers com. android. clock. ALARM_DONE. That is to say, You need to register a broadcast listener to listen to broadcast events:
IntentFilter filter = new IntentFilter();filter.addAction("com.android.deskclock.ALARM_ALERT");filter.addAction("com.android.deskclock.ALARM_DONE");registerReceiver(mReceiver, filter);
Here the mReceiver is the BroadCastReceiver object, which processes the event in onReceive. When the alarm is triggered, AlarmActivity is displayed. the java path is packages/apps/javasclock/src/com/android/javasclock/alarms/AlarmActivity. java, which provides two important alarm operation interfaces: stop and snooze:
49 // AlarmActivity listens for this broadcast intent, so that other applications50 // can snooze the alarm (after ALARM_ALERT_ACTION and before ALARM_DONE_ACTION).51 public static final String ALARM_SNOOZE_ACTION = "com.android.deskclock.ALARM_SNOOZE";5253 // AlarmActivity listens for this broadcast intent, so that other applications54 // can dismiss the alarm (after ALARM_ALERT_ACTION and before ALARM_DONE_ACTION).55 public static final String ALARM_DISMISS_ACTION = "com.android.deskclock.ALARM_DISMISS";
This method provides two broadcast receiving interfaces: com. android. javasclock. ALARM_SNOOZE and com. android. javasclock. ALARM_DISMISS. The former is used for snooze and the latter is used for stop. You can see the processing of this 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 BroadcastReceiver mReceiver = new BroadcastReceiver() {119 @Override120 public void onReceive(Context context, Intent intent) {121 String action = intent.getAction();122 Log.v("AlarmActivity - Broadcast Receiver - " + action);123 if (action.equals(ALARM_SNOOZE_ACTION)) {124 snooze();125 } else if (action.equals(ALARM_DISMISS_ACTION)) {126 dismiss();127 } else if (action.equals(AlarmService.ALARM_DONE_ACTION)) {128 finish();129 } else {130 Log.i("Unknown broadcast in AlarmActivity: " + action);131 }132 }133 };
That is to say, you can use the broadcast method in the code to control the stop and snooze of the alarm:
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);}
The Demo is attached. when the alarm is triggered, it is received and stopped/Snooze. The Code is as follows:
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); } Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: Log.i("Seven", "stop alarm"); stopAlarm(); break; case 1: Log.i("Seven", "snooze alarm"); snoozeAlarm(); break; default: break; } } }; 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) { if (intent.getAction().equals("com.android.deskclock.ALARM_ALERT")) { 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"); } } }
Some may be wondering why the sendEmptyMessageDelayed method of handler is used instead of directly calling stopAlarm and snoozeAlarm. If you use the stopAlarm and snoozeAlarm methods directly, it will have no effect. A delay of at least 15 ms will be required.
Add some English translations so that foreign children's shoes can also get some help.
How to stop an 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