Runtime permissions with Jetpack Compose
最終更新日:2024-07-10
To request runtime permissions in Jetpack Compose, use rememberLauncherForActivityResult
and ActivityResultContracts.RequestPermission()
.
@Composable
fun PermissionScreen() {
val request = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
// Granted!
} else {
// Failed..
}
}
Scaffold { contentPadding ->
Column(
modifier = Modifier.padding(contentPadding),
) {
Button(onClick = {
// Show dialog
request.launch(android.Manifest.permission.POST_NOTIFICATIONS)
}) {
Text("Request permissions")
}
}
}
}