You have to capture the event as mentionned here : Android - Volume Buttons used in my application
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
//TODO
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
//TODO
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
dispatchKeyEvent is not only called for volume keys, it will catch all the key event so you have to :
- Get the event code
- Check if it's what you are seeking for
- Do what you want according to the event :)
The key is dispatchKeyEvent is called before any other method by the system, so you can intercept the event