App not sending location from Foreground service on Android 10

Viewed 1598

My app is targetting API 28. I have a foreground service sending location updates.

The app stops sending locations when the following conditions are met:

  • Running on Android 10
  • App is in background
  • Location Permissions were selected as "Allow only while using the app"

On running

adb shell dumpsys package packageName

I see that

android.permission.ACCESS_BACKGROUND_LOCATION: granted=false

From the documentation, it appears that the "android.permission.ACCESS_BACKGROUND_LOCATION" permission was backward compatible.

How do I fix this issue? Is there a way to disable the tri-state permission request?

1 Answers

Per https://developer.android.com/about/versions/10/privacy/changes#app-access-device-location

An app is considered to be accessing location in the background unless one of the following conditions is satisfied:

  • An activity belonging to the app is visible.
  • The app is running a foreground service that has declared a foreground service type of location.

You're using a foreground service to get location updates but (I assume) you haven't told the OS that, so it believes you're accessing the location in the background. All you need to do is declare that your foreground service processes location.

<service android:name="com.example.MyForegroundService"
         android:foregroundServiceType="location"/>

Now when your service is running, the OS treats your app as being in the foreground for the purposes of auditing location access - and you don't need ACCESS_BACKGROUND_LOCATION.

Related