type
status
date
slug
summary
tags
category
icon
password

Android Compose 权限请求

在Compose中以往的权限请求方式就不再适用了。因此我在使用过程中发现了一种非常简单优雅的权限申请方法。

实现

权限请求

  • Activity中添加一下请求方法
    • private fun requestPermissions(permissions: Array<String>, onResult: (List<String>) -> Unit) {        registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result ->            val failed = result.filter { !it.value }.keys            onResult(failed.toList())       }.launch(permissions)   }
  • 请求权限,改写onCreate的部分代码如下
    • override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        val requestList =            arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)        requestPermissions(requestList) {            setContent {                if (it.isEmpty()) {                    Text(text = "已获取全部权限.")               } else {                    RequestFailedDialog(it)               }           }       }   }
      ::: warning 注意
      别忘了在Manifest文件中先声明权限
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
      :::

请求失败对话框提示(可选)

@Composable fun RequestFailedDialog(permissions: List<String>) {    val activity = LocalContext.current as? Activity    AlertDialog(onDismissRequest = { },        buttons = {            Row {                Button(                    onClick = {                        activity?.finish()                   },                    modifier = Modifier.weight(1f,true),                    shape = RoundedCornerShape(bottomStart = 8.dp),                    colors = ButtonDefaults.buttonColors(backgroundColor = Color.White),               ) {                    Text(text = "取消")               }                Button(                    onClick = {                        val intent = Intent()                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)                        intent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"                        intent.data = Uri.fromParts("package", activity?.packageName, null)                        activity?.startActivity(intent)                   },                    modifier = Modifier.weight(1f,true),                    shape = RoundedCornerShape(bottomEnd = 8.dp),                    colors = ButtonDefaults.buttonColors(backgroundColor = Color.White),               ) {                    Text(text = "确定")               }           }       },        title = {            Text(text = "提示")       },        text = {            Surface {                LazyColumn() {                    items(permissions) { permission ->                        Text(text = permission)                   }               }           }       },        shape = RoundedCornerShape(8.dp),        backgroundColor = Color.White,        contentColor = Color.Black,        properties = DialogProperties()   ) }

Demo

截图

notion image
notion image

参考

 
 
Python执行终端命令AndroidCompose使用Dialog

Ghlerrix
Ghlerrix
山高水长,怕什么来不及,慌什么到不了。
公告
type
status
date
slug
summary
tags
category
icon
password
这是新版的博客,想要访问旧版页面请使用以下链接。