Android 申请权限的最新方式

需要添加新的库

def activity_version = "1.2.1"
implementation "androidx.activity:activity:$activity_version"
implementation 'androidx.fragment:fragment:1.3.0-beta01'

首先在Activity注册回调

private final ActivityResultLauncher<String> requestPermissionLauncher =
            registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
                if (isGranted) {
                    SDKRoute.pickers(this);
                } else {
                    Toast.makeText(this, "您需要开启读取外部存储权限", Toast.LENGTH_SHORT).show();
                }
            });

检测是否有权限,如果没有权限用新的方式去申请权限

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            SDKRoute.pickers(this);
        } else {
            requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
        }
0 0 投票数
文章评分
订阅评论
提醒
guest
0 评论
内联反馈
查看所有评论
京ICP备17066706号-1
0
希望看到您的想法,请发表评论。x
()
x