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
Recommended alternative to foreground services: WorkManager
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, 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
- 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
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
methods are deprecated. In particular, on devices that run
Android 12, trying to call
ListenableWorker.setForegroundAsync() results in an
It's recommended that developers use
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:
- Your app transitions from a user-visible state, such as an activity.
- Your app receives a high-priority message using Firebase Cloud Messaging.
- The user performs an action on a UI element related to your app. For example, they might interact with a bubble, notification, widget, or activity.
- Your app receives an event that's related to geofencing or activity recognition.
- After the device reboots and receives the
ACTION_BOOT_COMPLETEDintent action in a broadcast receiver.
- Apps with certain system roles or permission, such as device owners and profile owners.
- Your app uses the Companion Device Manager.
Your app has requested the
REQUEST_IGNORE_BATTERY_OPTIMIZATIONSpermission, and has directed the user to a settings page where the user has disabled battery optimizations for the app.
To send users to the Battery optimization page in system settings, invoke an intent that contains the
ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONSintent action. On that page, after users turn off battery optimization for your app, the system shows the dialog that appears in Figure 1.
Your app fulfills any of the conditions described as exceptions to restrictions on starting activities from the background, except for the case where the app has an activity in the back stack of an existing task.