Override Power button just like Home button

Related searches

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.


Then, I had created a ReceiverScreen which receives broadcast of Screen on/off


public class ReceiverScreen extends BroadcastReceiver {

    public static boolean wasScreenOn = true;

    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;


public class DisableHardButton extends Activity {

public void onCreate(Bundle savedInstanceState) {

    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    BroadcastReceiver mReceiver = new ReceiverScreen();
    registerReceiver(mReceiver, filter);

    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

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

Phew. This is quite a contended question, with a great deal of commentary behind it.

Let me begin by rephrasing your question a bit. If I understand clearly, you'd like to disable all physical buttons on the device for the duration of your activity. This is inclusive of the power button, which you detect by handling the ACTION_SCREEN_OFF intent. Your current (successful) workaround for this scenario is to broadcast an ACTION_SCREEN_ON, kicking the screen back to life when it's turned off, provided the host implements this correctly.

You'd now like to go the extra mile, by rendering this action unnecessary, if (and only if) you are able to catch and handle ACTION_SCREEN_OFF before it gets sent to the system. Is this possible, and if so, how?

This bears some discussion. The short version, however, is simple: this is not possible, without custom modification to your firmware or the core of the Android operating system, for a limited number of devices.

The Android system, as far as is documented, defines this as a broadcast action. Following the publish-subscribe pattern of message propagation, this message will notify all concerned parties of this action. Because this message is sent by the system, because the message stack is managed by the system, and because the message is also received by the system, your code simply isn't injected in the right place to block the reception of this message.

Furthermore, for some devices, this will be a physical switch that has no programmatic interrupt whatsoever. At best, the Android system can hope to handle for the event when it happens, which is precisely why this broadcast message is something of a special case in the service stack. To the best of my knowledge and based upon the sparse documentation on this scenario, this is why it's not an out-of-the-box, supported feature, before we consider any of the possible vectors for abuse.

Your best recourse is actually a much simpler one, if you have the ability to modify the physical hardware for the device in question: restrict access to, mangle, or otherwise disable the physical power button. In many scenarios this is undesirable, but this works in situations where your Android device is being used, for example, as a point-of-sale or a public service machine. If this is unworkable, placing physical restrictions on access to the power button may be all that's required, handling for the remainder of cases (for example, spot attempts to toggle the screen) by sending ACTION_SCREEN_ON.

Just remember, this strategy isn't foolproof. Do this competently and document the procedure well, lest you become fodder for the next 2600 exposé.

Best of luck with your application.

Override Power button just like Home button, 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. "Is there a way to override it and have a button on the chassis of the box? (Without having to (un)solder any components on the board!) "Yes. In the DeviceTree, there is an entry for linking the power button GPIO to a power on/off. Just change this DT definition to the one you are using for your button (it must be attached to the GPIO lines

On the contrary to all the answers .. Check this out:

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);
                return true;

        return super.dispatchKeyEvent(event);

What it does:

Whenever the user presses the power button. We cancel the Dialog before its even seen!! :) . This works for me.. (Tested on note2)

Disable Hardware Buttons | SureLock for Android, How To Disable Power and Other Hardware Buttons On Android They can be used to launch unwanted apps like the camera to take screenshots or videos. The easiest way to prevent this is by disabling device hardware keys to Access SureLock Settings by tapping on SureLock Home Screen 5� Yes you can do override Home button. I have developed an application which disable hard button, you can have a look. I have taken a toggle button which locks all hard button to work except Power button

Here is what I have done:

(1) Create a receiver class:

public class ScreenReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        switch (action) {
            case Intent.ACTION_SCREEN_OFF:

