tags:

views:

122

answers:

1

Hi, I need to use volume buttons to control a variable parameter in my application. I use Activity.onKeyDown to get notified when the button is pressed but the media volume is also increased. Android is doing something like below when I press the volume key: 1. increase media / ringtone volume 2. pass the event to my application

Is there a way to avoid increasing the system volume and use volume key only for my application ?

Thanks

+1  A: 

You need to capture all actions:

@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_UP) {
                //TODO
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }
alex
works great..thanks alex
Iuliu Atudosiei