【问题标题】:Is there a callback function for launchPermission in the Accompanist library伴奏库中是否有launchPermission的回调函数
【发布时间】:2022-08-18 17:09:46
【问题描述】:

Accompanist Permissions 库中执行permissionState.launchPermissionRequest() 后有回调函数吗?我只想在完成后执行一些代码。

    标签: android callback android-jetpack-compose android-permissions jetpack-compose-accompanist


    【解决方案1】:

    我从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.GrantedPermissionStatus.Denied

    【讨论】:

      【解决方案2】:

      更直接的方法是使用rememberMultiplePermissionsStaterememberPermissionStateonPermissionsResult 回调函数,如API documentation 中所述,如下所示:

      val multiplePermissionsState = rememberMultiplePermissionsState(permissions = permissions) {            
        //Do post permissions requests operations here even if one or more permissions were denied.
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-11
        • 1970-01-01
        • 2014-03-28
        • 1970-01-01
        • 2022-11-02
        • 2019-12-15
        相关资源
        最近更新 更多