1

I need to capture power key press in android . I tried the following

      @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(KeyEvent.KEYCODE_POWER == event.getKeyCode()){
              //some operations
          }
    return super.onKeyDown(keyCode, event);
    }

But using this code on pressing Power Key control not getting into onKeyDown method.

On a long press of power key , this method gets called.But what i need i on single pressing i need to capture this event

Can anyone help?

Sjk
  • 377
  • 1
  • 5
  • 19
  • Here is the fully working [answer](https://stackoverflow.com/questions/57051157/how-to-override-double-or-even-3-times-clicking-power-button-or-even-volume-up-d/57056408#57056408) to this question – Niamatullah Bakhshi Jul 16 '19 at 11:43

2 Answers2

2

Plenty of threads on this, you have to add permission.

<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />

How to hook into the Power button in Android?

Community
  • 1
  • 1
Paul
  • 5,756
  • 6
  • 48
  • 78
  • 1
    thnks.i have already seen this thread.but its not working for me.That permission is not present in the list – Sjk Jun 24 '13 at 08:55
  • Then perhaps look at the logging of what happens when you press the power key, does it work for other keys? What value does it give when you press power, any? – Paul Jun 24 '13 at 09:01
  • On pressing powerkey control directly comes to onStop. ya other key press is working(back,home,volume) – Sjk Jun 24 '13 at 09:06
  • Hmm I have seen some comments saying this is not possible on stock firmware. They must have removed it. – Paul Jun 24 '13 at 09:09
  • So that means its not possible to capture power key press.?on long press above code will work. – Sjk Jun 24 '13 at 09:10
  • @Sjk Perhaps the accepted answer here could suit your purposes? http://stackoverflow.com/questions/7682016/android-listen-for-power-key-press?rq=1 – Paul Jun 24 '13 at 09:10
1

You are probably missing the permission in the manifest that allows your application to override the default behaviour for the power key, it is also important to return something if you've handled the event to prevent the default behaviour.

<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />

There's further discussion here

So your code would be:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(KeyEvent.KEYCODE_POWER == event.getKeyCode()){
              return true;//If event is handled, falseif 
        }
        return super.onKeyDown(keyCode, event);
    }

If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

Community
  • 1
  • 1
Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
  • 1
    this permission is not present in the list of uses-permission – Sjk Jun 24 '13 at 08:54
  • 2
    on pressing power button,control not event getting into onKeyDown method. I have added logs to check it. – Sjk Jun 24 '13 at 09:00