Vid*_*uri 5 android android-permissions android-camerax android-jetpack-compose jetpack-compose-accompanist
我想知道如何检测用户何时在伴奏者权限库中撤销权限(两次拒绝权限),我还检查了该库的 GitHub 存储库并且示例是旧的。
我在用,
compose_version = '1.2.0-alpha03'
accompanist_version = '0.24.2-alpha'
这是我的代码片段,
@ExperimentalMaterial3Api
@ExperimentalPermissionsApi
@Composable
fun CameraPermission() {
/* Camera permission state.*/
val cameraPermissionState = rememberPermissionState(permission = Manifest.permission.CAMERA)
val context = LocalContext.current
val intent =Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.fromParts("package", BuildConfig.APPLICATION_ID, null))
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 -> {
/*
* This is a rationale explaining why we need the camera permission.
* We are displaying this because the user has denied the permission once.
* */
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
* */
Column {
Text(text = "The camera is important for this app. Please grant the permission.")
Button(onClick = { cameraPermissionState.launchPermissionRequest() }) {
Text("Grant 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
* */
Column {
Text(text = "Camera permission required for this feature to be available. Please grant the permission")
Button(onClick = { cameraPermissionState.launchPermissionRequest() }) {
Text("Grant permission")
}
/*todo("permission denied twice.")*/
Text(text = "Camera permission denied twice. Please grant the permission")
Button(
onClick = {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
) {
Text("Open settings to grant permission.")
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
您需要启动 launchPermissionRequest 以获得足够的信息
var permissionAlreadyRequested by rememberSaveable {
mutableStateOf(false)
}
val cameraPermissionState = rememberPermissionState(permission = Manifest.permission.CAMERA) {
permissionAlreadyRequested = true
}
if (!permissionAlreadyRequested && !cameraPermissionState.shouldShowRationale) {
SideEffect {
cameraPermissionState.launchPermissionRequest()
}
} else if (cameraPermissionState.shouldShowRationale) {
ShowRationaleContent {
cameraPermissionState.launchPermissionRequest()
}
} else {
ShowOpenSettingsContent {
context.openSettings()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3276 次 |
| 最近记录: |