user3506179
user3506179

Reputation: 11

Change volume key function in android app

I have source code for SmartMouse android app. I want to alter the function of volume keys with the onscreen buttons. I have basic knowledge for C programming but don't know java. What part should I search for in the code?

This might be a lame question but I badly need this.

Upvotes: 1

Views: 2524

Answers (1)

Plumillon Forge
Plumillon Forge

Reputation: 1643

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

Upvotes: 4

Related Questions