How to disable the physical volume buttons on Android?

android

Solution

Try overriding `dispatchKeyEvent(KeyEvent event)`:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    boolean result;
     switch( event.getKeyCode() ) {
        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            result = true;
            break;

         default:
            result= super.dispatchKeyEvent(event);
            break;
     }

     return result;
}

also see this article.

Problem

I am developing an Android application, and my apps will play out an alarm once it is started, but I would like to disable the external volume buttons so that when the phone is alarming, user is not able to turn down the alarm volume. I have tested on my phone with Android version 2.3.5, but it is not working. Below is my coding. Hope someone can help me... Thanks. ``` @Override public boolean onKeyDown(int keyCode, KeyEvent event){ if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){ Toast.makeText(this, "Volume Up", Toast.LENGTH_LONG).show(); return true; } if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){ Toast.makeText(this, "Volume Down", Toast.LENGTH_LONG).show(); return true; } return super.onKeyDown(keyCode, event); } ```

Original source