Search code examples
androidandroid-permissionsandroid-locationforeground-service

Android Q - Background Location Permission needed for Foreground service?


The first point in this document says that Background Location permission is required if the foreground location service is started while the app is in foreground starting Android 11.

https://developer.android.com/guide/components/foreground-services#bg-access-restrictions

My use case is that a user taps on a button to start location tracking. Tapping that button starts a foreground service which puts a notification. Now, I want the app to continue tracking even after backgrounding. Until Android 10, background location permission wasn't required to accomplish this.

My question is that to support Android 11, do I need to start requesting background permission as well? Also, do I need to do the same for Android 10? Not finding any other reference on the internet to verify this. please let me know your thoughts.


Solution

  • According to the definition of background work

    An app is considered to be running in the background as long as each of the following conditions are satisfied:

    1. None of the app's activities are currently visible to the user.
    2. The app isn't running any foreground services that started while an activity from the app was visible to the user.

    Otherwise, the app is considered to be running in the foreground.

    In your case, you fit into the second point where you started a foreground service when an activity is visible to the user so technically your app is still considered to be in the foreground while the service notification is being shown even if the user navigates away from your app.

    The foreground service needs to have android:foregroundServiceType="location" in its manifest declaration if you are targeting Android 10 and above. See here.

    You don't necessarily need the ACCESS_BACKGROUND_LOCATION permission if you are fetching location updates in the foreground service that you have created since you are not technically accessing location while in the background. However, within your service, if you use any APIs that may require background location permission, such as Geofencing, then you will require the background permission to be added to the manifest and request and handle the permission accordingly.