After upgrading my Android application to target targetSdkVersion 34 (Android 14), my app crashes when the user denies location permissions. The app uses WorkManager to update the location on the server every 20 seconds. The issue occurs when the app is opened from background to foreground, and location permissions are denied from the settings.
Here’s the stack trace of the crash:
Fatal Exception: java.lang.SecurityException: Starting FGS with type location callerApp=ProcessRecord{7499c86 16252:com.prasko.ovdriver/u0a304} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_LOCATION] any of the permissions allOf=false [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION] and the app must be in the eligible state/exemptions to access the foreground only permission
at android.os.Parcel.createExceptionOrNull(Parcel.java:3087)
at android.os.Parcel.createException(Parcel.java:3071)
at android.os.Parcel.readException(Parcel.java:3054)
at android.os.Parcel.readException(Parcel.java:2996)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6761)
at android.app.Service.startForeground(Service.java:862)
at androidx.work.impl.foreground.SystemForegroundService$Api31Impl.startForeground(SystemForegroundService.java:193)
at androidx.work.impl.foreground.SystemForegroundService$1.run(SystemForegroundService.java:129)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:257)
at android.os.Looper.loop(Looper.java:368)
at android.app.ActivityThread.main(ActivityThread.java:8839)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1049)
Manifest Permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="location|dataSync"
tools:ignore="Instantiatable"
tools:node="merge" />
The app crashes when SystemForegroundService.startForeground() is called because the location permissions are denied from setting. How can I handle this situation properly in Android 14 to prevent the crash?
Additional Information:
I check location permissions before starting WorkManager.
The crash occurs specifically when the app is brought to the foreground after location permissions have been denied from setting.
nitin kadukar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.