Well, I am doing something in which I want to disable all hard buttons of the device.
Hard buttons like Power, Home, Volume up, Volume down, Search, Back.
I have successfully overridden almost all buttons here except Power.
So I just want you people to see and please share some ideas so that I get can away with it.
I am getting the long press Power keyevent in onDispatchKeyEvent()
, in the same way I want to catch the short click of the same. Moreover when pressing power I also tried to stop Screen off by getting the Broadcast
of SCREEN_OFF
and I succeeded in receiving it but I was not able to handle it.
Thanks.
Then, I had created a ReceiverScreen which receives broadcast of Screen on/off
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();
}
}
Stackoverflow.com
, what do you expect to here on yours now? – user