Want to Access Power Button events in android [duplicate]
Asked Answered
P

1

9
   i=0;
   public boolean onKeyDown(int keyCode, KeyEvent event) {
    System.out.println("In Key Down Method." + event.getKeyCode());
    if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
        i++;
        System.out.println("Power button pressed.");
        if (i == 2) {
            System.out.println("Power button pressed continuoulsy 3 times.");
            Toast.makeText(MainActivity.this, "Power button pressed " + i + " times.", Toast.LENGTH_SHORT).show();
        } else {
            System.out.println("Power button pressed continuoulsy " + i + " times.");
            Toast.makeText(MainActivity.this, "Power button pressed " + i + " times.", Toast.LENGTH_SHORT).show();
        }
    }
    return super.onKeyDown(keyCode, event);
}

I am trying to access power button event using above code.Its working fine for Volume_Up and Volume_Down Key but not Working For Power/Lock Button.Why it happens? Suggest Some Example or code.

Prevenient answered 11/5, 2016 at 9:8 Comment(1)
But its also not working. #3703571Prevenient
B
2

You should add the following permission to your manifest file:

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

Beekman answered 11/5, 2016 at 9:16 Comment(1)
This permission is no longer available. Its better to follow @Devendra Singh 's answerErvinervine

© 2022 - 2024 — McMap. All rights reserved.