gpt4 book ai didi

android - 如何识别用户是否在 Android 上的权限对话框之外单击

转载 作者:行者123 更新时间:2023-12-05 08:04:24 26 4
gpt4 key购买 nike

自从在 Android 11 ( https://developer.android.com/about/versions/11/privacy/location ) 上引入新的位置权限更改后,需要更加谨慎地处理权限。我们现在只能两次请求后台位置权限

我想根据用户的位置权限状态向用户显示一个对话框。

问题是系统处理对话框外的点击与拒绝权限相同,但它显然不计入 PermissionsUtil.shouldShowRequestStoragePermissionRationale 限制。因此很难区分用户处于哪种状态。

我最初的问题是:如何处理这种特定情况?

但我想更有用的是一般性问题:
如何识别用户是在系统权限对话框之外按下还是直接拒绝了权限?

最佳答案

我是这样解决的:

private val btRequestPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
val allPermissionsGranted = permissions.values.none { !it }
if (allPermissionsGranted) {
callback?.invoke().apply { this@PermissionActivity.callback = null }
} else if (Manifest.permission.BLUETOOTH_CONNECT in requiredPermissions || Manifest.permission.ACCESS_FINE_LOCATION in requiredPermissions) {
if (!shouldShowRequestPermissionRationale(Manifest.permission.BLUETOOTH_CONNECT) && !shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) && (this.getPreferenceBoolean("NEVER_SHOW_AGAIN", false) || shouldShowRationaleBefore)) {
this.getPreferenceBoolean("NEVER_SHOW_AGAIN", true)
//A dialog box with GO TO SETTING button
} else {
shouldShowRationaleBefore = true
requestBluetoothPermissions()
}
} else
requestBluetoothPermissions()

}


private var shouldShowRationaleBefore = false
protected fun requestBluetoothPermissions(callBack: (() -> Unit)? = null) {
this.callback = callBack

when {
areAllPermissionsGranted() -> {
callBack?.invoke()
}
shouldShowRequestPermissionRationale(Manifest.permission.BLUETOOTH_CONNECT) -> {
//A dialog box with CONTINUE button and callBack to btRequestPermissionLauncher.launch(...)
}
shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) -> {
//A dialog box with CONTINUE button and callBack to btRequestPermissionLauncher.launch(...)
}
!areAllPermissionsGranted() -> {
if (!shouldShowRationaleBefore)
btRequestPermissionLauncher.launch(requiredPermissions)
else {
private var shouldShowRationale = false
//A dialog box with CONTINUE button and callBack to btRequestPermissionLauncher.launch(...)
//This is where the click outside the permission dialog box is handled
}
}

}
}

关于android - 如何识别用户是否在 Android 上的权限对话框之外单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69103982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com