一次初始化处处可用的链式编程动态权限请求库
- 链式编程
- 不依赖Activity,Fragment
- 运行时权限申请
- 运行时权限组申请
- 运行时权限和权限组混合申请
- 支持多个权限并行申请
- 支持多个权限串行申请
- 支持特殊权限申请,如REQUEST_INSTALL_PACKAGES,SYSTEM_ALERT_WINDOW,ACCESS_NOTIFICATION_POLICY,WRITE_SETTINGS
- 最小支持android 14
- 多ROM多版本适配
//android support
implementation 'cn.junhua.android:permission-agent:1.3.1'
//androidx
implementation 'cn.junhua.android:permission-agent-x:1.3.1'
//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(Context context,List<String> permissions) {
//成功
}
})
.onDenied(new OnDeniedCallback<List<String>>() {
@Override
public void onDenied(Context context,List<String> permissions) {
//拒绝
}
})
.onRationale(new OnRationaleCallback<List<String>>() {
@Override
public void onRationale(Context context,List<String> permissions, AgentExecutor executor) {
//提示用户
executor.execute();//继续
executor.cancel();//取消
//do something
}
})
.apply();
并行请求时,结果会同时返回。当onRationale回调时用户拒绝,不继续下面的请求,直接执行已授予的权限和拒绝(或未授予)的权限回调。
PermissionAgent.getInstance()
.request(Manifest.permission.CAMERA, Manifest.permission.WRITE_CONTACTS)
//.code(123)//与你自定义code冲突时可以设置,一般不用自己设置
.onGranted(new OnGrantedCallback<List<String>>() {
@Override
public void onGranted(Context context,List<String> permissions) {
//成功
}
})
.onDenied(new OnDeniedCallback<List<String>>() {
@Override
public void onDenied(Context context,List<String> permissions) {
//拒绝
}
})
.onRationale(new OnRationaleCallback<List<String>>() {
@Override
public void onRationale(Context context,List<String> permissions, AgentExecutor executor) {
//提示用户
executor.execute();//继续
executor.cancel();//取消
//do something
}
})
.apply();
串行请求时,权限会顺序请求,当前一个请求处理完成后才会请求后一个权限。
注意:当使用onRationale
回调时,AgentExecutor
必须回调execute()
或者cancel()
之一,才能执行后续请求。
PermissionAgent.getInstance()
.requestEach(Manifest.permission_group.CONTACTS, Manifest.permission.ACCESS_COARSE_LOCATION)
.onGranted(new OnGrantedCallback<List<String>>() {
@Override
public void onGranted(Context context,List<String> permissions) {
toast("onGranted() called with: permissions = [" + permissions + "]");
Log.d(TAG, "onGranted() called with: permissions = [" + permissions + "]");
}
})
.onDenied(new OnDeniedCallback<List<String>>() {
@Override
public void onDenied(Context context,List<String> permissions) {
toast("onDenied() called with: permissions = [" + permissions + "]");
Log.d(TAG, "onDenied() called with: permissions = [" + permissions + "]");
}
})
.onRationale(new OnRationaleCallback<List<String>>() {
@Override
public void onRationale(Context context,List<String> permissions, AgentExecutor executor) {
executor.execute();
toast("onRationale() called with: permissions = [" + permissions + "]");
Log.d(TAG, "onRationale() called with: permissions = [" + permissions + "], executor = [" + executor + "]");
}
})
.apply();
使用SpecialPermission
枚举选择需要申请的特殊权限,其他操作不变。
SpecialPermission枚举如下:
- SpecialPermission.REQUEST_INSTALL_PACKAGES 安装未知apk权限
- SpecialPermission.WRITE_SETTINGS 修改设置权限
- SpecialPermission.SYSTEM_ALERT_WINDOW 系统窗口权限
- SpecialPermission.ACCESS_NOTIFICATION_POLICY 推送通知权限
PermissionAgent.getInstance()
.request(SpecialPermission.REQUEST_INSTALL_PACKAGES)
//.code(123)//与你自定义code冲突时可以设置,一般不用自己设置
.onGranted(new OnGrantedCallback<SpecialPermission>() {
@Override
public void onGranted(Context context,SpecialPermission permissions) {
//成功
}
})
.onDenied(new OnDeniedCallback<SpecialPermission>() {
@Override
public void onDenied(Context context,SpecialPermission permissions) {
//拒绝
}
})
.onRationale(new OnRationaleCallback<List<String>>() {
@Override
public void onRationale(Context context,SpecialPermission permissions, AgentExecutor executor) {
//提示用户
executor.execute();//继续
executor.cancel();//取消
//do something
}
})
.apply();
注:当使用权限组请求权限时,会拆分为权限组内多有的单个权限一起去请求权限。
PermissionAgent.getInstance().startSettingPage(0x123);
//当权限被拒绝时候调用此方法,检测权限是否永远被拒绝
PermissionAgent.getInstance().hasAlwaysDeniedPermission(Manifest.permission.ACCESS_FINE_LOCATION)
权限检测的工具方法,供单独使用时调用。
//检测单个权限
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}
感谢以下库或文章提供的思路和适配方案: