资料参考

参考资料​​XXPermissions​​和​​权限开发指导​

项目集成

1、项目级的build.gradle,添加如下代码

allprojects {
repositories {
repositories{
mavenCentral()
}
}
}

【JAVA UI】HarmonyOs 怎么集成XxPermissions_JAVA UI

2、应用级的build.gradle添加如下代码

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

【JAVA UI】HarmonyOs 怎么集成XxPermissions_JAVA UI_02

api讲解

1、检查权限 使用XXPermissions.hasPermission的方法

/**
* 第一个参数当前context
* 第二个参数是申请权限名称
* return boolean参数
* 返回是否存在该权限
*若为true 表示存在该权限 若为false表示不存在该权限
*/

【JAVA UI】HarmonyOs 怎么集成XxPermissions_JAVA UI_03

2、申请权限 代码如下

//todo 申请权限名称
.request(new OnPermission() {
@Override
public void hasPermission(List<String> granted, boolean all) {//todo 权限允许的回调
if (all) {
mTextResult.setText("获取拍照权限成功");
}
}

@Override
public void noPermission(List<String> denied, boolean never) {//todo 权限背景
if (never) {
mTextResult.setText("被永久拒绝授权,请手动授予拍照权限");
//todo 如果是被永久拒绝就跳转到应用权限系统设置页面

} else {
mTextResult.setText("获取拍照权限失败");
}
}
});

【JAVA UI】HarmonyOs 怎么集成XxPermissions_JAVA UI_04

3、跳转系统级应用

String[] permissions = new String[denied.size()];
denied.toArray(permissions);
XPermissions.startPermissionActivity(MainAbilitySlice.this, permissions, this);

【JAVA UI】HarmonyOs 怎么集成XxPermissions_JAVA UI_05

4、重写onAbilityResult代码如下

@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
super.onAbilityResult(requestCode, resultCode, resultData);
XXPermissions.onAbilityResult(requestCode);
//todo 判断权限是否存在 若存在则不需要申请权限 若不存在 则重新申请权限
mTextResult.setText("判断权限是否存在 若存在则不需要申请权限 若不存在 则重新申请权限");
}

【JAVA UI】HarmonyOs 怎么集成XxPermissions_JAVA UI_06

代码实现

在config.json添加拍照权限申请的代码,代码和效果图如下

"reqPermissions": [
{
"name": "ohos.permission.CAMERA",
}
],

【JAVA UI】HarmonyOs 怎么集成XxPermissions_XxPermissions_07

在xml写一个申请权限的Text和显示结果的Text,代码如下

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="horizontal_center"
ohos:orientation="vertical">
<Text
ohos:id="$+id:text_helloworld"
ohos:height="100vp"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:background_element="#ed6262"
ohos:layout_alignment="center"
ohos:text="申请权限"
ohos:text_size="40vp"

<Text
ohos:id="$+id:text_result"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:text_alignment="center"
ohos:layout_alignment="center"
ohos:text="权限结果"
ohos:text_size="40vp"


</DirectionalLayout>

【JAVA UI】HarmonyOs 怎么集成XxPermissions_XxPermissions_08

【JAVA UI】HarmonyOs 怎么集成XxPermissions_JAVA UI_09

java代码如下

package com.harmony.alliance.myapplication.slice;

import com.harmony.alliance.myapplication.MainAbility;
import com.harmony.alliance.myapplication.ResourceTable;
import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
Text mTextResult;

@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
mTextResult = findComponentById(ResourceTable.Id_text_result);
findComponentById(ResourceTable.Id_text_helloworld).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
RequestPermissions();
}
});

}

public void RequestPermissions() {
XXPermissions.with(MainAbilitySlice.this)
.permission(Permission.CAMERA)//todo 申请权限名称
.request(new OnPermission() {
@Override
public void hasPermission(List<String> granted, boolean all) {//todo 权限允许的回调
if (all) {
mTextResult.setText("获取拍照权限成功");
}
}

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

}


}

【JAVA UI】HarmonyOs 怎么集成XxPermissions_XxPermissions_10

运行效果

【JAVA UI】HarmonyOs 怎么集成XxPermissions_XxPermissions_11

欲了解更多更全技术文章,欢迎访问​​https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh​