Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to start receiver android.support.v4.media.session.MediaButtonReceiver

Tags:

android

I am getting this crash on my crashlytics,have anyone faced an issue like this?

Thx,in advance

Exception java.lang.RuntimeException: Unable to start receiver android.support.v4.media.session.MediaButtonReceiver: java.lang.IllegalStateException: Could not find any Service that handles [REDACTED_DOMAIN_NAME]_BUTTON or a media browser service implementation android.app.ActivityThread.handleReceiver (ActivityThread.java:3643) android.app.ActivityThread.access$2000 (ActivityThread.java:222) android.app.ActivityThread$H.handleMessage (ActivityThread.java:1878) android.os.Handler.dispatchMessage (Handler.java:102) android.os.Looper.loop (Looper.java:158) android.app.ActivityThread.main (ActivityThread.java:7230) java.lang.reflect.Method.invoke (Method.java) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230) com.android.internal.os.ZygoteInit.main

(ZygoteInit.java:1120) Caused by java.lang.IllegalStateException: Could not find any Service that handles [REDACTED_DOMAIN_NAME]_BUTTON or a media browser service implementation android.support.v4.media.session.MediaButtonReceiver.onReceive (MediaButtonReceiver.java:97) android.app.ActivityThread.handleReceiver (ActivityThread.java:3636) android.app.ActivityThread.access$2000 (ActivityThread.java:222) android.app.ActivityThread$H.handleMessage (ActivityThread.java:1878) android.os.Handler.dispatchMessage (Handler.java:102) android.os.Looper.loop (Looper.java:158) android.app.ActivityThread.main (ActivityThread.java:7230) java.lang.reflect.Method.invoke (Method.java) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)

like image 715
Arman Manucharyan Avatar asked Nov 06 '17 15:11

Arman Manucharyan


2 Answers

if you update your project with androidX dependencies, you must import

<receiver android:name="androidx.media.session.MediaButtonReceiver">
   <intent-filter>
      <action android:name="android.intent.action.MEDIA_BUTTON"/>
   </intent-filter>
</receiver>
like image 121
murgupluoglu Avatar answered Nov 20 '22 20:11

murgupluoglu


Google official document's explain: This class assumes you have a Service in your app that controls media playback via a MediaSessionCompat. Once a key event is received by MediaButtonReceiver, this class tries to find a Service that can handle Intent.ACTION_MEDIA_BUTTON, and a MediaBrowserServiceCompat in turn. If an appropriate service is found, this class forwards the key event to the service. If neither is available or more than one valid service/media browser service is found, an IllegalStateException will be thrown. Thus, your app should have one of the following services to get a key event properly.

You can solve it in the following ways:

1.A service can receive a key event by including an intent filter that handles Intent.ACTION_MEDIA_BUTTON:

<service android:name="com.example.android.MediaPlaybackService" >
   <intent-filter>
     <action android:name="android.intent.action.MEDIA_BUTTON" />
   </intent-filter>
 </service>

Events can then be handled in Service.onStartCommand(Intent, int, int) by calling handleIntent(MediaSessionCompat, Intent), passing in your current MediaSessionCompat:

private MediaSessionCompat mMediaSessionCompat = ...;

public int onStartCommand(Intent intent, int flags, int startId) {
   MediaButtonReceiver.handleIntent(mMediaSessionCompat, intent);
   return super.onStartCommand(intent, flags, startId);
 }

This ensures that the correct callbacks to MediaSessionCompat.Callback will be triggered based on the incoming KeyEvent.

2.MediaBrowserService

If you already have a MediaBrowserServiceCompat in your app, MediaButtonReceiver will deliver the received key events to the MediaBrowserServiceCompat by default. You can handle them in your MediaSessionCompat.Callback.

like image 1
andDack Avatar answered Nov 20 '22 19:11

andDack