Android: Expected 1 service that handles android.media.browse.MediaBrowserService, found 2

Viewed 283

I found a few crashes in Firebase Crashylytics concerning the MediaBrowserService. I think the reason for this is because i have registered two Services in my Manifest, but only one is allowed.

Complete stacktrace

Caused by java.lang.IllegalStateException: Expected 1 service that handles android.media.browse.MediaBrowserService, found 2
       at androidx.media.session.MediaButtonReceiver.getServiceComponentByAction(MediaButtonReceiver.java:318)
       at androidx.media.session.MediaButtonReceiver.onReceive(MediaButtonReceiver.java:118)
       at android.app.ActivityThread.handleReceiver(ActivityThread.java:3612)
       at android.app.ActivityThread.access$1300(ActivityThread.java:238)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1805)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:7099)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

Services in AndroidManifest.xml

<service
    android:name=".ServiceFromMyApp"
    android:enabled="true"
    android:exported="false" >
    <intent-filter>
        <action android:name="android.media.browse.MediaBrowserService" />
    </intent-filter>
</service>
  
<service
    android:name=".ServiceFromExternalLibrary"
    android:enabled="true"
    android:exported="false" >
    <intent-filter>
        <action android:name="android.media.browse.MediaBrowserService" />
    </intent-filter>
</service>

Unfortunatelly i need both. Is there a way to fix this issue? Or can i even work with only one?

1 Answers

As I can see from the google documentation, MediaButtonReceiver is called only if the application was killed and the user tries to click a button on headphones:

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.

(https://developer.android.com/guide/topics/media-apps/mediabuttons#finding-a-media-session)

So, I think this is a rare situation, so you can just remove these lines from your manifest and don't handle such case:

<receiver android:name="androidx.media.session.MediaButtonReceiver">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

Either you can implement your own customised MediaButtonReceiver, which will choose the appropriate media service to start, when the app was killed.

Related