EasyPermission Android 6.0 最简洁的权限框架

EasyPermission

一款非常简洁,依赖很少的权限请求框架。不依赖第三方类库,如RxJava,不依赖运行时或编译时注解,无须在Activity和Fragment发起请求和接收返回。核心代码只有5个类,并且适配了小米 AppOps 权限。

Dependencies

  • Gradle
    compile 'com.xcheng:easypermission:1.0.1'

UaseAge

  • 请求单个权限
 EasyPermission.with(activty)
                      .permissions(Manifest.permission.CAMERA)
                      .request(12, new OnRequestCallback() {
                          @Override
                          public void onAllowed() {
                              // request allowed
                          }

                          @Override
                          public void onRefused(DeniedResult deniedResult) {
                              // request denied
                          }
                      });
  • 请求多个权限
 EasyPermission.with(activty)
                      .permissions(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                      .request(12, new OnRequestCallback() {
                          @Override
                          public void onAllowed() {
                              // request allowed
                          }

                          @Override
                          public void onRefused(DeniedResult deniedResult) {
                              // request denied
                          }
                      });
  • 处理权限回调
EasyPermission.with(activty)
                      .permissions(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                      .request(12, new OnRequestCallback() {
                          @Override
                          public void onAllowed() {
                              // request allowed
                          }

                          @Override
                          public void onRefused(DeniedResult deniedResult) {
                              if (deniedResult.allNeverAsked) {

                              } else {

                              }
                          }
                      });
  • DeniedResult解析
public final class DeniedResult {
          // 所有被拒绝的权限
          public final List deniedPerms;
          // 被拒绝但未勾选不再询问的权限
          public final List showRationalePerms;
          // 被拒绝且勾选不再询问的权限
          public final List neverAskedPerms;
          // 是否全部权限都是被拒绝且不再询问
          public final boolean allNeverAsked;

          DeniedResult(@NonNull List deniedPerms, @NonNull List showRationalePerms) {
              this.deniedPerms = deniedPerms;
              this.showRationalePerms = showRationalePerms;
              this.allNeverAsked = showRationalePerms.isEmpty();
              //处理被完全拒绝的权限
              neverAskedPerms = new ArrayList<>();
              for (String permission : deniedPerms) {
                  //rationales 不包含 deniedPermissions 的就是完全被拒绝的
                  if (!showRationalePerms.contains(permission)) {
                      neverAskedPerms.add(permission);
                  }
              }
          }
      }

EasyPermisson大大简化了android的权限请求流程,增强了代码的可读性和连续性。
EasyPermission github地址 欢迎大家提疑问和bug

你可能感兴趣的:(android)