鸿蒙开源组件——一键式权限请求框架

jacksky
发布于 2021-11-17 18:15
浏览
0收藏

XxPermissions

XxPermissions权限请求框架 一键式权限请求框架

##图片示例鸿蒙开源组件——一键式权限请求框架-鸿蒙开发者社区

引用:

方式一: 通过library生成har包,添加har包到libs文件夹内 在entry的gradle内添加如下代码 implementation fileTree(dir: 'libs', include: ['.jar', '.har'])

方式二: allprojects{ repositories{ mavenCentral() } }

implementation 'io.openharmony.tpc.thirdlib:XXPermissions:1.0.3'

用法:

第一步:

  XXPermissions.with(getAbility())
                  .permission(Permission.CAMERA)
                  .request(new OnPermission() {
                      @Override
                      public void hasPermission(List<String> granted, boolean all) {
                          if (all) {
                              toast("获取拍照权限成功");
                          }
                      }

                      @Override
                      public void noPermission(List<String> denied, boolean never) {
                          if (never) {
                              toast("被永久拒绝授权,请手动授予拍照权限");
                              // 如果是被永久拒绝就跳转到应用权限系统设置页面
                              String[] permissions = new String[denied.size()];
                              denied.toArray(permissions);
                              XXPermissions.startPermissionActivity(MainAbilitySlice.this,permissions, this);
                          } else {
                              toast("获取拍照权限失败");
                          }
                      }
                  });

第二步:在当前的ability重写onRequestPermissionsFromUserResult方法, 调用如下方法:

XXPermissions.addPermissionsForResult(this,requestCode, permissions, grantResults);

 

例如:

        @Override
        public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
                   super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
                   XXPermissions.addPermissionsForResult(this,
                                            requestCode, permissions, grantResults);
        }

若:权限被永久的拒绝,跳转应用列表返回

   XXPermissions.startPermissionActivity(MainAbilitySlice.this);
    或者
    XXPermissions.startPermissionActivity(MainAbilitySlice.this,permissions, this);

其接收返回,需在其ability或者abilitySince中重写onAbilityResult(...)方法 如:

     @Override
      protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
              super.onAbilityResult(requestCode, resultCode, resultData);
              XXPermissions.onAbilityResult(requestCode);
      }

XXPermissions-master.zip 2.19M 10次下载
已于2021-11-17 18:15:44修改
收藏
回复
举报
回复
    相关推荐