Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not allowed to start service

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!

like image 573
Eduard Avatar asked Jun 25 '18 11:06

Eduard


People also ask

How to start service in Android?

Starting a service You can start a service from an activity or other application component by passing an Intent to startService() or startForegroundService() . The Android system calls the service's onStartCommand() method and passes it the Intent , which specifies which service to start.

How to declare service in manifest Android?

You declare a service in your app's Manifest, by adding a <service> element as a child of your <application> element. There's a list of attributes that you can use to control a service's behavior, but as a minimum you'll need to provide the service's name (android:name) and a description (android:description).

What is Java Lang IllegalStateException?

An IllegalStateException is a runtime exception in Java that is thrown to indicate that a method has been invoked at the wrong time. This exception is used to signal that a method is called at an illegal or inappropriate time.

What is background services?

A Background Service is a service that runs only when the app is running so it'll get terminated when the app is terminated. A Foreground Service is a service that stays alive even when the app is terminated. And a Bound Service is a service that runs only if the component it is bound to is still active.


1 Answers

I am having the same issue in my app, using the same cast framework version. Looking at the Google release notes, updating to 16.0.1 might solve it:

Cast

Fixed an IllegalStateException which happens when a seek request times out in RemoteMediaPlayer and RemoteMediaClient. This is a regression introduced in 15.0.0. Clients should update to 16.0.1 or newer to get the fix.

https://developers.google.com/android/guides/releases

like image 88
Nicklas Avatar answered Oct 02 '22 08:10

Nicklas