Android Tv开发遥控器按键长按处理

在源码的PhoneWindowManager.java 窗口管理类中interceptKeyBeforeDispatching方法中, 该方法是按键事件拦截的方法, 返回-1表示拦截, 按键事件就不会往后传递
直接上关键代码

 private boolean mFocusConsumed = false; //长按标志位
    /** {@inheritDoc} */
    @Override
    public long interceptKeyBeforeDispatching(WindowState win, KeyEvent event, int policyFlags) {
        final boolean keyguardOn = keyguardOn();
        final int keyCode = event.getKeyCode();
        final int repeatCount = event.getRepeatCount();
        final int metaState = event.getMetaState();
        final int flags = event.getFlags();
        final boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
        final boolean canceled = event.isCanceled();

        if (DEBUG_INPUT) {
            Log.d(TAG, "interceptKeyTi keyCode=" + keyCode + " down=" + down + " repeatCount="
                    + repeatCount + " keyguardOn=" + keyguardOn + " mHomePressed=" + mHomePressed
                    + " canceled=" + canceled);
        }
        .....
        //实现代码
        } else if (keyCode == KeyEvent.KEYCODE_3D_MODE) {
            if (!down) {   
                    if(mFocusConsumed) { 
                        mFocusConsumed = false;
                        return -1;
                    }else{
                        //按键短按, 在这里进行做你要做的事情, 打开Acitivity,广播,开启服务等
                        return -1;
                    }             
            } else {
                    if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) {   
                        if (!keyguardOn) {
                            Log.d(TAG,"xxxxxxxxxxx autofocus  111");
                            mFocusConsumed = true;
                           //按键长按,在这里进行做你要做的事情
                        }
                        return -1;
                    }
            }
        }

FLAG_LONG_PRESS长按事件tag

你可能感兴趣的:(Android Tv开发遥控器按键长按处理)