- 方法级权限适配、解耦Activity和Fragment、不再需要Context、不再需要onPermissionResult
- 内部涵盖版本判断,一行代码解决权限相关操作,无需在调用业务方写权限适配代码,继而实现真正调用时请求的“真运行时权限”
- 接入成本低,代码改动极小,零入侵,仅需要在gradle配置一行代码
- 支持多项权限同时请求
- 支持特殊权限(Notification[通知]、SystemAlert[应用悬浮窗]、UNKNOW_SOURCE[未知来源应用安装]、WRITE_SYS_SETTINGS[写入系统设置])的检查与请求
- 支持系统权限页面跳转
- 支持debug模式
dependencies {
implementation 'com.github.soulqw:SoulPermission:1.4.0'
}
如果你的应用还没有适配Android X:
dependencies {
implementation 'com.qw:soulpermission:1.2.2'
}
- 1.2.2即为支持support28的最后版本,后续不再维护,新功能只会在1.3.0基础上迭代(代码分支 master_old)
- 后期Jcenter 库将无法正常下载,建议尽快迁移到AndroidX,可享受最新的更新
- 一句话版本完成自动判断、权限检查、请求、后续操作:
SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
//if you want do noting or no need all the callbacks you may use SimplePermissionAdapter instead
new CheckRequestPermissionListener() {
@Override
public void onPermissionOk(Permission permission) {
Toast.makeText(ApiGuideActivity.this, permission.toString() +
"\n is ok , you can do your operations", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(Permission permission) {
Toast.makeText(ApiGuideActivity.this, permission.toString() +
" \n is refused you can not do next things", Toast.LENGTH_SHORT).show();
}
});
- 也可以一次请求多项权限
SoulPermission.getInstance().checkAndRequestPermissions(
Permissions.build(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
//if you want do noting or no need all the callbacks you may use SimplePermissionsAdapter instead
new CheckRequestPermissionsListener() {
@Override
public void onAllPermissionOk(Permission[] allPermissions) {
Toast.makeText(ApiGuideActivity.this, allPermissions.length + "permissions is ok" +
" \n you can do your operations", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(Permission[] refusedPermissions) {
Toast.makeText(ApiGuideActivity.this, refusedPermissions[0].toString() +
" \n is refused you can not do next things", Toast.LENGTH_SHORT).show();
}
});
- 包含shouldShowRequestPermissionRationale的情形
SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.READ_CONTACTS,
new CheckRequestPermissionListener() {
@Override
public void onPermissionOk(Permission permission) {
Toast.makeText(ApiGuideActivity.this, permission.toString() +
"\n is ok , you can do your operations", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(Permission permission) {
// see CheckPermissionWithRationaleAdapter
if (permission.shouldRationale()) {
Toast.makeText(ApiGuideActivity.this, permission.toString() +
" \n you should show a explain for user then retry ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ApiGuideActivity.this, permission.toString() +
" \n is refused you can not do next things", Toast.LENGTH_SHORT).show();
}
}
});
- 检查某项权限
//you can also use checkPermissions() for a series of permissions
Permission checkResult = SoulPermission.getInstance().checkSinglePermission(Manifest.permission.ACCESS_FINE_LOCATION);
- 检查特殊权限[通知权限]
boolean checkResult = SoulPermission.getInstance().checkSpecialPermission(Special.NOTIFICATION);
- 检查并请求特殊权限[未知应用安装]
//if you want do noting or no need all the callbacks you may use SimpleSpecialPermissionAdapter instead
SoulPermission.getInstance().checkAndRequestPermission(Special.UNKNOWN_APP_SOURCES, new SpecialPermissionListener() {
@Override
public void onGranted(Special permission) {
Toast.makeText(ApiGuideActivity.this, "install unKnown app is enable now", Toast.LENGTH_SHORT).show();
}
@Override
public void onDenied(Special permission) {
Toast.makeText(ApiGuideActivity.this, "install unKnown app is disable yet", Toast.LENGTH_SHORT).show();
}
});
- 跳转到应用设置页
SoulPermission.getInstance().goApplicationSettings(new GoAppDetailCallBack() {
@Override
public void onBackFromAppDetail(Intent data) {
//if you need to know when back from app detail
Utils.showMessage(view, "back from go appDetail");
}
});
- 设置跳过老的权限系统(老的系统默认权限直接授予)
SoulPermission.skipOldRom(true);
- 设置debug模式(看日志打印)
SoulPermission.setDebug(true);
- 最低支持Android 4.0(Api level 14)
- SoulPermission内部使用contentProvider自动初始化,如果你项目中使用了通过替换Application方式从而可能会导致SoulPermission内部初始化失败的框架(如Tinker,腾讯乐固等),请手动在你的Application类中调用init即可(通过设置debug,可以看到错误日志打印和相关Toast)。
//invoke init in your application when auto init failed
public class SimpleApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//no necessary
SoulPermission.init(this);
}
}
- 如果需要在某个页面创建时候请求权限,请在onCreate()中使用、请不要在onResume()调用,否则权限未被动态授予前会陷入死循环。
- for common Permission
- for Special Permission