I’m trying to make an application where the user can override the default behaviour of the volume up/down buttons (as well as the screen on/off button – is this possible?). Anyways, using some code along the lines of the following I can do this:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyUp(keyCode, event);
if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) {
//this is where I can do my stuff
return true; //because I handled the event
}
return false; //otherwise the system can handle it
}
But I would like it to be possible even when the application is not open, hence why I’d like to set up a broadcast receiver or maybe stick something in a service to make this possible.
Thanks for any help.
Fortunately, no.
This is not possible for the volume buttons.
That is the camera button. If the foreground activity does not consume a camera button click, that gets turned into a broadcast
Intent, which other applications can listen for. This behavior is unique to the camera and media buttons and is not used with any other hardware buttons.