Thursday, April 19, 2012

Override Power button just like Home button

Well, i am Doing a stuff in which I want to disable all hard button of the device



Hard button like Power, Home, Volume up, Volume down, Search, Back.



I successfully override almost all buttons Here except Power



so i just want you people to see and please share some idea so that I can away with it



I am getting the long press Power keyevent in onDispatchKeyEvent(), sameway i wanted to catch short click of the same. Morever when pressing power i also tried to stop Screen off by getting the Broadcast of SCREEN_OFF and succeed to receive but not able to handle it.



Thanks.



Till i had created a RecieverScreen which recieves 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();
}
}




No comments:

Post a Comment