Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 10 (Q) ACCESS_BACKGROUND_LOCATION permission

I need the user to check and set permission to ACCESS_BACKGROUND_LOCATION ("Allow all the time") without the option to "Allow only while using the app". My GPS Tracking app needs to continue accessing location even when the application is minimised.

I have already permissions working for ACCESS_FINE_LOCATION for Android 9.

AndroidManifest.xml--------------------------------------------------->

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
TrackingActivity.java (need to include ACCESS_BACKGROUND_LOCATION)---->

private void permissionCheck1() {
        int permissionCheck1 = ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION);
        if (permissionCheck1 != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        } else {
            grantedSetupGPSandMap();
        }
}

I need the only permission option to be "Allow all the time" if possible. How do I set this up when checking and setting permissions?

like image 512
Chris King Avatar asked Nov 12 '19 09:11

Chris King


2 Answers

For Android 10, if:

  • You omit ACCESS_BACKGROUND_LOCATION, and:
  • You use a foreground service with the android:foregroundServiceType="location" property set in the manifest, then:

The system will supply only the "Allow only while using the app" and "Deny" options to the user, in reference to the location permission.

I realize that's the exact opposite of what you asked for, but the point is, under this condition, "Allow only while using the app" effectively behaves like "Allow all the time", provided the aforementioned foreground service is running.

You will be able to receive location updates even when the app is offscreen. And the user does not have the option of only partially allowing location updates; they have to choose between fully allowing it, or fully denying it.

EDIT

This answer also assumes your targetSdkVersion is 29.

like image 157
greeble31 Avatar answered Oct 21 '22 10:10

greeble31


I added "ACCESS_BACKGROUND_LOCATION" permission in manifest but I am facing the same issue in Android 10 Device. "Allow all the time" option is not displaying in dialog.

enter image description here

Then I added "ACCESS_BACKGROUND_LOCATION" permission in permissions array to ask users to grant at runtime. Now all three options are displaying.

ActivityCompat.requestPermissions(this@MainActivity, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION), MY_PERMISSIONS_REQUEST_LOCATION)

enter image description here

like image 24
Dhrumil Shah Avatar answered Sep 29 '22 11:09

Dhrumil Shah