我希望在我的两个硬件按钮按下时(例如音量按钮和电源按钮),能够添加监听器。但是我了解到无法覆盖电源按钮。实际上我想要拍摄屏幕截图,所以我提出了这个解决方案。所以这就是我想要实现的,如果您有任何建议,请分享。
我希望在我的两个硬件按钮按下时(例如音量按钮和电源按钮),能够添加监听器。但是我了解到无法覆盖电源按钮。实际上我想要拍摄屏幕截图,所以我提出了这个解决方案。所以这就是我想要实现的,如果您有任何建议,请分享。
private static final int PRESS_INTERVAL = 700;
private long mUpKeyEventTime = 0;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_POWER == event.getKeyCode()) {
if ((event.getEventTime() - mUpKeyEventTime) < PRESS_INTERVAL) {
// This is to check if Volume UP key and Power key are pressed at the same time.
// Do the Task. Here You can add logic to take screenshot
}
return true;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return true;
}
return super.onKeyDown(keyCode, event);
}
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(KeyEvent.KEYCODE_VOLUME_UP == keyCode){
mUpKeyEventTime = event.getEventTime();
}
return super.onKeyUp(keyCode, event);
}
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;
}