鸿蒙开源三方组件(soul-permission)--权限检查、请求 精华

发布于 2021-7-27 09:37
浏览
1收藏

1. 介绍

soul-permission是一个权限组件,主要对权限的检查及请求。方法级权限适配、解耦FractionAbility和Fraction、不再需要Context、不再需要onPermissionResult,支持特殊权限(Notification[通知]、SystemAlert[应用悬浮窗]、UNKNOW_SOURCE[未知来源应用安装]、
WRITE_SYS_SETTINGS[写入系统设置])的检查与请求。

2. 效果展示

鸿蒙开源三方组件(soul-permission)--权限检查、请求-开源基础软件社区
鸿蒙开源三方组件(soul-permission)--权限检查、请求-开源基础软件社区

3. 依赖

(1). 在工程的build.gradle的allprojects中,添加HAR所在的Maven仓地址

repositories {
	maven {
		url 'https://s01.oss.sonatype.org/content/repositories/releases/'
	}
}

(2). 在应用模块的build.gradle的dependencies闭包中,添加如下代码:

	dependencies {
		implementation 'io.github.dzsf:soul-permission:1.0.0'
	}

4. 使用说明

(1). 一句话版本完成自动判断、权限检查、请求、后续操作:

    SoulPermission.getInstance().checkAndRequestPermission(SystemPermission.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) {
					Utils.showMessage(ApiGuideAbility.this, permission.toString() + "\n is ok , you can do your operations");
				}

				@Override
				public void onPermissionDenied(Permission permission) {
					Utils.showMessage(ApiGuideAbility.this, permission.toString() + " \n is refused , you can not do next things");
				}
			});

(2). 也可以一次请求多项权限

    SoulPermission.getInstance().checkAndRequestPermissions(
            Permissions.build(SystemPermission.CAMERA, SystemPermission.WRITE_USER_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) {
                    Utils.showMessage(ApiGuideAbility.this, allPermissions.length + "permissions is ok" + " \n  you can do your operations");
                }

                @Override
                public void onPermissionDenied(Permission[] refusedPermissions) {
                    Utils.showMessage(ApiGuideAbility.this, refusedPermissions[0].toString() + " \n is refused , you can not do next things");
                }
            });

(3). 包含shouldShowRequestPermissionRationale的情形

    SoulPermission.getInstance().checkAndRequestPermission(SystemPermission.READ_CONTACTS,
            new CheckRequestPermissionListener() {
                @Override
                public void onPermissionOk(Permission permission) {
                    Utils.showMessage(ApiGuideAbility.this, permission.toString() + "\n is ok , you can do your operations");
                }

                @Override
                public void onPermissionDenied(Permission permission) {
                    // see CheckPermissionWithRationaleAdapter
                    if (permission.shouldRationale()) {
                        Utils.showMessage(ApiGuideAbility.this, permission.toString() + " \n you should show a explain for user then retry ");
                    } else {
                        Utils.showMessage(ApiGuideAbility.this, permission.toString() + " \n is refused , you can not do next things");
                    }
                }
            });

(4). 检查某项权限

    //you can also use checkPermissions() for a series of permissions
    Permission checkResult = SoulPermission.getInstance().checkSinglePermission(SystemPermission.LOCATION);

(5). 检查特殊权限[通知权限]

    boolean checkResult = SoulPermission.getInstance().checkSpecialPermission(Special.NOTIFICATION);

(6). 检查并请求特殊权限[未知应用安装]

    //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) {
                    Utils.showMessage(ApiGuideAbility.this, "install unKnown app  is enable now ");
                }

                @Override
                public void onDenied(Special permission) {
                    Utils.showMessage(ApiGuideAbility.this, "install unKnown app  is disable yet");
                }
            });

(7). 跳转到应用设置页

    SoulPermission.getInstance().goApplicationSettings(data -> {
            //if you need to know when back from app detail
            Utils.showMessage(ApiGuideAbility.this, "back from go appDetail");
        });

(8). 注意事项:

    SoulPermission内部使用contentProvider自动初始化,
    如果你项目中使用了通过替换Application方式从而可能会导致SoulPermission内部初始化失败的框架(如Tinker,腾讯乐固等),
    请手动在你的Application类中调用init即可(通过设置debug,可以看到错误日志打印和相关Toast)。
    // invoke init in your application when auto init failed
    public class MyApplication extends AbilityPackage {
    
        @Override
        public void onInitialize() {
            super.onInitialize();
            SoulPermission.setDebug(true);
        }
    }

5. 相关资料

项目地址:https://gitee.com/openneusoft/soul-permission
IDE官方下载地址:https://developer.harmonyos.com/cn/develop/deveco-studio

1
收藏 1
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