一次初始化处处可用的链式编程动态权限请求库
- 链式编程
- 运行时权限申请
- 支持多个权限同时申请
- 支持特殊权限申请,如REQUEST_INSTALL_PACKAGES,SYSTEM_ALERT_WINDOW,ACCESS_NOTIFICATION_POLICY,WRITE_SETTINGS
- 最小支持android 14
- 多ROM多版本适配
implementation 'cn.junhua.android:permission-agent:<latest-release>'
//Application的onCreate中初始化
PermissionAgent.setDebug(BuildConfig.DEBUG);//开启debug
PermissionAgent.getInstance().init(this);
PermissionAgent.getInstance()
.request(Manifest.permission.CAMERA)
//.code(123)//与你自定义code冲突时可以设置,一般不用自己设置
.onGranted(new OnGrantedCallback<List<String>>() {
@Override
public void onGranted(List<String> permissions) {
//成功
}
})
.onDenied(new OnDeniedCallback<List<String>>() {
@Override
public void onDenied(List<String> permissions) {
//拒绝
}
})
.onRationale(new OnRationaleCallback<List<String>>() {
@Override
public void onRationale(List<String> permissions, AgentExecutor executor) {
//提示用户
executor.execute();//继续
executor.cancel();//取消
//do something
}
})
.apply();
PermissionAgent.getInstance()
.request(Manifest.permission.CAMERA, Manifest.permission.WRITE_CONTACTS)
//.code(123)//与你自定义code冲突时可以设置,一般不用自己设置
.onGranted(new OnGrantedCallback<List<String>>() {
@Override
public void onGranted(List<String> permissions) {
//成功
}
})
.onDenied(new OnDeniedCallback<List<String>>() {
@Override
public void onDenied(List<String> permissions) {
//拒绝
}
})
.onRationale(new OnRationaleCallback<List<String>>() {
@Override
public void onRationale(List<String> permissions, AgentExecutor executor) {
//提示用户
executor.execute();//继续
executor.cancel();//取消
//do something
}
})
.apply();
使用SpecialPermission
枚举选择需要申请的特殊权限,其他操作不变
PermissionAgent.getInstance()
.request(SpecialPermission.REQUEST_INSTALL_PACKAGES)
//.code(123)//与你自定义code冲突时可以设置,一般不用自己设置
.onGranted(new OnGrantedCallback<SpecialPermission>() {
@Override
public void onGranted(SpecialPermission permissions) {
//成功
}
})
.onDenied(new OnDeniedCallback<SpecialPermission>() {
@Override
public void onDenied(SpecialPermission permissions) {
//拒绝
}
})
.onRationale(new OnRationaleCallback<List<String>>() {
@Override
public void onRationale(List<String> permissions, AgentExecutor executor) {
//提示用户
executor.execute();//继续
executor.cancel();//取消
//do something
}
})
.apply();
权限检测的工具方法,供单独使用时调用。
//检测单个权限
PermissionAgent.getInstance().checkPermission(Manifest.permission.ACCESS_FINE_LOCATION)
//检测多个权限,如果存在权限没有授予就返回false
PermissionAgent.getInstance().checkPermission(Manifest.permission.CALL_PHONE,
Manifest.permission.CAMERA)
//检测特殊权限,通过SpecialPermission选择权限
PermissionAgent.getInstance().checkPermission(SpecialPermission.ACCESS_NOTIFICATION_POLICY)
log标签分类两级:
- 一级:TAG1=PermissionAgent可以过滤出本库所有的log日志,通过标签过滤可得。
- 二级:TAG2=xxx.class.getSimpleName()可以过滤出xxx类的log日志,通过内容过滤可得。
log格式:D/{TAG1}: {TAG2}/{log msg}