ExoPlayer icon indicating copy to clipboard operation
ExoPlayer copied to clipboard

Error while trying to use Downloadservice for background download on android 12

Open jhnaiem opened this issue 3 years ago • 3 comments
trafficstars

ExoPlayer Version

2.16.1

Devices that reproduce the issue

Pixel 4A

Devices that do not reproduce the issue

No response

Reproducible in the demo app?

Not tested

Reproduction steps

Tried to use the DownloadService for downloading content while the app is in the background using push.

Expected result

It works in all the android versions except android 12

Actual result

android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service

Media

Any content .

Bug Report

  • [ ] You will email the zip file produced by adb bugreport to [email protected] after filing this issue.

jhnaiem avatar Jul 26 '22 05:07 jhnaiem

Android 12 introduces new restrictions on starting a foreground service from the background.

ExoPlayer's DownloadService keeps itself in the foreground while waiting for requirement to be met (e.g. internet connection), but you can't start it from the background from Android 12 onwards.

tonihei avatar Jul 26 '22 08:07 tonihei

Android 12 introduces new restrictions on starting a foreground service from the background.

ExoPlayer's DownloadService keeps itself in the foreground while waiting for requirement to be met (e.g. internet connection), but you can't start it from the background from Android 12 onwards.

@tonihei, Thank you for the reply. So, what would be your suggestion in case I want to use ExoPlayer for background download?

ghost avatar Jul 26 '22 09:07 ghost

It depends what action starts your download and whether it falls under the exceptions that are allowed to start a foreground service from the background:

  • If the download is started by a user action, you should already be in a foreground service / activity and there shouldn't be an issue starting the download service.
  • If the download is triggered by an action external to the device (e.g. a user asking to download something on another device) you could use a high-priority Firebase Cloud Message to start the service. This is allowed per the exceptions linked above.
  • If the download should be scheduled based on timing (e.g. download latest episodes once a day), you essentially need to find one of the exceptions to start the download. For example, Firebase Cloud Messages are an option again, exact alarms should work, or you could prompt your users to disable battery optimizations (which they may want to grant if you explain the reason is to download in the background).

tonihei avatar Aug 03 '22 13:08 tonihei

Closing because I don't think there is anything else we can do here.

tonihei avatar Sep 06 '22 09:09 tonihei