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,
)
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"
shouldShowRequestPermissionRationale()
is answer.lookthis If it is useful, please share your usage method – 卡尔斯路西法 Commented Jan 24 at 3:27shouldShowRequestPermissionRationale()
return false(in request permission result listener) maybe? – 卡尔斯路西法 Commented Feb 5 at 3:39