0

I was wondering if I am able to disable the default volume up and down key press on the side of a cell phone.

    @Override
public boolean dispatchKeyEvent(KeyEvent event) {
    Log.v("key pressed", String.valueOf(event.getKeyCode()));
    return false;
    //return super.dispatchKeyEvent(event);
}

I am using dispatchKeyEvent to find when the user presses the volume up or down button but I am not able to stop the phone from actually turning up the volume or turning it down. I want to enable the user to fire methods in my application if they decide to use the volume up or down button. Am I able to stop the phone from performing the volume control when my application is focused?

Kara
  • 6,115
  • 16
  • 50
  • 57
AssemblyX
  • 1,841
  • 1
  • 13
  • 15

1 Answers1

3

I used this in a previous project...perhaps it is what you're looking for:

@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);
    }
}

Android - Volume Buttons used in my application

Community
  • 1
  • 1
jak10h
  • 519
  • 4
  • 11