Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not allowed to start service, app is in background #7306

Closed
tuesda opened this issue Apr 28, 2020 · 4 comments
Closed

Not allowed to start service, app is in background #7306

tuesda opened this issue Apr 28, 2020 · 4 comments
Assignees
Labels

Comments

@tuesda
Copy link

tuesda commented Apr 28, 2020

[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)")
        }
      }
    }
@ojw28
Copy link
Contributor

ojw28 commented Apr 30, 2020

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

ojw28 added a commit that referenced this issue May 1, 2020
Issue: #7306
PiperOrigin-RevId: 309392633
@ojw28 ojw28 closed this as completed May 3, 2020
@tuesda
Copy link
Author

tuesda commented May 7, 2020

Thanks!

@frushlabo
Copy link

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?

@ojw28
Copy link
Contributor

ojw28 commented May 27, 2020

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

ojw28 added a commit that referenced this issue May 28, 2020
Issue: #7306
PiperOrigin-RevId: 309392633
andrewlewis pushed a commit that referenced this issue Jun 1, 2020
Issue: #7306
PiperOrigin-RevId: 309392633
@google google locked and limited conversation to collaborators Jul 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants