On an Android device, where the only buttons are the volume buttons and a power button, I want to make the app react to presses on the power button (long and short). How is
You could overwrite the public boolean onKeyDown(int keyCode, KeyEvent event)
and public boolean onKeyUp(int keyCode, KeyEvent event)
functions in your Activity class and test if keyCode
is equal to KeyEvent.KEYCODE_POWER
.
I haven't tested this, but I would assume the system treats this like it does the Home
key in that you cannot stop the system from receiving the key event, you can only observe that it occurs. To test this, try returning True
from the above functions and see if this catches the key event.