6

I have a problem with the new Android version, that is 8.0 (Oreo). I have to register a broadcast and I do this with this code:

// android.intent.action.MEDIA_BUTTON
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
r = new MediaButtonIntentReceiver();
// this line sets receiver priority
filter.setPriority(999);
registerReceiver(r, filter);

This works on older Android version but on Android 8 this doesn't work because it is necessary register explicit broadcast but I don't know how. How can I register explicit broadcast to detect media button?

This is in Manifest.xml:

<receiver android:name=".MediaButtonIntentReceiver">
    <intent-filter android:priority="999">
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • This may help https://developer.android.com/guide/topics/media-apps/mediabuttons.html – Eugen Pechanec Feb 17 '18 at 11:49
  • @EugenPechanec Thank for your reply but I have already read this but I don't understand how implementing a broadcast receiver for media button in Android 8 – Alessandro Marino Feb 17 '18 at 11:55
  • did you read this? "If you are running Android 8.0 (API level 26) or later, the system tries to find the last app with a MediaSession that played audio locally. If the session is still active Android sends the event directly to it. Otherwise, if the session is not active and it has a mediabutton receiver, Android sends the event to the receiver, which will restart the session and so it can receive the event. (See Using media buttons to restart an inactive media session for details.) " https://developer.android.com/guide/topics/media-apps/mediabuttons.html#finding-a-media-session – karandeep singh Feb 17 '18 at 12:06
  • @karandeepsingh yes but I don't use a media session. If I have to implement media session, how can I do this? I've never used media session – Alessandro Marino Feb 17 '18 at 12:08
  • https://stackoverflow.com/questions/10537184/capture-media-button-on-android-4-0-works-on-2-3 – karandeep singh Feb 17 '18 at 12:16
  • @karandeepsingh I have already implemented this but this doesn't work anymore in Android 8 because I have to register explicit broadcast receiver. – Alessandro Marino Feb 17 '18 at 12:19
  • My bad. It seems we can't use this as a broadcast anymore for android 8.0 or above in future. – karandeep singh Feb 17 '18 at 12:20
  • @AlessandroMarino Then start using a media session (as you should have been since API 21). The same API guide should help you with that: https://developer.android.com/guide/topics/media-apps/working-with-a-media-session.html – Eugen Pechanec Feb 17 '18 at 12:21
  • @karandeepsingh In fact this seems impossible to implement on Oreo XD – Alessandro Marino Feb 17 '18 at 12:22
  • @EugenPechanec I have already implemented this too xD but nothing. Maybe I have implemented wrong – Alessandro Marino Feb 17 '18 at 12:23

1 Answers1

8

If you want to receive media button, you have to play audio and use mediasession.

i.g

    MediaSession ms = new MediaSession(getApplicationContext(), getPackageName());
    ms.setActive(true);

    ms.setCallback(new MediaSession.Callback() {
        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
            Log.e("hmhm", "hmhm media button");
            return super.onMediaButtonEvent(mediaButtonIntent);
        }
    });

    // you can button by receiver after terminating your app
    ms.setMediaButtonReceiver(mbr); 

    // play dummy audio
    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
            AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT), AudioTrack.MODE_STREAM);
    at.play();

    // a little sleep 
    at.stop();
    at.release();
오효민
  • 181
  • 1
  • 4