0

I need an ideal way to use the power button in the android. I have a code but it does not work and when running onKeyLongPress it works with the power button and the device is closed or restarted.. So what is your suggestion for this problem ?!

In the first I used it in the manifest file ..

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

Then I wrote the code at home..

@Override public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
    // Do something here...
    return true;
}
return super.onKeyLongPress(keyCode, event);

}

So what is the problem ?

  • The power key is taken by the system to show the power dialog. I don't think its passed to apps at all. – Gabe Sechan Apr 28 '19 at 21:21
  • You can explain to me in more detail @GabeSechan – Nadra Magdy Apr 28 '19 at 21:47
  • and If you have a way or another solution instead of better than this way .. You can tell me ! @GabeSechan – Nadra Magdy Apr 28 '19 at 21:51
  • I'd really recommend against overriding the power button for any reason. Regardless, it looks like later API versions require [a more advanced method](https://stackoverflow.com/a/39205226/608312). – Jake Lee Apr 28 '19 at 22:07

0 Answers0