kotlin - Android Location Permission "Ask every time" - Stack Overflow

admin2025-04-22  2

Is there a way of knowing either the user selected "Only this time" (in settings "Ask every time") rather than "Don't allow"?

I am dealing with location permissions with maps. I have a button to place the user on the map and depending on the state of the permission it:
a) ask every time- shows the native dialog

b) don't allow- carries the user to system settings

So far I am saving the value in dataStore and I compare it with the system settings value but there must be a better way

// local value is true and system settings false == Ask every time
val isAskEveryTime = localVariable && ContextCompat.checkSelfPermission(
    context,
    Manifest.permission.ACCESS_FINE_LOCATION == PackageManager.PERMISSION_DENIED,
)

Is there a way of knowing either the user selected "Only this time" (in settings "Ask every time") rather than "Don't allow"?

I am dealing with location permissions with maps. I have a button to place the user on the map and depending on the state of the permission it:
a) ask every time- shows the native dialog

b) don't allow- carries the user to system settings

So far I am saving the value in dataStore and I compare it with the system settings value but there must be a better way

// local value is true and system settings false == Ask every time
val isAskEveryTime = localVariable && ContextCompat.checkSelfPermission(
    context,
    Manifest.permission.ACCESS_FINE_LOCATION == PackageManager.PERMISSION_DENIED,
)
Share Improve this question edited Jan 21 at 15:01 tyg 16.8k4 gold badges39 silver badges49 bronze badges asked Jan 21 at 14:51 Anna BAnna B 559 bronze badges 3
  • Maybe,shouldShowRequestPermissionRationale() is answer.lookthis If it is useful, please share your usage method – 卡尔斯路西法 Commented Jan 24 at 3:27
  • Unfortunately shouldShowRequestPermissionRationale() doesn't return true when user chooses "ask every time" – Anna B Commented Jan 24 at 12:47
  • Request permission when return false(when you select "only this time"). show dialog when renturn true(when you select "deny") but i can't read state "Deny and never ask",you can carries user to system settings when permission deny and shouldShowRequestPermissionRationale() return false(in request permission result listener) maybe? – 卡尔斯路西法 Commented Feb 5 at 3:39
Add a comment  | 

1 Answer 1

Reset to default 0

This is My example Activity:

class MainActivity : AppCompatActivity(), ActivityResultCallback<Boolean> ...
private lateinit var permissionRequestLauncher: ActivityResultLauncher<String>

override fun onCreate(savedInstanceState: Bundle?) {

    ...

    permissionRequestLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission(), this)
    setContentView(binding.root)
    locationButton.setOnClickListener {
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "App granted Fine Location Permission", Toast.LENGTH_SHORT).show()
            return@setOnClickListener
        }
        requestLocationPermissions()
    }
}

private fun requestLocationPermissions() {
    val shouldShowDialog = shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
    if (shouldShowDialog) {
        MaterialAlertDialogBuilder(this).setMessage("Do you need location function?").setPositiveButton("confirm") { _, _ ->
            permissionRequestLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
        }.show()
    } else {
        permissionRequestLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
    }
}

override fun onActivityResult(result: Boolean) {
    val selectString = if (result) {
        "\"Allow only while using the app\" or \"Only this time\""
    } else {
        val shouldShowDialog = shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)
        if (shouldShowDialog) { // user select
            "\"Deny\""
        } else {
            "\"Deny and don't ask again\""
        }
    }
    Toast.makeText(this, "User select $selectString", Toast.LENGTH_SHORT).show()
}

no permission + should show dialog = deny
nopermission + should not show dialog = Deny and don't ask again

Extra: have ACCESS_BACKGROUND_LOCATION = "allow all time"

转载请注明原文地址:http://anycun.com/QandA/1745302466a90596.html