处理Android蓝牙耳机按钮

处理Android蓝牙耳机按钮,android,bluetooth,headset,media-buttons,Android,Bluetooth,Headset,Media Buttons,这个话题有时会突然出现,但我不能让它正常工作。我的应用程序过去处理媒体按钮,但Android 5.0后的API发生了变化,它需要调整此应用程序以“捕获”蓝牙按钮并“按我的方式”处理它们 我做了很多搜索,发现: final MediaSession session = new MediaSession(this, "spy tag"); session.setCallback(new MediaSession.Callback() { @Override

这个话题有时会突然出现,但我不能让它正常工作。我的应用程序过去处理媒体按钮,但Android 5.0后的API发生了变化,它需要调整此应用程序以“捕获”蓝牙按钮并“按我的方式”处理它们

我做了很多搜索,发现:

        final MediaSession session = new MediaSession(this, "spy tag");

    session.setCallback(new MediaSession.Callback() {
        @Override
        public boolean onMediaButtonEvent(final Intent mediaButtonIntent) {
            Toast.makeText(me, "on media button event", Toast.LENGTH_SHORT);
            Log.i("TAG", "GOT EVENT");
            return super.onMediaButtonEvent(mediaButtonIntent);
        }

    });

    session.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);

    PlaybackState state = new PlaybackState.Builder()
            .setActions(
                    PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE |
                            PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE |
                            PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS)
            .setState(PlaybackState.STATE_PLAYING, 0, 1)
            .build();
    session.setPlaybackState(state);

    session.setActive(true);
但是。。。它仍然不起作用。无论何时按下任何媒体按钮,我都会在Logcat中看到:

2019-06-16 19:40:14.4582936-4422/?D/MediaSessionService:将KeyEvent{action=action\u DOWN,keyCode=keyCode\u MEDIA\u NEXT,scanCode=0,metaState=0,flags=0x0,repeatCount=0,eventTime=0,downTime=0,deviceId=-1,source=0x0}发送到最后一个已知的PendingEvent PendingEvent{afe4538:PendingEntRecord{{13648e com.google.android.youtube broadcastIntent}} 2019-06-16 19:40:14.460 3230-3230/? V/Avrcp:recordKeyDispatched:KeyEvent{action=action\u DOWN,keyCode=keyCode\u MEDIA\u NEXT,scanCode=0,metaState=0,flags=0x0,repeatCount=0,eventTime=0,downTime=0,deviceId=-1,source=0x0}被发送到com.google.android.youtube 2019-06-16 19:40:14.466 2936-4422/? D/MediaSessionService:将KeyEvent{action=action\u UP,keyCode=keyCode\u MEDIA\u NEXT,scanCode=0,metaState=0,flags=0x0,repeatCount=0,eventTime=0,downTime=0,deviceId=-1,source=0x0}发送到最后一个已知的PendingEvent PendingEvent{afe4538:PendingEntRecord{{13648e com.google.android.youtube broadcastIntent}} 2019-06-16 19:40:14.475 3230-3230/? V/Avrcp:recordKeyDispatched:KeyEvent{action=action\u UP,keyCode=keyCode\u MEDIA\u NEXT,scanCode=0,metaState=0,flags=0x0,repeatCount=0,eventTime=0,downTime=0,deviceId=-1,source=0x0}被发送到com.google.android.youtube

我的应用程序没有收到任何回调。我错过了什么?

这方面运气好吗?(波兹德罗)