0

I'm working on upgrading a feature that uses location in the background and I want to have precise location always, not just for one time. This is an essential app feature so if the process closes, I want to have it again when the app starts instead of having to ask them for it again.

The Android 12 documentation here does not describe a way to do it

Blake
  • 77
  • 9
  • It does describe it and gives sample code. read the whole page. – javdromero Aug 20 '21 at 18:01
  • It doesn't say anything about permanent precise location though. If I request ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION, then click "Only this time," re-request permissions, it will say that both are granted even though "Use precise location" is only granted for the duration of the app process. I still don't see anything in the documentation about that and I've read it several times. – Blake Aug 20 '21 at 22:34
  • @Blake I hope you found your answer by now. I'm actually working on similar issue, so I'll just post my observations. In order to know if Precise location is granted, one needs to check if Permissions.ACCESS_FINE_LOCATION is allowed. If user has selected "only this time", the permissions is granted only for some time (while app is active, and couple of secs after going to background). The check will return "granted" in that case, but system settings shows the "precision" toggle to be at "off" state. Unfortunately, there is no way to know if it's temporary or user promoted to precise location. – Krzysztof Borowy Apr 12 '22 at 15:14
  • The way I solved it is by storing the numbers of times they denied permissions in shared prefs. We would have no way to know if they deny the permissions permanently. A user can click out of permissions prompt without clicking deny and that will count as a deny but won't toggle the shouldShowPermissionRationale. If they click deny then shouldShowPermissionsRationale will be true, if they deny after that then it's permanently denied and shouldShowPermissionsRationale is back to false where we're left in a weird state where we can't tell they permanently denied permissions unless we track it. – Blake Apr 13 '22 at 16:48

1 Answers1

0

To determine which permissions the system has granted to your app, check the return value of your permissions request

ActivityResultLauncher<String[]> locationPermissionRequest =
registerForActivityResult(new ActivityResultContracts
    .RequestMultiplePermissions(), result -> {
        Boolean fineLocationGranted = result.getOrDefault(
                Manifest.permission.ACCESS_FINE_LOCATION, false);
        Boolean coarseLocationGranted = result.getOrDefault(
                Manifest.permission.ACCESS_COARSE_LOCATION,false);
        if (fineLocationGranted != null && fineLocationGranted) {
            // Precise location access granted.
        } else if (coarseLocationGranted != null && coarseLocationGranted) {
            // Only approximate location access granted.
        } else {
            // No location access granted.
        }
    }
);
Subham Naik
  • 411
  • 5
  • 12