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?
For Android 10, if:
ACCESS_BACKGROUND_LOCATION
, and: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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With