1. 程式人生 > >Android Tv開發遙控器按鍵長按處理

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