5

I'm developing an app in which there is a requirement to not let the user to lock the screen or turn off the android device using power button,so I have to disable the power button and over ride the power button functionality, I have searched alot on internet as well but can't find anything.I have used these two pieces of code but still it did not work for me.

 @Override
      public boolean dispatchKeyEvent(KeyEvent event) {
              if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
                      Log.i("", "Dispath event power");
                      Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                      sendBroadcast(closeDialog);
                      return true;
              }

              return super.dispatchKeyEvent(event);
      }
      public boolean onKeyDown(int keyCode, KeyEvent event)
      {
          if (keyCode == KeyEvent.KEYCODE_POWER) {
                // Back
                moveTaskToBack(true);
                return true;
            }

            else {
                // Return

Please help me thanks in advance.

Talib
  • 1,134
  • 5
  • 31
  • 58

3 Answers3

2

You can't deal with the Power event in any Android Apps. Events ofPower Buttonare handled in Framework level in Android architecture. You can't stop framework to dispatch events but accept its dispatch. You can receive the result of event dispatch from framework but not block the process.

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
0

You cannot prevent the user from controlling his phone,You could perhaps prevent the device from sleeping or locking itself. Tapping into the power button,however, is possible as mentioned here

Community
  • 1
  • 1
Droidekas
  • 3,464
  • 2
  • 26
  • 40
0

I had this problem as well. This link was helpful: "Overriding The Power Button"

My issues was keeping a camera app from being accidentally stopped, so my answer was slightly different than Bob's. I simplly recalled the existing event. So, in other words, this worked for my app: (put in BroadcastReceiver:

public class ScreenReceiver extends BroadcastReceiver

The snippet that is different for me is:

//-------------------------------------------------------------
// POWER BUTTON - SHORT click
// We are moving from a SCREEN ON state to a SCREEN OFF State
//--------------------------------------------------------------
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
{
    wasScreenOn = false;
    if (cameraIsOn)
    {
        PowerManager newPM = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = newPM.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "TEST");
        wakeLock.acquire();

        AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent ExistingIntent = intent;
        PendingIntent pi = PendingIntent.getActivity(context, 0, ExistingIntent, 0);
        alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10, pi);
    }
        return; 
}

Still doing tests, but that seems to work for me. (I have to also monitor the camera record state because when the screen goes off, the camera stops. I restart it -- but of course, some video is going to be lost. It seems, tho, that without rooting the device, that is an impossible thing to stop from happening.)

On one hand, I understand why the power switch HAS to be able to be called to shut the device off. OTOH,it makes idiot proofing stuff (on a re-purposed device) impossible.

Community
  • 1
  • 1
MarkJoel60
  • 537
  • 2
  • 6
  • 24