skip to content

On Android 12+, why does a dash-cam app's camera foreground service, started from a receiver when the car's Bluetooth connects, throw ForegroundServiceStartNotAllowedException, and what do you change?

level: seniorimportance: must knowfreq 62%

answer

  1. where is the app when it starts
  2. the Android 12 background-start rule
  3. a short list of exemptions
  4. camera is a while-in-use permission
  5. a notification tap passes both checks

basics

~20 s

Apps targeting Android 12 (API 31) cannot start foreground services from the background except in listed cases, and a Bluetooth broadcast is not one. Post a notification whose Start recording action starts the service, or start it from the visible app.

solid answer

~40 s

From Android 12 (API 31), an app targeting API 31 or higher that starts a foreground service while in the background gets `ForegroundServiceStartNotAllowedException` unless an exemption applies: a user-visible transition, a tap on a notification or widget, an exact alarm for a user-requested action, a high-priority FCM message, certain system broadcasts, companion-device permissions, disabled battery optimization. A Bluetooth broadcast is none of these. The camera adds a second rule: for apps targeting Android 14 (API 34), a `camera` service created in the background throws `SecurityException`, because `CAMERA` is a while-in-use permission, even under most exemptions, and `checkSelfPermission()` still reports it granted. So the receiver should post a notification with a Start recording action whose `PendingIntent` from `getForegroundService()` starts the service; a notification tap is exempt from both rules.

go deeper

for a junior

Know that modern Android refuses most foreground-service starts while the app is in the background, and that the exception names this.

for a middle

List the main exemptions, a notification or widget tap, a user-requested exact alarm, high-priority FCM, and tell them from triggers that are not exempt.

for a senior

Separate the Android 12 background-start rule from the Android 14 while-in-use check, explain why checkSelfPermission() misleads, and redesign the trigger around a user tap.

for a principal

Set a rule that camera, microphone and location services start only from user-visible moments, and review every automatic trigger against both lists before it ships.

## What the rule says Since **Android 12 (API 31)**, an app that targets API 31 or higher cannot start a foreground service while the app is in the background, except in a listed set of situations. Without an exemption the system throws **`ForegroundServiceStartNotAllowedException`**. The dash-cam app's receiver runs when the car's Bluetooth connects, typically with the phone in a pocket and no activity on screen, so the app is in the background and the start is refused. Being granted `CAMERA`, holding `FOREGROUND_SERVICE_CAMERA` or returning `START_STICKY` changes none of that. When the target is another app's service, the rule applies only if both apps target Android 12 or higher. ## The exemptions The Android guide lists the situations where a background app may still start a foreground service, among them: - The app is transitioning from a user-visible state, such as an activity. - The user acts on a UI element of the app: a **notification**, a widget, a bubble or an activity. - The app can start an activity from the background (except when its activity is only in an existing task's back stack). - An **exact alarm** fires to complete an action the user requested. - A **high-priority** Firebase Cloud Messaging message arrives; if the system downgrades it to normal priority, the start throws. - `ACTION_BOOT_COMPLETED`, `ACTION_LOCKED_BOOT_COMPLETED` or `ACTION_MY_PACKAGE_REPLACED` reaches a receiver (with type restrictions on Android 14 and 15), or a time, timezone or locale change does. - The app uses the Companion Device Manager with `REQUEST_COMPANION_START_FOREGROUND_SERVICES_FROM_BACKGROUND` or `REQUEST_COMPANION_RUN_IN_BACKGROUND`. - The user turned off battery optimizations for the app. - The app holds `SYSTEM_ALERT_WINDOW`; from Android 15 it must also have a visible overlay window. A broadcast about a Bluetooth connection is not on the list. ## The second wall: while-in-use permissions For the dash cam, finding an exemption is not enough. `CAMERA`, `RECORD_AUDIO` and the location permissions are **while-in-use** permissions: the app holds them only while it is in use. For apps targeting **Android 14 (API 34)**, the system checks a service's runtime prerequisites when the foreground service is created, so creating a `camera` service from the background throws **`SecurityException`** even when a background-start exemption applies. On earlier versions the service started but could not use the camera. Two details make this hard to see coming: - `checkSelfPermission(CAMERA)` returns `PERMISSION_GRANTED` in the background, because the grant is real; it just is not usable from there. Checking first does not predict the failure. - Only a narrower list lifts the while-in-use restriction: the service started by a system component, by interacting with a widget, **by interacting with a notification**, by a `PendingIntent` sent from a different visible app, by a device owner, by the voice interaction service, or by an app with `START_ACTIVITIES_FROM_BACKGROUND`. Battery-optimization exemption, exact alarms and the companion permissions are not on it. ## Fixing the dash-cam flow 1. **Stop starting the camera service from the receiver.** On the Bluetooth event, post a notification with a "Start recording" action instead. 2. **Start the service from that action.** The action's `PendingIntent`, created with `PendingIntent.getForegroundService()`, starts the service when the user taps it, which passes both the background-start rule and the while-in-use rule. Building the notification and its action is covered in the notifications lesson. 3. **Keep the in-app path.** Starting recording from the visible activity always works. 4. **Catch and degrade.** Wrap the start in a check for `ForegroundServiceStartNotAllowedException` (guarded by `Build.VERSION.SDK_INT >= Build.VERSION_CODES.S`, since the class is API 31) and fall back to the notification rather than crashing. ## Diagnosing it in the field - The crash names the exception; correlate it with where the start was called and whether an activity was visible. - A `SecurityException` from a camera, microphone or location service on Android 14 while the app was backgrounded is the while-in-use rule, not a missing manifest entry. - When a service started from the background runs with restricted access instead of failing, Logcat shows a line such as `Foreground service started from background can not have location/camera/microphone access`. - Restarts of a sticky foreground service by the system are not affected by the background-start rule; starts your code makes are. - Reproduce it deliberately: put the app in the background, trigger the receiver, and check both the exception type and the target SDK, because the same code behaves differently on an app targeting API 30, 31 and 34. - Audit every place that starts a foreground service, not only the crashing one: a boot receiver, a push handler and a job can each hit the same rule on different days.

  • Would holding REQUEST_COMPANION_START_FOREGROUND_SERVICES_FROM_BACKGROUND fix this dash cam?
    Only half of it. A companion-device app is exempt from the background-start rule, but the companion permissions are not on the while-in-use exemption list, so on Android 14 a `camera` service created in the background still throws `SecurityException`. It helps for types without a while-in-use prerequisite, such as `connectedDevice`.
  • Why doesn't checking checkSelfPermission(CAMERA) before the start prevent the SecurityException?
    For a while-in-use permission it returns `PERMISSION_GRANTED` even while the app is in the background, because the grant exists; it means 'granted while the app is in use'. The failure comes from where the start happens, so the guard has to be about visibility or an exempt trigger, not the grant.
  • Does a START_STICKY restart by the system count as a background start?
    No. The `Service` Javadoc states that the background-start restriction does not affect the system's restarts of a sticky foreground service. A start your own code makes while the app is backgrounded is still restricted, even for a service that returns `START_STICKY`.

  • Holding the CAMERA permission lets a camera foreground service start from any broadcast.
  • Turning off battery optimization also lifts the while-in-use camera restriction.
  • ForegroundServiceStartNotAllowedException only happens when battery saver is on.
  • Using startService() instead of startForegroundService() avoids the background limits.
  • checkSelfPermission(CAMERA) returning granted proves a background camera start will work.

Assumes targetSdk 31+ for the background-start rule (Android 12, API 31) and targetSdk 34+ for the while-in-use check when the service is created (Android 14, API 34); checked against the API 37 docs.