I'm working on a streaming application that also has Chromecast integration. According to my Crashlytics console, a significant number of users are experiencing the following crash:
Fatal Exception: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.gms.cast.framework.action.UPDATE_NOTIFICATION pkg=com.package.name cmp=com.package.name/com.google.android.gms.cast.framework.media.MediaNotificationService (has extras) }: app is in background uid UidRecord{4ee441 u0a213 CAC bg:+11m25s10ms idle procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1538)
at android.app.ContextImpl.startService(ContextImpl.java:1484)
at android.content.ContextWrapper.startService(ContextWrapper.java:663)
at com.google.android.gms.internal.cast.zzai.zzg(Unknown Source:269)
at com.google.android.gms.internal.cast.zzai.onStatusUpdated(Unknown Source:1)
at com.google.android.gms.cast.framework.media.zzr.onStatusUpdated(Unknown Source:30)
at com.google.android.gms.internal.cast.zzdh.onStatusUpdated(Unknown Source:6)
at com.google.android.gms.internal.cast.zzdh.zzn(Unknown Source:573)
at com.google.android.gms.cast.framework.media.RemoteMediaClient.onMessageReceived(Unknown Source:2)
at com.google.android.gms.internal.cast.zzct.run(Unknown Source:34)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
I couldn't reproduce it so far and that's why I'm posting this here. It seems that the Chromecast device is trying to notify my application about something while it is in background.
All the crashes are only on Oreo and they appear while the application is in background.
The crash wasn't there a few builds ago and I didn't change the Chromecast code in a while, so I have a feeling the cause of it is updating some dependencies in my build.gradle file.
There are a few crash groups with the same cause and stack trace, but each group has a different UidRecord.
I've read about Background Execution Limits in Oreo, but this seems to be out of my hands, since I'm not touching any service myself.
Since I needed a custom look for my button, this is the way I integrated the Chromecast feature:
<android.support.v7.app.MediaRouteButton
android:id="@+id/btn_cast".../>
...
CastButtonFactory.setUpMediaRouteButton(context, binding.btnCast);
binding.btnCast.setRemoteIndicatorDrawable(ContextCompat.getDrawable(context, R.drawable.ic_control_bar_cast));
...
CastContext castContext = CastContext.getSharedInstance(context);
castContext.addCastStateListener(this);
...
@Override
public void onCastStateChanged(int state) {
switch (state) {
case CastState.CONNECTED:
...
}
}
...
CastSession currentCastSession = castContext.getSessionManager().getCurrentCastSession();
...
RemoteMediaClient remoteMediaClient = currentCastSession.getRemoteMediaClient();
...
remoteMediaClient.load(mediaInfo, mediaLoadOptions);
Basically, clicking on the button opens the system dialog in which you select the device. When you select a device, the callback is called and I'm sending the data I want to play using the load() method of the RemoteMediaClient class.
My dependencies are:
implementation 'com.android.support:mediarouter-v7:27.1.1'
implementation 'com.google.android.gms:play-services-cast-framework:15.0.1'
I already updated these libraries a few times hoping the problem will go away (I think the problem started somewhere with 27.0.2 and 11.8.0).
Any idea what could case this, in what conditions and how to solve it?
Thanks!