Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect event when hardware volume button is pressed in android

Tags:

android

i am developing an app which listen volume events whenever hardware volume button is pressed. app can be in foreground or background. i have followed [this] (Is there a broadcast action for volume changes?) but its not working correctly. following issues i am facing

1) If I press the volume button once - the event is triggered 4-6 times.
2) If current volume is maximum and i increase the volume then event doesn't fire..

Please help me.

like image 364
tarun Avatar asked Jan 04 '16 13:01

tarun


1 Answers

Try to use following code -

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
        //Do something
    }
    return true;
}

for background - any way to detect volume key presses or volume changes with android service?

like image 129
NehaK Avatar answered Oct 15 '22 11:10

NehaK