软硬件环境

  • OTT Box 5.1.1
  • Android studio 3.0.1

前言

在做Android TV时,经常会将一些对系统或者是app的设置独立出来,用一种相对隐秘的方法叫出,供工程部署人员使用,即所谓的工程菜单。

实现

通过在dispatchKeyEvent方法中拦截键值,以组合键*0#为例,当系统接收到这个组合键时,开启另一个app,函数doStartApplicationWithPackageName就是用来做这个的,换成你想实现的方法就成。

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_STAR:
            keyStarTime = System.currentTimeMillis();
            break;

        case KeyEvent.KEYCODE_0:
            keyZeroTime = System.currentTimeMillis();
            break;

        case KeyEvent.KEYCODE_POUND:
            keyPoundTime = System.currentTimeMillis();
            break;
    }

    checkCombinationKey(keyStarTime, keyZeroTime, keyPoundTime);

    return super.dispatchKeyEvent(event);
}

private void checkCombinationKey(long keystar, long keyZero, long keyPound) {
    //组合键 *0# 打开系统设置app
    long currTime = System.currentTimeMillis();

    if ((keystar <= keyZero) && (keyZero <= keyPound) && (currTime - keystar < 2000) && (currTime - keyZero < 2000) && (currTime - keyPound < 2000)) {
        doStartApplicationWithPackageName(settingPackageName);
    }
}

参考资料


Comments

comments powered by Disqus