欢迎访问我的网站,希望内容对您有用,感兴趣的可以加入免费知识星球。

Android TV键值复用

Android 迷途小书童 4年前 (2020-01-03) 2250次浏览 0个评论

软硬件环境

  • OTT Box 5.1.1
  • Android studio 3.0.1

前言

在做Android TV开发时经常会碰到遥控器的使用,同一款盒子会同时匹配多款遥控器,而对应遥控器的按键一般是不一样的,有的按键多,有的按键少,如果不巧用的是按键少的遥控器,那键值复用就不可避免了。本文就来实现通过复用左右键来实现音量加减的功能。

实现

通过在dispatchKeyEvent方法中拦截键值,进行重新装配KeyEvent对象,达到键值复用的目的。

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int keyCode = event.getKeyCode();
    int action = event.getAction();
    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int repeat = event.getRepeatCount();
    int metaState = event.getMetaState();

    switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            event = new KeyEvent(downTime, eventTime, action, KeyEvent.KEYCODE_VOLUME_UP, repeat, metaState);
            break;

        case KeyEvent.KEYCODE_DPAD_LEFT:
            event = new KeyEvent(downTime, eventTime, action, KeyEvent.KEYCODE_VOLUME_DOWN, repeat, metaState);
            break;
    }

    return super.dispatchKeyEvent(event);
}
喜欢 (0)

您必须 登录 才能发表评论!

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

请关闭 Adblock 等类似浏览器插件,然后刷新页面访问,感谢您的支持!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.