            case Intent.ACTION_SCREEN_ON:
                // and do whatever you need to do here

(2) The two methods that are called above looked like this:

public static void unlockScreen () {
    if (current == null) return;

    Log.i( Constants.TAG, "Turning on screen ... " );

    Window window = current.getWindow();
    window.addFlags( WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD );
    window.addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED );
    window.addFlags( WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON   );

public static void clearScreen () {
    if (current == null) return;

    Log.i( Constants.TAG, "Clearing screen flag when on ... " );

    Window window = current.getWindow();
    window.clearFlags( WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD );
    window.clearFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED );
    window.clearFlags( WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON   );

private static BaseActivity current;

protected void onResume () {
    current = this;

(3) In the main activity class onCreate() method register the receiver:

    IntentFilter filter = new IntentFilter( Intent.ACTION_SCREEN_ON );
    filter.addAction( Intent.ACTION_SCREEN_OFF );
    registerReceiver(new ScreenReceiver(), filter);

Power Button to Volume Button, If you love this app, check out Fetch Internet, which gives your computer Internet access WITHOUT PAYING for a hotspot! hi your just trying to get notifed of the event than as Kartik pointed out you just need play around with the onKeyDown Event. but form what I understand of your question you actually want to prevent the behavior of the power button and that can't be done. just like the home button pressed some events in android can't be prevented to avoid people having apps they can't quit.

You can get to know about the Power Button press, but I dont think you will get to override it and you will be able to dismiss the dialog for power off and show something else.

Here is what I have tried,

package com.easylogs.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
    public void onReceive(final Context context, final Intent intent) {
        Log.i("HERE", "------------------------------------HERE");
        Toast.makeText(context, "OFFFFFFFFFFFFFFFF",Toast.LENGTH_LONG).show();

and declaration in Manifest as,

    <receiver android:name="CloseSystemDialogsIntentReceiver">
            <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
            <category android:name="android.intent.category.DEFAULT" />

How to disable or switch the functionality of the Power button , P.S - my Nexus 5 is right now unrooted, so I rather have a solution that I wish there were a setting to have to press the power button twice or Initially I thought it was a slightly annoying design flaw, but now I kind of like that side effect. long hold on the home button enacted Play/Pause for music controls� To Change Default Power Button Action in Advanced Power Options 1 Open Advanced Power Options. 2 Expand open Power buttons and lid and Power button action. (see screenshot below) 3 Select the action you want, and click/tap on OK. (see screenshot below)

public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {

        return true;

    return super.dispatchKeyEvent(event);

have you tried the above code where we can handle power key event. Also have a look at this LINK which has handled power button pressed event.

How to Unlock Your Android Phone Screen Without the Power Button, Whether the power button is not working on Android or you are simply tired of Screen Off adds a handy shortcut on your phone's home screen. Access > Device Admin Apps. Disable the permission for the app you'd like to remove and then� Turning off Touch ID until your Home button is fixed. Alright, so your Home button is back as a fittingly spooky, ghost-like version of its former self, and you’ve got everything you need to

Hold down the power button until the “slide to power off” option appears on the device’s display. 3. Next, release the power button, and then hold down the home button for roughly five seconds. And

1) Start your iTunes and connect your iPod touch to your comp. 2) Keep holding the POWER button (top right) together with the HOME button (centre, bellow the ipod screen) for about 10 seconds. You should hear a sound (s).. Release the POWER button ONLY, keep holding the HOME button, you will again hear some sounds.

For the button at least there is setting that you need to do long press for it, so that removes accidental short clicks: VR: Settings > Dash > Long Press even saw people selling some kind of button covers for home/system buttons.. but no real solutions to override.

  • what is the purpose..., may be there is other workaround
  • You can't do that with the stock android. Also there are many questions regarding this on Stackoverflow.com, what do you expect to here on yours now?
  • Seems similar Question,check this stackoverflow.com/questions/8036701/how-to-disable-power-button
  • So you can hijack somebody's phone?
  • Why you people always thinking of misusing technology?, i can't understand
  • Extra credit to improve this answer: does someone have a good list of devices that run Android and use physical device switches for their screen and power states? Given the open-source nature of Android, it's clear why this is a design decision, but I can find no good, current list of examples to cite here.
  • I don't know of a device that doesn't use a physical switch for power at least.
  • Me too agreed that its not possible without any modification at firmware. @MrGomez Firstly let me tell you earlier what i get to know is you cant override the HOME button but still I had done something with it(link pasted above), so the same thing i want with Power button. Expecting some nuisance idea. I dint expect still answer but a +1 to you.
  • @hotveryspicy Agreed. The interesting thing about the Android home button is it's not guaranteed to be physical and, indeed, it doesn't carry the same power state coupling. So, at the OS level, it can be disabled by using tricky workarounds (ie, orphan theming, TYPE_KEYGUARD, or in some cases, simply pretending the system has a physical key when it doesn't). When you have something that alters the power state in hardware, however, it becomes more difficult, bordering on solutions that are even more convoluted (if that makes sense). Thank you for your support, in any event. :)
  • @MrGomez Convinced, this is the answer as of now, but I will try. many Thanks.
  • I could confirm that this works on my Moto G running on Android 4.4.4
  • Hi venkat.. Is the above code working for you... I tried it in Moto X but the screen is going off.
  • @amalBit i using this code in Activity. But when i am pressing the power button i still able to see the dialog box.