开发者

Detect presses to headset buttons on Android?

开发者 https://www.devze.com 2023-03-07 14:44 出处:网络
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.

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?


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);
}


It's the ACTION_MEDIA_BUTTON intent.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消