SoulPermission

Introduction: a permission check or request for android
More: Author   ReportBugs   
Tags:

Hex.pm

Android 权限适配的更优解决方案:

  • 解耦 Activity 和 Fragment、不再需要 Context、不再需要 onPermissionResult
  • 内部涵盖版本判断,一行代码解决权限相关操作,无需在调用业务方写权限适配代码,继而实现真正调用时请求的“真运行时权限”
  • 接入成本低,零入侵,仅需要在 gradle 配置一行代码
  • 支持多项权限同时请求
  • 支持特殊权限(Notification[通知]、SystemAlert[应用悬浮窗]、未知来源应用安装)的检查与请求
  • 支持系统权限页面跳转
  • 支持 debug 模式

    Installation:

dependencies {
    implementation 'com.qw:soulpermission:1.1.8'
}

1.1.8 ReleaseNote

Usage:

基本用法:

  • 一句话版本完成自动判断、权限检查、请求、后续操作: ```java 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();
                  }
              });
    
- 也可以一次请求多项权限

```java
  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");
            }
        });
  • 设置 debug 模式(看日志打印)
SoulPermission.setDebug(true);

注意事项:

  • 最低支持 Android 4.0(Api level 14)
  • SoulPermission 内部自动初始化,如果你项目中使用了通过替换 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()调用,否则权限未被动态授予前会陷入死循环。

    Screenshot:

    image

image

  • for common Permission

image

  • for Special Permission

image

MoreDetail:

工作原理和最佳示例

如果有任何问题或者反馈请联系我:

cd5160866@126.com

Support Me
Apps
About Me
Google+: Trinea trinea
GitHub: Trinea