好吧,我正在做一些事情,我想禁用设备的所有硬按钮。
电源、主页、音量增大、音量减小、搜索、返回等硬按钮。
我已经成功地覆盖了除了电源之外的几乎所有按钮。
所以我只是想让你们看到并分享一些想法,这样我就可以摆脱它。
我得到了长按Power keyevent in onDispatchKeyEvent()
,就像我想抓住它的短按一样。此外,当按下电源时,我还尝试通过获取of来停止屏幕关闭,并且我成功接收了它,但我无法处理它。Broadcast
SCREEN_OFF
谢谢。
然后,我创建了一个接收屏幕开/关广播的 ReceiverScreen
ReceiverScreen.java
public class ReceiverScreen extends BroadcastReceiver {
public static boolean wasScreenOn = true;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
// do whatever you need to do here
wasScreenOn = false;
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// and do whatever you need to do here
wasScreenOn = true;
}
}
}
DisableHardButton.java
public class DisableHardButton extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ReceiverScreen();
registerReceiver(mReceiver, filter);
}
@Override
protected void onPause() {
// when the screen is about to turn off
if (ScreenReceiver.wasScreenOn) {
// this is the case when onPause() is called by the system due to a screen state change
System.out.println("SCREEN TURNED OFF");
} else {
// this is when onPause() is called when the screen state has not changed
}
super.onPause();
}
@Override
protected void onResume() {
// only when screen turns on
if (!ScreenReceiver.wasScreenOn) {
// this is when onResume() is called due to a screen state change
System.out.println("SCREEN TURNED ON");
} else {
// this is when onResume() is called when the screen state has not changed
}
super.onResume();
}
}