title: Override Power button just like Home button
body: <p>Well, I am doing something in which I want to disable all hard buttons of the device.</p>
  
  <p>Hard buttons like Power, Home, Volume up, Volume down, Search, Back.</p>
  
  <p>I have successfully overridden almost all buttons <a href="https://stackoverflow.com/questions/10025660/override-home-and-back-button-is-case-a-boolean-is-true/10025904#10025904">here</a> except Power.</p>
  
  <p>So I just want you people to see and please share some ideas so that I get can away with it.</p>
  
  <p>I am getting the <strong>long press</strong> Power keyevent in <code>onDispatchKeyEvent()</code>, in the same way I want to catch the short click of the same. Moreover when pressing power I also tried to stop <em>Screen off</em> by getting the <code>Broadcast</code> of <code>SCREEN_OFF</code> and I succeeded in receiving it but I was not able to handle it.</p>
  
  <p>Thanks.</p>
  
  <p>Then, I had created a ReceiverScreen which receives broadcast of Screen on/off</p>
  
  <p><strong>ReceiverScreen.java</strong></p>
  
  <pre><code>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;
          }
      }
  }
  </code></pre>
  
  <p><strong>DisableHardButton.java</strong></p>
  
  <pre><code>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();
  }
  }
  </code></pre>