0

On Android 12 devices (API 31),

User has GRANTED both the precise and approximate permissions and app is working fine post that.
When user turns off the device location by going into settings, I see the ACCESS_FINE_LOCATION permission as NOT GRANTED.
When checked by navigating into settings, we see precise location is turned on and location is set to allow all the time. Still the ACCESS_FINE_LOCATION is returned as NOT GRANTED.
In this scenario below code returns false -

override fun hasLocationPermission(): Boolean {
    return PermissionChecker.checkSelfPermission(
        mContext,
        android.Manifest.permission.ACCESS_FINE_LOCATION
    ) === PermissionChecker.PERMISSION_GRANTED
}

So if user turns off device location then FORGROUND LOCATION PERMISSION IS NOT GRANTED. is this the expected behaviour in Android 12?
Please assist.

Thales Isidoro
  • 1,753
  • 1
  • 5
  • 19
Amrut
  • 2,655
  • 3
  • 24
  • 44
  • Is it returns true when a user manually again clicks on the location Icon from the setting? – M DEV Jul 01 '22 at 12:52
  • Yes. It return true again when location is turned on – Amrut Jul 01 '22 at 13:16
  • see android 12 updates doc. You can not get permission to access the location if the location is turned off by a user. I also wanted to do local payment by turning off the location on Google Pay, but then google pay started requesting permission again even though it got permission of the location. – M DEV Jul 01 '22 at 13:44
  • can you share the doc link where it's mentioned. – Amrut Jul 01 '22 at 14:04
  • I just suggest you to check location permission for android 12 doc for more info. I just told an example of my test. Check this answer https://stackoverflow.com/a/70437959/16765223 and try. If the answer not work then try to request `background location service`. This service access location for all time. See doc https://developer.android.com/training/location/permissions#background-dialog-target-sdk-version – M DEV Jul 01 '22 at 16:53

1 Answers1

2

I think that PermissionChecker returns PERMISSION_DENIED_APP_OP. PermissionChecker can return three result: PERMISSION_DENIED, PERMISSION_GRANTED or PERMISSION_DENIED_APP_OP. If you want result PERMISSION_GRANTED, try to use ContextCompat.

override fun hasLocationPermission(): Boolean {
return ContextCompat.checkSelfPermission(
    mContext,
    android.Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED }