Android权限请求框架XXPermissions

Github项目地址

1、添加远程仓库和依赖

  • 项目根目录下的build.gradle:
allprojects {
    repositories {
        google()
        jcenter()
        // JitPack 远程仓库:https://jitpack.io
        maven { url 'https://jitpack.io' }
    }
}
  • app目录下的bulid.gradle:
android{
    // 支持 JDK 1.8
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
  // 权限请求框架:https://github.com/getActivity/XXPermissions
     implementation 'com.github.getActivity:XXPermissions:13.5'
}

  • 使用方式:
       XXPermissions.with(this)
                        //单个权限
                        .permission(Permission.RECORD_AUDIO)
                        .permission(Permission.WRITE_EXTERNAL_STORAGE)
                        .permission(Permission.READ_EXTERNAL_STORAGE)
                        // .interceptor(new IPermissionInterceptor() {})
                        //  .unchecked()
                        .request(new OnPermissionCallback() {
                            @Override
                            public void onGranted(List permissions, boolean all) {
                                if (all){
                                    Log.e(TAG, "onGranted: 获取权限成功!");
                                }
                            }

                            @Override
                            public void onDenied(List permissions, boolean never) {
                                if (never){
                                    Log.e(TAG, "onDenied:被永久拒绝授权,请手动授予权限 " );
                                    XXPermissions.startPermissionActivity(MainActivity.this,permissions);
                                }else {
                                    Log.e(TAG, "onDenied: 权限获取失败");
                                }
                            }
                        });
  • 权限获取结果回调处理:
 @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == XXPermissions.REQUEST_CODE){
            if (XXPermissions.isGranted(this,Permission.RECORD_AUDIO)&&
                    XXPermissions.isGranted(this,Permission.READ_EXTERNAL_STORAGE)){
                Toast.makeText(MainActivity.this,"权限已获取",Toast.LENGTH_LONG).show();
            }else {
                Toast.makeText(MainActivity.this,"权限获取失败",Toast.LENGTH_LONG).show();
            }
        }
    }

更详细的用法还要参考官方文档。在此仅作记录。

你可能感兴趣的:(Android权限请求框架XXPermissions)