【发布时间】:2022-08-18 17:09:46
【问题描述】:
Accompanist Permissions 库中执行permissionState.launchPermissionRequest() 后有回调函数吗?我只想在完成后执行一些代码。
标签: android callback android-jetpack-compose android-permissions jetpack-compose-accompanist
Accompanist Permissions 库中执行permissionState.launchPermissionRequest() 后有回调函数吗?我只想在完成后执行一些代码。
标签: android callback android-jetpack-compose android-permissions jetpack-compose-accompanist
我从their guide 复制这个例子:
val cameraPermissionState = rememberPermissionState(
android.Manifest.permission.CAMERA
)
when (cameraPermissionState.status) {
// If the camera permission is granted, then show screen with the feature enabled
PermissionStatus.Granted -> {
Text("Camera permission Granted")
}
is PermissionStatus.Denied -> {
Column {
val textToShow = if (cameraPermissionState.status.shouldShowRationale) {
// If the user has denied the permission but the rationale can be shown,
// then gently explain why the app requires this permission
"The camera is important for this app. Please grant the permission."
} else {
// If it's the first time the user lands on this feature, or the user
// doesn't want to be asked again for this permission, explain that the
// permission is required
"Camera permission required for this feature to be available. " +
"Please grant the permission"
}
Text(textToShow)
Button(onClick = { cameraPermissionState.launchPermissionRequest() }) {
Text("Request permission")
}
}
}
}
2 个回调是:PermissionStatus.Granted 和 PermissionStatus.Denied
【讨论】:
更直接的方法是使用rememberMultiplePermissionsState 或rememberPermissionState 的onPermissionsResult 回调函数,如API documentation 中所述,如下所示:
val multiplePermissionsState = rememberMultiplePermissionsState(permissions = permissions) {
//Do post permissions requests operations here even if one or more permissions were denied.
}
【讨论】: