之前的博文一直在持续分享Phone相关的知识也算是知识笔记,但在工作中难免遇到其他模块的一些问题,因此在解决这些问题的时候顺手将这些知识记录并分享出来。有些知识在不知道的时候会觉得非常难,当了解之后便会觉得非常easy。同一时候部分知识也是具有时效性的,比方随着Android版本号的更迭。Phone的架构变化等等,因此希望自己的笔记可以帮助到一些童鞋。这样就足够了。
转载请务必注明出处:http://blog.csdn.net/yihongyuelan
Android中假设闹钟响起时。而应用须要对此做一些处理应该怎么办呢?首先我们须要接收到该事件。之后再考虑是关闭(stop)还是小睡(snooze)。
在代码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";
37
38 // 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";
通过查看凝视能够知道,系统提供了两个Action用于广播闹钟事件,包含:com.android.deskclock.ALARM_ALERT和com.android.deskclock.ALARM_DONE,即当闹钟响起时触发com.android.deskclock.ALARM_ALERT,而当闹钟停止或者小睡时触发com.android.deskclock.ALARM_DONE。也就说须要监听广播事件就注冊一个广播监听器:
IntentFilter filter = new IntentFilter();
filter.addAction("com.android.deskclock.ALARM_ALERT");
filter.addAction("com.android.deskclock.ALARM_DONE");
registerReceiver(mReceiver, filter);
这里的mReceiver是BroadCastReceiver的对象,在onReceive中处理事件。当闹钟响起时,弹出的界面是AlarmActivity.java路径位于:packages/apps/DeskClock/src/com/android/deskclock/alarms/AlarmActivity.java。在该类中提供了两个重要的闹钟操作接口,即stop和snooze,能够看到例如以下描写叙述:
49 // AlarmActivity listens for this broadcast intent, so that other applications
50 // 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";
52
53 // AlarmActivity listens for this broadcast intent, so that other applications
54 // 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";
该方法提供了两个广播接收的接口各自是com.android.deskclock.ALARM_SNOOZE和com.android.deskclock.ALARM_DISMISS,前者用于snooze后者用于stop。在AlarmActivity中能够看到对该action的处理,例如以下:
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 @Override
120 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 };
也就是说能够在代码中通过使用广播的方式来控制闹钟的stop和snooze:
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);
}
最后附上Demo。当闹钟响起时接收并将其Stop/Snooze,代码例如以下:
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");
}
}
}
这里可能有的人会感觉奇怪,为什么不直接调用stopAlarm和snoozeAlarm而是使用handler的sendEmptyMessageDelayed方法。
假设直接使用stopAlarm和snoozeAlarm方法会没有效果。最少须要延迟15ms才会有作用。
最后加上一些英文的翻译,以便国外的童鞋也能获得一些帮助。
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