Android How to listen for Volume Button events?

后端 未结 5 2423
执念已碎
执念已碎 2020-11-27 04:13

I know you guys are probably tired of these kinds of posts, but why doesn\'t anything happen when I press volume down? I\'m just trying to make a simple code, but apparently

5条回答
  •  甜味超标
    2020-11-27 04:52

    Another approach

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
            switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                if (action == KeyEvent.ACTION_DOWN) {
                    //TODO
                }
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                if (action == KeyEvent.ACTION_DOWN) {
                    //TODO
                }
                return true;
            default:
                return super.dispatchKeyEvent(event);
            }
        }
    

提交回复
热议问题