Restrictions on starting a foreground service from the background

Apps that target Android 12 (API level 31) or higher can't start foreground services while the app is running in the background, except for a few special cases. If an app tries to start a foreground service while the app runs in the background, and the foreground service doesn't satisfy one of the exceptional cases, the system throws a ForegroundServiceStartNotAllowedException.

In addition, if an app wants to launch a foreground service that needs while-in-use permissions (for example, body sensor, camera, microphone, or location permissions), it cannot create the service while the app is in the background, even if the app falls into one of the exemptions from background start restrictions. The reason for this is explained in the section Restrictions on starting foreground services that need while-in-use permissions.

Exemptions from background start restrictions

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

Restrictions on starting foreground services that need while-in-use permissions

On Android 14 (API level 34) or higher, there are special situations to be aware of if you're starting a foreground service that needs while-in-use permissions.

If your app targets Android 14 or higher, the operating system checks when you create a foreground service to make sure your app has all the appropriate permissions for that service type. For example, when you create a foreground service of type microphone, the operating system verifies that your app currently has the RECORD_AUDIO permission. If you don't have that permission, the system throws a SecurityException.

For while-in-use permissions, this causes a potential problem. If your app has a while-in-use permission, it only has that permission while it's in the foreground. This means if your app is in the background, and it tries to create a foreground service of type camera, location, or microphone, the system sees that your app doesn't currently have the required permissions, and it throws a SecurityException.

Similarly, if your app is in the background and it creates a health service that needs the BODY_SENSORS permission, the app doesn't currently have that permission, and the system throws an exception. (This doesn't apply if it's a health service that needs different permissions, like ACTIVITY_RECOGNITION.) Calling PermissionChecker.checkSelfPermission() does not prevent this problem. If your app has a while-in-use permission, and it calls checkSelfPermission() to check if it has that permission, the method returns PERMISSION_GRANTED even if the app is in the background. When the method returns PERMISSION_GRANTED, it's saying "your app has this permission while the app is in use."

For this reason, if your foreground service needs a while-in-use permission, you must call Context.startForegroundService() or Context.bindService() while your app has a visible activity, unless the service falls into one of the defined exemptions.

Exemptions from restrictions on while-in-use permissions

In some situations, even if a foreground service is started while the app runs in the background, it can still access location, camera, and microphone information while the app runs in the foreground ("while-in-use").

In these same situations, if the service declares a foreground service type of location and is started by an app that has the ACCESS_BACKGROUND_LOCATION permission, this service can access location information all the time, even when the app runs in the background.

The following list contains these situations:

  • A system component starts the service.
  • The service starts by interacting with app widgets.
  • The service starts by interacting with a notification.
  • The service starts as a PendingIntent that is sent from a different, visible app.
  • The service starts by an app that is a device policy controller that runs in device owner mode.
  • The service starts by an app which provides the VoiceInteractionService.
  • The service starts by an app that has the START_ACTIVITIES_FROM_BACKGROUND privileged permission.

Determine which services are affected in your app

When testing your app, start its foreground services. If a started service has restricted access to location, microphone, and camera, the following message appears in Logcat:

Foreground service started from background can not have \
location/camera/microphone access: service SERVICE_NAME