Search code examples
androidandroid-permissions

Permission to access device location in foreground only


My app uses location data in the foreground only. Because Android 10 distinguishes between location in the background and in the foreground, I want to set the permission in the manifest.xml as minimalistic as possible, as not to bother the user.

The release notes (https://developer.android.com/about/versions/10/privacy/changes) state the following: If I put the target SDK to Android 10 (29) and only request ACCESS_FINE_LOCATION in the manifest (purposely excluding the new ACCESS_BACKGROUND_LOCATION permission), I should only get foreground access (that's what I want).

In the grade file:

compileSdkVersion 29
    defaultConfig {
        applicationId "com.genewarrior.sunlocator"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 70
        versionName "3.10"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        renderscriptTargetApi 19
        renderscriptSupportModeEnabled true
    }

In the manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />

However, instead of the expected "Allow only while using the app", it's still possible to choose "Allow all the time" in the Location permission.

Also from time to time the notification "App got your location in the background" pops up, which is exactly what I want to avoid.

enter image description here


Solution

  • I think what you did is correct. You can request only location permission to "Allow only while using the app" for your app. On the request code you need to request only ACCESS_FINE_LOCATION permission, in this case:

    ActivityCompat.requestPermissions(thisActivity, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 1234)
    

    And put only ACCESS_FINE_LOCATION in the manifest.

    You are seeing "Allow all the time" in your app location settings. As suggested by @greywolf82 there is a possibility that ACCESS_BACKGROUND_LOCATION permission is included in some of your dependencies.

    enter image description here enter image description here