Exoplayer: Not allowed to start service, app is in background

Created on 28 Apr 2020  路  4Comments  路  Source: google/ExoPlayer

[REQUIRED] Issue description

When disable foreground service, DownloadService restarting will trigger IllegalStateException, since app is in background, invoking startService is not permitted.

[REQUIRED] Reproduction steps

  • There is downloads in queue because of network requirement is not met
  • app is in background
  • network requirement is met
  • restartService() trigger crash

[REQUIRED] Link to test content

None

A full bug report captured from the device

java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.exoplayer.downloadService.action.INIT cmp=app.podcast.cosmos/io.iftech.android.podcast.player.remote.ffcache.FFDownloadService }: app is in background uid UidRecord{8df726c u0a474 RCVR bg:+1h7m27s763ms idle change:uncached procs:3 seq(0,0,0)}
    at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1720)
    at android.app.ContextImpl.startService(ContextImpl.java:1675)
    at android.content.ContextWrapper.startService(ContextWrapper.java:669)
    at com.google.android.exoplayer2.offline.DownloadService$DownloadManagerHelper.onDownloadsPausedChanged(DownloadService.java:18)
    at com.google.android.exoplayer2.offline.DownloadService$DownloadManagerHelper.onWaitingForRequirementsChanged(DownloadService.java:14)
    at com.google.android.exoplayer2.offline.DownloadManager.notifyWaitingForRequirementsChanged(DownloadManager.java:2)
    at com.google.android.exoplayer2.offline.DownloadManager.lambda$9oihGmKoXEDrfeODE3DbaHprOHM(DownloadManager.java:45)
    at com.google.android.exoplayer2.offline.DownloadManager.lambda$9oihGmKoXEDrfeODE3DbaHprOHM
    at com.google.android.exoplayer2.offline.-$$Lambda$DownloadManager$9oihGmKoXEDrfeODE3DbaHprOHM.onRequirementsStateChanged
    at com.google.android.exoplayer2.scheduler.RequirementsWatcher.checkRequirements(RequirementsWatcher.java:4)
    at com.google.android.exoplayer2.scheduler.RequirementsWatcher.access$200(RequirementsWatcher.java:1)
    at com.google.android.exoplayer2.scheduler.RequirementsWatcher$NetworkCallback.lambda$onNetworkCallback$0(RequirementsWatcher.java:2)
    at com.google.android.exoplayer2.scheduler.-$$Lambda$RequirementsWatcher$NetworkCallback$791AAbo2y2AoEG_LWG4fQV9-Ibc.run
    at android.os.Handler.handleCallback(Handler.java:888)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:213)
    at android.app.ActivityThread.main(ActivityThread.java:8169)
    at java.lang.reflect.Method.invoke(Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

[REQUIRED] Version of ExoPlayer being used

2.11.4

[REQUIRED] Device(s) and version(s) of Android being used

ONEPLUS A6000

Suggests

Can you wrap restartService()'s code in DownloadService.java with catching IllegalStateException to prevent from start-service-in-background crash, like below:

private void restartService() {
      if (foregroundAllowed) {
        Intent intent = getIntent(context, serviceClass, DownloadService.ACTION_RESTART);
        Util.startForegroundService(context, intent);
      } else {
        // The service is background only. Use ACTION_INIT rather than ACTION_RESTART because
        // ACTION_RESTART is handled as though KEY_FOREGROUND is set to true.
        try {
          Intent intent = getIntent(context, serviceClass, DownloadService.ACTION_INIT);
          context.startService(intent);
        } catch (IllegalArgumentException e) {
          // The process is classed as idle by the platform. Starting a background service is not
          // allowed in this state.
          Log.w(TAG, "Failed to restart DownloadService (process is idle).");
        } catch (IllegalStateException e) {
          // The app is in background, starting service is disallow
          Log.w(TAG, "Failed to restart DownloadService (app is in background, foregroundAllowed == false)")
        }
      }
    }
bug

Most helpful comment

Also facing this issue - thanks for fixing. Not to rush you guys or anything, but by any chance is there a planned ETA for this to make it into a release?

All 4 comments

I think IllegalArgumentException should just be IllegalStateException in the existing code. I can change that.

Thanks!

Also facing this issue - thanks for fixing. Not to rush you guys or anything, but by any chance is there a planned ETA for this to make it into a release?

It should be in 2.11.5, which will hopefully go out sometime next week.

Was this page helpful?
0 / 5 - 0 ratings