在android中组合电源按钮和音量增大按钮

时间:2013-03-24 02:24:54

标签: android

就像电源按钮和音量调低按钮的组合一样 - 获取手机的屏幕截图...同样地,我试图在我的应用程序中访问音量增大和电源按钮长按键并提供快捷方式。这可能吗?

我知道如何单独访问这两个按钮但不能同时进行组合。

      @Override
      public boolean onKeyLongPress(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_POWER){
        Log.w("LongPress", "power LONG PRESS");
    }

                if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        Log.w("LongPress", "Volume Up LONG PRESS");
    }

    return super.onKeyLongPress(keyCode, event);
   }

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        event.startTracking();
        return true;
    }
    return super.onKeyDown(keyCode, event);
  }

如何在这里完成组合'&&'不起作用?

1 个答案:

答案 0 :(得分:-1)

我不理解android下的编程,但我看到它使用Java类,所以试试这个

public class BitKeys implements KeyListener {

    private BitSet keyBits = new BitSet(256);

    @Override
    public void keyPressed(final KeyEvent event) {
        int keyCode = event.getKeyCode();
        keyBits.set(keyCode);
    }

    @Override
    public void keyReleased(final KeyEvent event) {
        int keyCode = event.getKeyCode();
        keyBits.clear(keyCode);
    }

    @Override
    public void keyTyped(final KeyEvent event) {
        // don't care
    }

    public boolean isKeyPressed(final int keyCode) {
        return keyBits.get(keyCode);
    }

}

我在这里找到了这个: handle multiple key presses ignoring repeated key