android frameworks的按键监听

做应用程序的时候,如果需要监听什么按钮,则只需要添加
对应的监听器,而在, 然后再类似于onClick()的方法中实现真正的处理。


但在针对整个系统级别的按钮监听呢,之前在解决一个硬件按钮声效的bug的时候对这部分的源代码了查看了下,所以记下哈


首先是KeyEvent这个类

每个按钮的点击,都是通过一系列的按键事件组成的,文档描述如下
Object used to report key and button events.

Each key press is described by a sequence of key events. A key press starts with a key event with  ACTION_DOWN . If the key is held sufficiently long that it repeats, then the initial down is followed additional key events with  ACTION_DOWN  and a non-zero value for  getRepeatCount() . The last key event is a  ACTION_UP  for the key up. If the key press is canceled, the key up event will have the  FLAG_CANCELED  flag set.

Key events are generally accompanied by a key code ( getKeyCode() ), scan code ( getScanCode() ) and meta state ( getMetaState() ). Key code constants are defined in this class. Scan code constants are raw device-specific codes obtained from the OS and so are not generally meaningful to applications unless interpreted using the  KeyCharacterMap . Meta states describe the pressed state of key modifiers such as  META_SHIFT_ON  or  META_ALT_ON .

即由ACTION_DOWN开始了一个按键的点击事件,然后根据持续的事件,接着会有一个非0的值来表示按键被持续hold的时间。当我们放开手之后,就是一个ACTION_UP的key up事件。此外KeyEvent还一定了一系列的KEYCODE. 常见的有
    public static final int KEYCODE_HOME            = 3;

    /** Key code constant: Back key. */

    public static final int KEYCODE_BACK            = 4;

    /** Key code constant: Call key. */

    public static final int KEYCODE_CALL            = 5;


像KEYCODE_HOME的说明  * This key is handled by the framework and is never delivered to applications. */ 

主页的按钮是由framework来处理的,而在应用程序中是不会接收到这个按钮的点击事件。那么framework中又是在那里对按键进行处理的呢?

在framework的phonewindow中有如下一段代码和注释
/**
     * A key was pressed down and not handled by anything else in the window.
     *
     * @see #onKeyUp
     * @see android.view.KeyEvent
     */
    
protected boolean onKeyDown(int featureId, int keyCode, KeyEvent event) {
        /* ****************************************************************************

         * HOW TO DECIDE WHERE YOUR KEY HANDLING GOES.

         *  如何确定自定义的按键处理的代码应该放在哪里呢?

         * If your key handling must happen before the app gets a crack at the event,

         * it goes in PhoneWindowManager.

         *如果处理必须在APP获取事件之前,需要在PhoneWindowManager中

         * If your key handling should happen in all windows, and does not depend on

         * the state of the current application, other than that the current

         * application can override the behavior by handling the event itself, it

         * should go in PhoneFallbackEventHandler.

         * 如果对按钮的处理需要在所有的窗口中都生效,而不是可以被当前的app覆盖从而进行处理的,需要 PhoneFallbackEventHandler,,

         * Only if your handling depends on the window, and the fact that it has

         * a DecorView, should it go here. 如果按钮的处理与当前的窗口有关,那么就是放在phonewindow中
         * ****************************************************************************/
         final KeyEvent.DispatcherState dispatcher =
                mDecor != null ? mDecor.getKeyDispatcherState() : null;

        //Log.i(TAG, "Key down: repeat=" + event.getRepeatCount()
        //        + " flags=0x" + Integer.toHexString(event.getFlags()));
         switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_MUTE: {

                // Similar code is in PhoneFallbackEventHandler in case the window
                // doesn't have one of these.  In this case, we execute it here and
                // eat the event instead, because we have mVolumeControlStreamType
                // and they don't.
                 getAudioManager().handleKeyDown(event, mVolumeControlStreamType);
                return true;

             }
            case KeyEvent.KEYCODE_MENU: {
                onKeyDownPanel((featureId < 0) ? FEATURE_OPTIONS_PANEL : featureId, event);
                return true;
            }
            case KeyEvent.KEYCODE_BACK: {
                if (event.getRepeatCount() > 0) break;
                if (featureId < 0) break;
                // Currently don't do anything with long press.
                if (dispatcher != null) {
                    dispatcher.startTracking(event, this);
                }
                return true;
            }
        }
        return false;
    }


所以,例如对于返回按钮的处理,上层的app可以获取callback的按钮事件,从而来处理返回按钮。例如关闭当前对话框等操作。因此对于callback按钮事件的处理就放在了phonewindow中。菜单按钮也是同样地道理。

那么像home键这样的不希望app来处理的系统级别的按钮呢?按照注释中的说明,对home键的处理应该在PhoneWindowManager。于是在PhoneWindowManager找到如下代码:



        // First we always handle the home key here, so applications

        // can never break it, although if keyguard is on, we do let

        // it handle it, because that gives us the correct 5 second

        // timeout.

        if (keyCode == KeyEvent.KEYCODE_HOME) {

                。。。。。
        }


所以,要添加系统级别的按钮处理,例如我的这个任务,添加按钮声效,或者按钮震动反馈的时候就是在这里啦。~~~




目前处理了一些bug,在找问题的过程中,学到了很多,看源代码的感觉还不错~~~。很多时候,找了两三天,最后的修改也许不过是一句短短的代码,但期间看到的,学到的东西才是最珍贵的。要继续努力,阶段性总结,才有进步啊,继续燃烧吧,骚年
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目标检测(Object Detection)是计算机视觉领域的一个核心问题,其主要任务是找出图像中所有感兴趣的目标(物体),并确定它们的类别和位置。以下是对目标检测的详细阐述: 一、基本概念 目标检测的任务是解决“在哪里?是什么?”的问题,即定位出图像中目标的位置并识别出目标的类别。由于各类物体具有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具挑战性的任务之一。 二、核心问题 目标检测涉及以下几个核心问题: 分类问题:判断图像中的目标属于哪个类别。 定位问题:确定目标在图像中的具体位置。 大小问题:目标可能具有不同的大小。 形状问题:目标可能具有不同的形状。 三、算法分类 基于深度学习的目标检测算法主要分为两大类: Two-stage算法:先进行区域生成(Region Proposal),生成有可能包含待检物体的预选框(Region Proposal),再通过卷积神经网络进行样本分类。常见的Two-stage算法包括R-CNN、Fast R-CNN、Faster R-CNN等。 One-stage算法:不用生成区域提议,直接在网络中提取特征来预测物体分类和位置。常见的One-stage算法包括YOLO系列(YOLOv1、YOLOv2、YOLOv3、YOLOv4、YOLOv5等)、SSD和RetinaNet等。 四、算法原理 以YOLO系列为例,YOLO将目标检测视为回归问题,将输入图像一次性划分为多个区域,直接在输出预测边界框和类别概率。YOLO采用卷积网络来提取特征,使用全连接来得到预测值。其网络结构通常包含多个卷积和全连接,通过卷积提取图像特征,通过全连接输出预测结果。 五、应用领域 目标检测技术已经广泛应用于各个领域,为人们的生活带来了极大的便利。以下是一些主要的应用领域: 安全监控:在商场、银行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值