首先引入依赖
compile 'pub.devrel:easypermissions:0.3.0'
其次实现EasyPermissions.PermissionCallbacks接口
implements EasyPermissions.PermissionCallbacks
定义所需权限的数组
//权限标识
private static final int REQUEST_CODE_SAVE_IMG = 10;
//所需要的全部权限
String[] mPermissionList = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
实现接口中的方法
//授权结果
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
//跳转到onPermissionsGranted或者onPermissionsDenied去回调授权结果
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
//同意授权
@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
}
//拒绝授权
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
//打开系统设置,手动授权
new AppSettingsDialog.Builder(this).build().show();
}
}
调用方法:
if (EasyPermissions.hasPermissions(MainActivity.this, mPermissionList)) {
//已经同意过
具体的业务方法
} else {
//未同意过,或者说是拒绝了,再次申请权限
EasyPermissions.requestPermissions(
MainActivity.this, //上下文
"保存图片需要读取sd卡的权限", //提示文字
REQUEST_CODE_SAVE_IMG, //请求码
mPermissionList //权限列表
);
}