Android 权限申请框架介绍

tech2024-05-13  93

step1: 在app工程 配置如下代码 implementation 'pub.devrel:easypermissions:2.0.0' step2: 在Activity 中 implements EasyPermissions.PermissionCallbacks 中重写下面两个方法 @Override public void onPermissionsGranted(int requestCode, List<String> perms) { } @Override public void onPermissionsDenied(int requestCode, List<String> perms) { }

step3: 请求存储读写权限

public String[] getPermissions() { String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}; return permissions; } EasyPermissions.requestPermissions(this, "程序运行需要存储权限", RC_PERMISSIONS, getPermissions());

step4: 判断是否有权限

String[] permissions = getPermissions(); boolean hasPermissions = EasyPermissions.hasPermissions(this, permissions);

 

最新回复(0)