12

Is it possible to override the functionality of the android power button ? I want to 'disable' the power button. My plan is to override the functionality of this button to do nothing ( {}; ) Well my main idea is to disable the turning the screen off.

I want when someone press the power button nothing to happen.

How can I do this ?

Lukap
  • 31,523
  • 64
  • 157
  • 244
  • 3
    IMO this is not good behaviour. I'd uninstall an app that does this. If you want to keep the screen on you could aquire the `PowerManager.WakeLock` – Ben Weiss Nov 07 '11 at 12:55
  • I don't think it is possible. – Andro Selva Nov 07 '11 at 12:58
  • 1
    That doesn't seem like a good idea. You can keep the phone from automatically going to sleep, but if the user wants to turn the screen off (s)he should be able to do it. I know I'd be pretty angry at the app for doing this. – DeeV Nov 07 '11 at 13:27
  • 2
    If the user doesn't want this behavior he/she will not install the app at all. My goal is to make app that will delay the screen off for few second and before the turning off I want to make some animations. – Lukap Nov 07 '11 at 13:29
  • Now you can: http://stackoverflow.com/questions/10817367/disable-screen-lockpower-button-in-android – justnajm Mar 25 '13 at 08:29

3 Answers3

9

Is it possible to override the functionality of the android power button ? I want to 'disable' the power button.

Fortunately, this is not possible, except perhaps via custom firmware.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    why being not possible is a "fortunately" – B.shruti May 21 '19 at 11:31
  • 3
    @B.shruti: Because disabling the power button is not something an ordinary app should be able to do. Among other things, it would make it easier for malware to prevent users from removing that malware by rebooting in safe mode. – CommonsWare May 21 '19 at 15:40
3

It works on Cyanogenmod: http://review.cyanogenmod.com/#/c/18924/4/core/res/AndroidManifest.xml you would need the android.permission.PREVENT_POWER_KEY permission.

Btw one good scenario is to use the power button as a camera shutter button (if the device does not have a dedicated one).

Afaik system apps can intercept the power button normally.

user765269
  • 311
  • 1
  • 4
  • 12
-3

Try implementing onKeyDown, and if the given key pressed keycode is KEYCODE_POWER and the action is ACTION_DOWN and right after it ACTION_UP return true (means you handled this key press)

Jong
  • 9,045
  • 3
  • 34
  • 66