运行时权限标准化实例

声明权限
<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.
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容