3

The new registerForActivityResult method makes it very simple to ask for permission, as follows:

registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
    when {
        granted -> getUserLocation()
        else -> {
            //permission denied
        }
    }
}

However, how can I detect that the user has denied permission with the "never ask again" checkbox or in Android 11+ has denied permission permanently? In these scenarios, I want to redirect the user to the device's Settings screen?

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
Rahul Pawar
  • 403
  • 3
  • 13
  • Does this answer your question? [How to know user check "Never ask again" before calling requestPermissions](https://stackoverflow.com/questions/34573627/how-to-know-user-check-never-ask-again-before-calling-requestpermissions) – Omar Shawky Aug 16 '21 at 19:41

2 Answers2

1

Sadly, the Android APIs do not allow us to detect when the user has denied the permission via the "Never ask again" checkbox in older Android versions or denied the permission twice (and therefore permanently) in newer Android versions (i.e. Android 11+ devices).

Focusing on Android 11+, the reason why the above is not possible is because the Android APIs do not allow us to distinguish between the following two cases:

  1. The user has never denied the permission request.
  2. The user has denied the permission request twice.

In both cases, the shouldShowRequestPermissionRationale(permission:) method returns false and the checkSelfPermission(permission:) method returns PackageManager.PERMISSION_DENIED.

With a bit of ingenuity, we can work around the Android API limitations and allow ourselves to differentiate between the above two cases. See my answer here for an example of how to do so.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
0

I would suggest treating the first deny as "Deny Forever" because the first deny can be checked by shouldShowRequestPermissionRationale(). After that you should show your own dialog and take the user to the permission page.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
Sam Chen
  • 7,597
  • 2
  • 40
  • 73
  • 1
    In my opinion, we should only resort to directing the user to the Settings screen after the user has seen the "request permission rationale" and denied the permission for a second time. For some guidance on how to achieve this, see my answer [here](https://stackoverflow.com/a/73826849/1071320). – Adil Hussain Sep 23 '22 at 11:25