Welcome to Android 12 Developer Preview! Please give us feedback early and often, and help us make Android 12 the best release yet!

Foreground service launch restrictions

Give feedback icon We'd love to hear your feedback about these restrictions. Take a short survey to tell us what you think. In particular, let us know what use cases in your app are affected by this change.

Apps that target Android 12 can no longer start foreground services while running in the background, except for a few special cases. If an app tries to start a foreground service while the app is running in the background, and the foreground service doesn't satisfy one of the exceptional cases, the system throws an IllegalStateException.

If your app is affected by this change, it's recommended that you migrate to using WorkManager. When Android 12 beta is released, WorkManager will become the recommended solution for starting higher-priority background tasks.

Work Manager 2.7.0 includes support for expedited jobs, which is a new type of job in Android 12. On Android 11 and lower, this version of WorkManager uses foreground services to provide backward compatibility.

To see a complete example of how Work Manager 2.7.0 uses expedited jobs, look through the WorkManagerSample on GitHub.

Expedited jobs

Expedited jobs, new in Android 12, allow apps to execute important tasks while giving the system better control over access to resources. These jobs have a set of characteristics somewhere in between a foreground service and a JobScheduler job:

  • They aren't affected by some of the system's power management restrictions, including Battery Saver and Doze.
  • The system runs them immediately, provided that the system's current workload makes it possible to do so.

Expedited jobs might be deferred

The system tries to execute a given expedited job as soon as possible after the job is invoked. However, as is the case with other types of jobs, the system might defer the start of new expedited jobs if there are too many jobs already running, or if system resources are running low.

In particular, the system defers the execution of expedited jobs when at least one of the following conditions occur:

  • The system load is too high.
  • The expedited job quota limit has been exceeded. Expedited jobs use a quota system that's based on the App Standby Buckets and limits the maximum execution time within a rolling time window. The quotas used for expedited jobs are more restrictive than the ones used for other types of background jobs.

Updates to WorkManager

Starting in WorkManager 2.7.0, your app can call setExpedited() to declare that a Worker should be expedited. This new API uses expedited jobs when running on Android 12, and the API uses foreground services on prior versions of Android.

To encourage developers to be intentional about when they request expedited work in their apps, and to better support the ability to extend the length of time that a task can run, the CoroutineWorker.setForeground() and ListenableWorker.setForegroundAsync() methods are deprecated. In particular, on devices that run Android 12, trying to call ListenableWorker.setForegroundAsync() results in an IllegalStateException. It's recommended that developers use setExpedited() instead.

Cases where foreground service launches from the background are allowed

In the following situations, your app can start foreground services even while your app is running in the background:

The dialog asks whether the user wants to let an app always run in the
    background. The dialog contains 2 buttons: Deny and Allow.
Figure 1. System dialog warning users that removing battery optimization could reduce device battery life.