Android中对闹钟Alarm的事件处理

时间:2021-06-12 16:48:02

        之前的博文一直在持续分享Phone相关的知识也算是知识笔记,但在工作中难免遇到其它模块的一些问题,因此在解决这些问题的时候顺手将这些知识记录并分享出来。有些知识在不知道的时候会认为很难,当了解之后便会觉得很容易。同时部分知识也是具有时效性的,比如随着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 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";
        该方法提供了两个广播接收的接口分别是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        @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    };
        也就是说可以在代码中通过使用广播的方式来控制闹钟的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