1

I would like to disable the side volume buttons so the only way to control the volume will be from a dedicated activity inside my android app. I managed to disable it for all my activities by adding the following code:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.d(TAG, "onKeyDown = " + keyCode);
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        return true;
        }
    if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        return true;
    }
    return super.onKeyDown(keyCode, event);

}

But I don't know how to disable it for the activities i start from my app (for example I start the gallery app)!

I know it is possible as 'Volume Locker' app doing similar stuff.

Amir
  • 153
  • 1
  • 1
  • 9

1 Answers1

3

It is not possible within public APIs to suppress the key events outside of your own Activities, if there is an app that has managed to do it what they are doing would be considered malicious by the platform designers and will get fixed at some point.

Based on the description given for that app (note: I've never used it personally)

Prevent accidental changes to your volume settings, install Volume Locker today. This app helps prevent against accidental volume changes by confirming the change you made, by either tray notification or a pop up. If you don't approve the change, the volume will be reset within a set amount of seconds... By setting the timeout to "instant", the locked volumes will revert instantly without prompting.

I suspect what that is actually doing is listening for the volume buttons using a similar technique to the one in this answer and just reverting whatever change was made instantly(ish). That would make it seem to the user like the key press did nothing but in reality what happened is the volume changed and then quickly changed back.

Community
  • 1
  • 1
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156