在 Jetpack Compose 动态申请权限可以使用两种方法
接下来分贝展示一下两种用法
- 代码环境如下:
- Kotlin 1.5.2
- Jetpack Compose 1.0.2
- Android Studio Chipmunk | 2021.2.1
要使用 Accompanist 需要额外引入
dependencies {
...
implementation "com.google.accompanist:accompanist-permissions:0.16.1"
...
}
1. rememberLauncherForActivityResult 方式
rememberLauncherForActivityResult 是基于 Activity 的 ResultAPI 获取动态权限。例如我们可以在新打开一个 Activity 时通过这种方式获取权限:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
RequestPermission()
}
}
}
@Composable
private fun RequestPermission() {
val context = LocalContext.current
val lifecycle = LocalLifecycleOwner.current.lifecycle
val permission = Manifest.permission.READ_EXTERNAL_STORAGE
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission(),
onResult = { isGranted ->
}
)
val lifecycleObserver = remember {
LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START) {
if (!permission.isGrantedPermission(context)) {
launcher.launch(permission)
}
}
}
}
DisposableEffect(lifecycle, lifecycleObserver) {
lifecycle.addObserver(lifecycleObserver)
onDispose {
lifecycle.removeObserver(lifecycleObserver)
}
}
}
private fun String.isGrantedPermission(context: Context): Boolean {
return context.checkSelfPermission(this) == PackageManager.PERMISSION_GRANTED
}
当进入 MainActivity 时请求权限的效果
2. Accompanist Permissions方式
Accompanist Permission 将权限申请结果以一个 Composable State 的形式返回,调用形式相较于第一种更加易用 。比如,我们通过该点击按钮动态申请权限
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
RequestPermissionUsingAccompanist()
}
}
}
@OptIn(ExperimentalPermissionsApi::class)
@Composable
private fun RequestPermissionUsingAccompanist() {
val permission = Manifest.permission.READ_EXTERNAL_STORAGE
val permissionState = rememberPermissionState(permission)
PermissionRequired(
permissionState = permissionState,
permissionNotAvailableContent = {
Text("Permission Denied.")
}, permissionNotGrantedContent = {
Button(onClick = { permissionState.launchPermissionRequest() }) {
Text("Request permission.")
}
}, content = {
Text("Permission Granted.")
}
)
}
代码非常清晰,PermissionRequired 接收几个 Composable lambda 的参数,分别对应权限申请后,不同处理下的回调:
permissionNotGrantedContent :尚未获取权限或者获取失败(点击【Deny】)permissionNotAvailableContent :点击【Deny & don’t ask again】时content : 获取成功(点击【Allow】)
|