在您Activity
只需添加以下代码段:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// do nothing but show a warning message
Toast.makeText(this, "you pressed the power button",Toast.LENGTH_SHORT).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
如果上面的代码段不起作用,你可以试试这个:
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
//do nothing but show a warning message
Toast.makeText(this, "you pressed the power button",Toast.LENGTH_SHORT).show();
return true;
}
return super.dispatchKeyEvent(event);
}