Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect presses to headset buttons on Android?

Tags:

android

How can I detect if a buton, on a pair of headphones, was pressed while running an Android app? Music applications use to this button to stop, play, pause music, etc.

Does a signal get sent to the microphone? Is it treated as a key press event?

like image 859
George Johnston Avatar asked May 20 '11 18:05

George Johnston


3 Answers

There's a simpler way which doesn't require any BroadcastReceiver to be registered if you only want to listen for headset button callbacks while your app (particular activity) is running, simply override Activity onKeyDown method:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_HEADSETHOOK){
        //handle click
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
like image 90
Adrian Kapuscinski Avatar answered Oct 31 '22 16:10

Adrian Kapuscinski


It's the ACTION_MEDIA_BUTTON intent.

like image 23
EboMike Avatar answered Oct 31 '22 18:10

EboMike


Use the ACTION_MEDIA_BUTTON and ACTION_AUDIO_BECOMING_NOISY. Refer this doc: Developer Android

like image 2
ankit Avatar answered Oct 31 '22 18:10

ankit