On Android 14, what must a dash-cam app declare and call so its recording Service runs as a camera foreground service?
answer
- manifest entry plus two permissions
- a declared foregroundServiceType
- FOREGROUND_SERVICE_CAMERA beside CAMERA
- startForegroundService, then startForeground
- a few seconds, or the app crashes
basics
~10 sDeclare the service with android:foregroundServiceType="camera", request FOREGROUND_SERVICE, FOREGROUND_SERVICE_CAMERA and the runtime CAMERA permission, start it with startForegroundService(), and call startForeground() with a notification and FOREGROUND_SERVICE_TYPE_CAMERA within seconds.
solid answer
~40 sApps targeting Android 14 (API 34) must give every foreground service a type. In the manifest the `<service>` declares `android:foregroundServiceType="camera"`, and the app requests `FOREGROUND_SERVICE`, `FOREGROUND_SERVICE_CAMERA` and the `CAMERA` runtime permission, which the user must have granted. A visible activity calls `startForegroundService()`; the service then calls `ServiceCompat.startForeground()` with a non-zero notification ID, the notification and `FOREGROUND_SERVICE_TYPE_CAMERA`, before any slow setup, because it has only a few seconds (five, per the services guide). Each slip throws something distinct: no manifest type gives `MissingForegroundServiceTypeException`, an undeclared type passed to the call gives `IllegalArgumentException`, a missing permission gives `SecurityException`, and a late promotion crashes with `ForegroundServiceDidNotStartInTimeException`.
code
xml · 12 lines<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" />
<application>
<service
android:name=".RecordingService"
android:foregroundServiceType="camera"
android:exported="false" />
</application>
</manifest>go deeper
Remember the two calls, startForegroundService() then startForeground(), and that the service must show a notification.
Name the manifest type, the FOREGROUND_SERVICE and FOREGROUND_SERVICE_CAMERA permissions, the CAMERA prerequisite, and the exception each omission throws on Android 14.
Promote before slow setup, pick the narrowest honest type, and check runtime permissions before starting so the service never throws mid-drive.
Treat each foreground-service type as a policy commitment the platform and the store audit, and keep a team inventory of which type every service declares and why.
## Two steps: start, then promote A **foreground service** is a started service the user is aware of: it shows a status-bar notification and the system is far less likely to kill it. Getting one is always two steps: 1. A component calls `Context.startForegroundService(intent)` (or `ContextCompat.startForegroundService()` on older minimum SDKs). This creates an ordinary service and tells the system it promises to promote itself. 2. The service calls `startForeground()`, normally from `onStartCommand()`, passing a notification ID (never `0`), the `Notification`, and the foreground service type. AndroidX's `ServiceCompat.startForeground(service, id, notification, foregroundServiceType)` handles the API-level differences in that last argument. The promise has a deadline. The troubleshooting guide says the service has **a few seconds** to call `startForeground()` (the services overview puts it at five seconds); otherwise the app crashes with `RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground()`. So promote first, then open the camera or do any slow setup. ## What Android 14 adds: a type and a matching permission For apps targeting **Android 14 (API 34)** or higher, every foreground service must declare what it does: - **In the manifest**, the `<service>` element gets `android:foregroundServiceType`, here `camera`; several types are joined with `|`, as in `camera|microphone`. - **Permissions**: the app requests `FOREGROUND_SERVICE` (required since Android 9) **and** the type-specific one, here `FOREGROUND_SERVICE_CAMERA`. - **Runtime prerequisites**: each type has one. For `camera` it is the `CAMERA` runtime permission, which the user must already have granted when the service is promoted. - **At promotion**, the type passed to `startForeground()` must be a subset of the manifest's types: `ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA`. You can pass fewer types first and call `startForeground()` again later to add one. ## What each mistake throws | Mistake (targetSdk 34+) | Result | |---|---| | No `foregroundServiceType` in the manifest | `MissingForegroundServiceTypeException` from `startForeground()` | | Type `none` in the manifest or the call | `InvalidForegroundServiceTypeException` | | Passing a type the manifest does not declare | `IllegalArgumentException` | | Missing `FOREGROUND_SERVICE_CAMERA` or the `CAMERA` grant | `SecurityException` | | No `startForeground()` within the deadline | `ForegroundServiceDidNotStartInTimeException` crash | | Starting it while the app is in the background | `ForegroundServiceStartNotAllowedException` (Android 12+) | ## Picking the type The type describes the work, and the system and Google Play hold you to it: apps targeting Android 14 also declare their types in the Play Console. A few the dash-cam app meets: - `camera`: keep using the camera from the background; prerequisite `CAMERA`. It is a while-in-use permission, so the service cannot be created while the app is in the background. - `microphone`: add it if the clip records sound; `FOREGROUND_SERVICE_MICROPHONE` plus `RECORD_AUDIO`. - `location`: for a GPS track; `FOREGROUND_SERVICE_LOCATION` plus coarse or fine location. - `dataSync`: uploads and downloads; no runtime prerequisite, but a time limit on Android 15. - `shortService`: about three minutes of critical work, no type-specific permission, no sticky restarts. ## Checking before you start Most of the exceptions above are avoidable with a short checklist run in the activity, before `startForegroundService()`: - **Confirm the runtime grant.** If `CAMERA` is not granted, request it first; the Android guide's own camera sample also checks inside the service and calls `stopSelf()` when the permission is missing, rather than letting the promotion throw. - **Start while visible.** Because `CAMERA` is a while-in-use permission, the start should come from the visible activity (or a notification or widget tap), not from a background trigger. - **Promote before working.** Build the notification and call `startForeground()` first in `onStartCommand()`, then open the camera, so slow hardware cannot miss the deadline. - **Add types as features turn on.** A service declared `camera|microphone` can promote with the camera type alone and call `startForeground()` again with both flags when the user enables audio. - **Stop cleanly.** When the drive ends, call `stopForeground()` with `STOP_FOREGROUND_REMOVE` (or `ServiceCompat.stopForeground()`) and `stopSelf()`. ## How the rules arrived 1. Android 8 (API 26) restricted background services and added `startForegroundService()`. 2. Android 9 (API 28) required the `FOREGROUND_SERVICE` permission. 3. Android 10 and 11 required the `location`, then `camera` and `microphone` types for services using them. 4. Android 12 (API 31) blocked most starts from the background. 5. Android 14 (API 34) made a type and its permission mandatory for every foreground service. 6. Android 15 (API 35) added time limits for `dataSync` and `mediaProcessing`. The notification the service shows (its channel, title and ongoing flag) is covered in the notifications lesson; here it only needs to exist when `startForeground()` runs.
- The dash cam now records sound as well. What changes?Declare `android:foregroundServiceType="camera|microphone"`, request `FOREGROUND_SERVICE_MICROPHONE` and the `RECORD_AUDIO` runtime permission, and pass both type flags to `startForeground()`. You may promote with the camera type alone and call `startForeground()` again with both flags when the user turns audio on, as long as both are declared in the manifest.
- Why promote before opening the camera rather than after?After `startForegroundService()` the service has only a few seconds to call `startForeground()`. Camera setup can be slow or fail, and if promotion waits for it the app crashes with `ForegroundServiceDidNotStartInTimeException`. Promote first, then do the work, and stop the service if the work cannot start.
- Is declaring the type in the manifest enough to get camera access?No. The type and `FOREGROUND_SERVICE_CAMERA` only allow the service to run as a camera foreground service. The `CAMERA` runtime permission still has to be granted by the user, and because it is a while-in-use permission the service must be created while the app is visible or under a narrow exemption.
- On Android 14, the FOREGROUND_SERVICE permission alone covers every service type.
- startForegroundService() itself makes the service a foreground service.
- startForeground() can wait until the camera has finished opening.
- Declaring the camera type in the manifest grants the CAMERA runtime permission.
- Any type constant can be passed to startForeground() whether declared or not.
Assumes targetSdk 34 or higher on Android 14+ (API 34), where a declared foregroundServiceType and its FOREGROUND_SERVICE_* permission became mandatory; checked against the API 37 sources.