android – 同时检测两个硬件按钮

当我的两个硬件按钮按下音量按钮和电源按钮时,我想添加监听器.但我知道你无法覆盖电源按钮.实际上我想拍摄屏幕快照,所以我想出了这个解决方案.所以这就是我想要实现的,如果您对此有任何建议请分享.
您需要覆盖onKeyDownonKeyUp事件以检测按下的两个按钮.

 public boolean first,second;

 public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_POWER){
        first = true;
    }
    else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        second = true;
    }

    if(first && second) {
        // Two buttons pressed, Do your stuff
    }
    return true;
 }

 public boolean onKeyUp(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_POWER){
        first = false;
    }
    else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
        second = false;
    }
    return true;
 }
https://stackoverflow.com/questions/25420414/detect-two-hardware-button-press-simultaneously

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:android – 同时检测两个硬件按钮