• Android Compose 权限请求


    Android Compose 权限请求

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

    全部代码见Github Shanyaliux/ComposeDemo (github.com)

    实现

    权限请求

    • 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)
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    • 请求权限,改写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)
                      }
                  }
              }
          }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15

      ::: warning 注意

      别忘了在Manifest文件中先声明权限

      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
      
      • 1

      :::

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

    @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()
    
        )
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    Demo

    完整Demo代码见GitHub:ComposeDemo/app/src/main/java/cn/shanyaliux/composedemo/permission at master · Shanyaliux/ComposeDemo (github.com)

    截图

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YkMp9fiM-1660316455418)(https://cdn.jsdelivr.net/gh/Shanyaliux/PicBed/img/image-20220812224844049.png)]

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VTSOxJGL-1660316455420)(https://cdn.jsdelivr.net/gh/Shanyaliux/PicBed/img/image-20220812225142494.png)]

    参考

    https://icode.best/i/68552745363490

  • 相关阅读:
    JAVA随机数真的随机吗?
    C++实战-Linux多线程(入门到精通)
    day49【动态规划】买卖股票的最佳时机问题
    Error could not open `Ejdklibamd64jvm.cfg‘问题解决
    C++下基于竞拍算法解决无人机任务分配问题
    从北京到南京:偶数在能源行业的数据迁移实践
    俄罗斯方块
    java虚拟机详解篇四(类的生命周期)
    06【数据库的约束】
    python计算点到面的距离
  • 原文地址:https://blog.csdn.net/qq_41121080/article/details/126312762