0

I am trying to override the onkeylongpress method in my activity to detect long press of the power button, but nothing happens. It looks like the system is dropping the code in the method.

I have implemented a solution from this site where you track the long press with the method onkeypress, but the toast code does not show up. I have also declared the permission <uses-permission android:name="android.permission.PREVENT_POWER_KEY" />.

The code is not working so I assumed Google did something to the permission. The Android Java code I am using to achieve this with is below:

public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
{
    if (keyCode == Keycode.Power)
    {
        // Start tracking
        e.StartTracking();
        return true;
    }
    return base.OnKeyDown(keyCode, e);
}

public override bool OnKeyLongPress(Keycode keyCode, KeyEvent e)
{
    if (keyCode == Keycode.Power)
    {
        // Show a toast
        Toast.MakeText(this, "Power button long press", ToastLength.Long).Show();
        return true;
    }
    else
    {
        return OnKeyLongPress(keyCode, e);
    }
}
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
TechGeek
  • 128
  • 7
  • There is no such permission: https://developer.android.com/reference/android/Manifest.permission ...you might be rather looking for https://developer.android.com/reference/android/provider/Settings#ACTION_MANAGE_OVERLAY_PERMISSION – Martin Zeitler May 29 '22 at 13:53

0 Answers0