Foreground Service Location Permission Enforcement Boundary on API 29+
0 reputation · 06 Jan 2021, 00:31 UTC
0 reputation · 06 Jan 2021, 00:31 UTC
Goal: start a foreground service with the location type on Android 10 (API 29) and later without triggering a SecurityException, while preserving the ability to launch the service from both foreground and background app states.
Constraint: apps targeting API 29+ must declare the normal-level FOREGROUND_SERVICE_LOCATION permission in the manifest when using a location-related foreground service type. The permission is granted at install time, but the exact point at which the platform validates it during the service lifecycle remains unclear.
Uncertainty: it is not documented whether the check occurs when calling startForegroundService(), when calling startForeground(), or only when the app is in the background. OEM-modified Android builds may also apply different enforcement timing or strictness compared with the AOSP baseline.
Questions:
FOREGROUND_SERVICE_LOCATION enforced at startForegroundService() or at startForeground()?35275 reputation · 06 Jan 2021, 01:52 UTC
On AOSP Android 10 (API 29) and later, FOREGROUND_SERVICE_LOCATION is enforced when the service is promoted to the foreground, not when startForegroundService() is called. The platform checks the manifest permission during startForeground(id, notification, FOREGROUND_SERVICE_TYPE_LOCATION). If the permission is missing, the rejection typically appears as a SecurityException at promotion time. The check is bound to the foreground service type, so it applies whether the app is visible or in the background.
android.permission.FOREGROUND_SERVICE_LOCATION in the manifest for a location-type foreground service.ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, or ACCESS_BACKGROUND_LOCATION.SecurityException when the system rejects the foreground service start or promotion.startForeground() promotion rather than at startForegroundService().android.permission.FOREGROUND_SERVICE_LOCATION in the manifest.android:foregroundServiceType='location' to the service component.ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION at runtime if your app needs actual location data.startForegroundService(), then immediately call the API 29+ overload of startForeground() that includes FOREGROUND_SERVICE_TYPE_LOCATION.startForeground(); the system may stop the service if promotion is not prompt.On an API 29+ device or emulator, start the service from both visible and background states and inspect logcat around system_server and ActivityManager for denial messages. Repeat on a different OEM device if you suspect manufacturer-specific strictness.
Missing diagnostic detail: share your targetSdkVersion. If it is below 29, this permission requirement may not apply in the same way, and the steps above change.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.