当前位置:   article > 正文

SoulPermission 动态申请权限(封装)

soulpermission

                                       SoulPermission使用方法

适配Android X使用(建议使用):

  1. dependencies {
  2. implementation 'com.qw:soulpermission:1.2.2_x'
  3. }
  4. 如果你的应用
没有适配Android X使用:
  1. dependencies {
  2. implementation 'com.qw:soulpermission:1.2.2'
  3. }

基本用法:

  • 一句话版本完成自动判断、权限检查、请求、后续操作:
  1. SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.ACCESS_FINE_LOCATION,
  2. //if you want do noting or no need all the callbacks you may use SimplePermissionAdapter instead
  3. new CheckRequestPermissionListener() {
  4. @Override
  5. public void onPermissionOk(Permission permission) {
  6. Toast.makeText(ApiGuideActivity.this, permission.toString() +
  7. "\n is ok , you can do your operations", Toast.LENGTH_SHORT).show();
  8. }
  9. @Override
  10. public void onPermissionDenied(Permission permission) {
  11. Toast.makeText(ApiGuideActivity.this, permission.toString() +
  12. " \n is refused you can not do next things", Toast.LENGTH_SHORT).show();
  13. }
  14. });
  • 也可以一次请求多项权限
  1. SoulPermission.getInstance().checkAndRequestPermissions(
  2. Permissions.build(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
  3. //if you want do noting or no need all the callbacks you may use SimplePermissionsAdapter instead
  4. new CheckRequestPermissionsListener() {
  5. @Override
  6. public void onAllPermissionOk(Permission[] allPermissions) {
  7. Toast.makeText(ApiGuideActivity.this, allPermissions.length + "permissions is ok" +
  8. " \n you can do your operations", Toast.LENGTH_SHORT).show();
  9. }
  10. @Override
  11. public void onPermissionDenied(Permission[] refusedPermissions) {
  12. Toast.makeText(ApiGuideActivity.this, refusedPermissions[0].toString() +
  13. " \n is refused you can not do next things", Toast.LENGTH_SHORT).show();
  14. }
  15. });
  • 包含shouldShowRequestPermissionRationale的情形
  1. SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.READ_CONTACTS,
  2. new CheckRequestPermissionListener() {
  3. @Override
  4. public void onPermissionOk(Permission permission) {
  5. Toast.makeText(ApiGuideActivity.this, permission.toString() +
  6. "\n is ok , you can do your operations", Toast.LENGTH_SHORT).show();
  7. }
  8. @Override
  9. public void onPermissionDenied(Permission permission) {
  10. // see CheckPermissionWithRationaleAdapter
  11. if (permission.shouldRationale()) {
  12. Toast.makeText(ApiGuideActivity.this, permission.toString() +
  13. " \n you should show a explain for user then retry ", Toast.LENGTH_SHORT).show();
  14. } else {
  15. Toast.makeText(ApiGuideActivity.this, permission.toString() +
  16. " \n is refused you can not do next things", Toast.LENGTH_SHORT).show();
  17. }
  18. }
  19. });
  • 检查某项权限
  1. //you can also use checkPermissions() for a series of permissions
  2. Permission checkResult = SoulPermission.getInstance().checkSinglePermission(Manifest.permission.ACCESS_FINE_LOCATION);
  • 检查特殊权限[通知权限]
 boolean checkResult = SoulPermission.getInstance().checkSpecialPermission(Special.NOTIFICATION);
  • 检查并请求特殊权限[未知应用安装]
  1. //if you want do noting or no need all the callbacks you may use SimpleSpecialPermissionAdapter instead
  2. SoulPermission.getInstance().checkAndRequestPermission(Special.UNKNOWN_APP_SOURCES, new SpecialPermissionListener() {
  3. @Override
  4. public void onGranted(Special permission) {
  5. Toast.makeText(ApiGuideActivity.this, "install unKnown app is enable now", Toast.LENGTH_SHORT).show();
  6. }
  7. @Override
  8. public void onDenied(Special permission) {
  9. Toast.makeText(ApiGuideActivity.this, "install unKnown app is disable yet", Toast.LENGTH_SHORT).show();
  10. }
  11. });
  • 跳转到应用设置页
  1. SoulPermission.getInstance().goApplicationSettings(new GoAppDetailCallBack() {
  2. @Override
  3. public void onBackFromAppDetail(Intent data) {
  4. //if you need to know when back from app detail
  5. Utils.showMessage(view, "back from go appDetail");
  6. }
  7. });
  • 设置跳过老的权限系统(老的系统默认权限直接授予)
 SoulPermission.skipOldRom(true);
  • 设置debug模式(看日志打印)
SoulPermission.setDebug(true);

注意事项:

  • 最低支持Android 4.0(Api level 14)
  • SoulPermission内部自动初始化,如果你项目中使用了通过替换Application方式从而可能会导致SoulPermission内部初始化失败的框架(如Tinker,腾讯乐固等),请手动在你的Application类中调用init即可(通过设置debug,可以看到错误日志打印和相关Toast)。
  1. //invoke init in your application when auto init failed
  2. public class SimpleApplication extends Application {
  3. @Override
  4. public void onCreate() {
  5. super.onCreate();
  6. //no necessary
  7. SoulPermission.init(this);
  8. }
  9. }
  • 如果需要在某个页面创建时候请求权限,请在onCreate()中使用、请不要在onResume()调用,否则权限未被动态授予前会陷入死循环。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/474674
推荐阅读
相关标签
  

闽ICP备14008679号