如何在 Compose 应用程序中请求权限?

How to request permissions in Compose application?

我试图了解如何在 Jetpack Compose 中请求权限。找到了an article in official documentation, but I couldn't figure out how to use it in my case; there's also ,但我就是看不懂。

如果您能展示一些带有解释的示例,或者帮助我理解我提到的答案中的代码,我将不胜感激。

对于我来说,这很简单,我只是制作了一个可组合项并在我的 MainActivity 中调用它,在 setContent 中:

 checkNotificationPolicyAccess(notificationManager, this)

基本上,如果没有授予权限,我会显示一个对话框。

@Composable
fun checkNotificationPolicyAccess(
    notificationManager: NotificationManager,
    context: Context
): Boolean {
    if (notificationManager.isNotificationPolicyAccessGranted) {
        return true
    } else {
        NotificationPolicyPermissionDialog(context)
    }
    return false
}

好的,我明白了。

您需要实现此可组合项,并在需要使用需要权限的功能时使用它:您传入一个 permissions 数组,一个 requestCode ( any Int),以及两个具有可组合项的 lambda:onGranted,在授予权限时使用,在另一种情况下 onDenied 可组合。

@Composable
fun PermissionsRequest(
    permissions: Array<out String>,
    requestCode: Int,
    onGranted: @Composable () -> Unit,
    onDenied: @Composable () -> Unit,
    onDeniedPermanently: (@Composable () -> Unit)? = null,
    rational: (@Composable () -> Unit)? = null,
    awaitResult: (@Composable () -> Unit)? = null,
) {
    val permissionHandler = AmbientPermissionHandler.current
    val (permissionResult, setPermissionResult) = remember(permissions) {
        mutableStateOf<PermissionResult?>(null)
    }

    LaunchedEffect(Unit) {
        setPermissionResult(permissionHandler.requestPermissions(requestCode, permissions))
    }

    when (permissionResult) {
        is PermissionResult.PermissionGranted -> onGranted()
        is PermissionResult.PermissionDenied -> onDenied()
        is PermissionResult.PermissionDeniedPermanently -> onDeniedPermanently?.invoke()
        is PermissionResult.ShowRational -> rational?.invoke()
        null -> awaitResult?.invoke()
    }
}

您还需要实施环境。据我了解,它用于将值传递给可组合项子项。在我们的例子中,AmbientPermissionHandler 将连同它的值 — PermissionHandler — 传递给来自 Providers 可组合项的 PermissionsRequest

val AmbientPermissionHandler = ambientOf<PermissionHandler>()

将作为 AmbientPermissionHandler 传递给 PermissionRequestPermissionHandler 实现使用 Providers.

class PermissionHandler(private val context: AppCompatActivity) {
    suspend fun requestPermissions(
        requestCode: Int,
        permissions: Array<out String>
    ): PermissionResult {
        return PermissionManager.requestPermissions(context, requestCode, *permissions)
    }
}

然后你像这样使用它:

class MainActivity : AppCompatActivity() {
    private val permissionHandler = PermissionHandler(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            Providers(
                AmbientPermissionHandler provides permissionHandler
            ) {
                PermissionsRequest(
                    permissions = arrayOf(Manifest.permission.READ_SMS),
                    requestCode = PERMISSION_REQUEST_CODE,
                    onGranted = { /* Here goes the composables when the permission is granted */ },
                    onDenied = { /* Is used when the permission is denied */ }
                )
            }
        }
    }
}

MainActivity中初始化PermissionHandler,然后在setContent.

中的Providers中提供

要使用 PermissionManagerLaunchedEffect,您需要这些依赖项:

implementation 'com.sagar:coroutinespermission:2.0.3'
implementation 'androidx.compose.runtime:runtime:1.0.0-alpha11'

感谢2jan222 for the