美文网首页
运行时权限标准化实例

运行时权限标准化实例

作者: 济公大将 | 来源:发表于2022-03-27 10:57 被阅读0次
声明权限
<uses-permission android:name="android.permission.CAMERA"/>
注册启动器
//注册权限回调,它处理用户对系统权限对话框的响应。
//保存返回值,ActivityResultLauncher的一个实例。你可以使用val,
//如这段代码所示,也可以在onAttach()或onCreate()方法中使用lateinit var。
val requestPermissionLauncher =
    registerForActivityResult(RequestPermission()
    ) { isGranted: Boolean ->
        if (isGranted) {
            // 授予许可。继续你的应用程序中的动作或工作流程
        } else {
           //向用户解释该特性不可用,因为该特性需要用户拒绝的权限。
           //同时,尊重用户的决定。不要为了说服用户改变他们的决定而链接系统设置。
        }
    }
向用户请求权限的建议流程
when {
    ContextCompat.checkSelfPermission(
            CONTEXT,
            Manifest.permission.REQUESTED_PERMISSION
            ) == PackageManager.PERMISSION_GRANTED -> {
        // 您可以使用需要该权限的API。
    }
    shouldShowRequestPermissionRationale(...) -> {
        //在教育性UI中,向用户解释为什么你的应用程序需要这个权限才能让某个特性按照预期的方式运行。
        //在这个UI中,包含一个“取消”或“不感谢”按钮,允许用户在没有授权的情况下继续使用你的应用。
        showInContextUI(...)
    }
    else -> {
        //你可以直接请求许可,注册的ActivityResultCallback获取此请求的结果。
        requestPermissionLauncher.launch(
                Manifest.permission.REQUESTED_PERMISSION)
    }
}
自行管理请求的实例
when {
    ContextCompat.checkSelfPermission(
            CONTEXT,
            Manifest.permission.REQUESTED_PERMISSION
            ) == PackageManager.PERMISSION_GRANTED -> {
        // You can use the API that requires the permission.
        performAction(...)
    }
    shouldShowRequestPermissionRationale(...) -> {
        // In an educational UI, explain to the user why your app requires this
        // permission for a specific feature to behave as expected. In this UI,
        // include a "cancel" or "no thanks" button that allows the user to
        // continue using your app without granting the permission.
        showInContextUI(...)
    }
    else -> {
        // You can directly ask for the permission.
        requestPermissions(CONTEXT,
                arrayOf(Manifest.permission.REQUESTED_PERMISSION),
                REQUEST_CODE)
    }
}
override fun onRequestPermissionsResult(requestCode: Int,
        permissions: Array<String>, grantResults: IntArray) {
    when (requestCode) {
        PERMISSION_REQUEST_CODE -> {
            // If request is cancelled, the result arrays are empty.
            if ((grantResults.isNotEmpty() &&
                    grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                // Permission is granted. Continue the action or workflow
                // in your app.
            } else {
                // Explain to the user that the feature is unavailable because
                // the features requires a permission that the user has denied.
                // At the same time, respect the user's decision. Don't link to
                // system settings in an effort to convince the user to change
                // their decision.
            }
            return
        }

        // Add other 'when' lines to check for other
        // permissions this app might request.
        else -> {
            // Ignore all other requests.
        }
    }
}

相关文章

  • 运行时权限标准化实例

    声明权限 注册启动器 向用户请求权限的建议流程 自行管理请求的实例

  • 3.安全认证

    3.1 MongoDB的用户和角色权限简介 默认情况下,MongoDB实例启动运行时是没有启用用户访问权限控制的,...

  • Android - base - 运行时权限获取

    Android 6.0 引入的运行时权限机制 大纲 运行时权限机制简介 在程序运行时申请权限 #运行时权限机制简介...

  • 【Susen】目录

    Android运行时权限Android运行时权限列表EasyPermissionsAndPermissionPer...

  • 运行时权限

    1.运行时权限  以下权限需要进行运行时权限处理:  以CALL_PHONE为例,进行运行时权限申请:   第一步...

  • 无标题文章

    Android 6.0 运行时权限处理 标签(空格分隔): android 运行时权限介绍 Android 6.0...

  • 新特性与行为变更 -- 代码2

    运行时权限 运行时权限 在应用间共享文件 FileProvider 计划排定作业 JobIntentService...

  • MongoDB学习笔记(三)

    默认情况下,MongoDB实例启动运行时是没有启用用户访问权限控制的,也就是说,在实例本机服务器上都可以随意连接到...

  • Android优雅地申请动态权限

    Android6.0以上的系统中,引入了运行时权限检查,运行时权限分为正常权限和危险权限,当我们的App调用了需要...

  • Android 6.0变更一览

    Android 6.0 API 运行时权限 运行时申请权限,需要targetSDK>=23,并且手机的系统大于6....

网友评论

      本文标题:运行时权限标准化实例

      本文链接:https://www.haomeiwen.com/subject/xzoejrtx.html