How to check if user chose "ask every time" for android permission

Viewed 59

I've been working lately on a method that checks and asks for location permission. I followed android's guideline for implementing user permissions but I also added some stuff to manage the case when the user has denied the permission forever.

so what I did is use the shouldShowRequestPermissionRationale() which return false if the user hasn't been asked for the permission or the user chose "don't show again" when asked for it, and return true if the user denied the permission on previous occasion, I'm also storing a boolean in the shared preferences to check if this is the first time the user has been asked for this permission. which make this method a reliable way to determine if permission were denied forever or not (on API <30). you can check the code below.

    when {
           ContextCompat.checkSelfPermission(
                requireContext(),
                Manifest.permission.ACCESS_FINE_LOCATION
            ) == PackageManager.PERMISSION_GRANTED -> {
              //location permission granted, get location info
            }               
           
           shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) -> {
               //user denied permission before but didn't chose "don't show again" option
               //show rationale dialog
            }
            !shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
                    && SharedPreferencesHelper.getInstance(
                requireContext()
            ).locationPermissionAskedBefore() -> {
               //user denied permission permanently, show dialog to head to app settings
            }
            else -> {
                //haven't requested the permission before, request permission
            }
      }

the problem is when user change the permission state to ask every time from the settings on android 11 or above, the shouldShowRequestPermissionRationale() will now return false, and since its not the first time asking for the permission, the boolean saved in the shared preferences will return true and so the "permission denied permanently" code will run and the user would be asked to head to the settings and to change the permission instead of actually asking for the permission from the app itself.

So, what can I do to distinguish between permission being denied permanently and using ask every time?

0 Answers
Related