网站组网图,河南企业网站备案,江苏省建设工程网站,分销系统多少钱一、简介
鸿蒙开发过程中可用于请求的权限一共有两种#xff1a;normal和system_basic。以下内容摘自官网#xff1a; normal权限 normal 权限允许应用访问超出默认规则外的普通系统资源。这些系统资源的开放#xff08;包括数据和功能#xff09;对用户隐私以及其他应用带…一、简介
鸿蒙开发过程中可用于请求的权限一共有两种normal和system_basic。以下内容摘自官网 normal权限 normal 权限允许应用访问超出默认规则外的普通系统资源。这些系统资源的开放包括数据和功能对用户隐私以及其他应用带来的风险很小。 该类型的权限仅向APL等级为normal及以上的应用开放。 system_basic权限 system_basic权限允许应用访问操作系统基础服务相关的资源。这部分系统基础服务属于系统提供或者预置的基础功能比如系统设置、身份认证等。这些系统资源的开放对用户隐私以及其他应用带来的风险较大。 该类型的权限仅向APL等级为system_basic及以上的应用开放。 system_core权限 system_core权限涉及到开放操作系统核心资源的访问操作。这部分系统资源是系统最核心的底层服务如果遭受破坏操作系统将无法正常运行。 鉴于该类型权限对系统的影响程度非常大目前暂不向任何三方应用开放。
二、使用
以用户的MICROPHONE麦克风权限为例
我们在使用麦克风之前需要先查看用户权限然后如果没有开启需要跳转系统设置页面引导用户开启权限。
import abilityAccessCtrl, { Permissions } from ohos.abilityAccessCtrl;
import bundleManager from ohos.bundle.bundleManager;
import common from ohos.app.ability.common;
Entry
Component
struct PermissionTest {build() {Column() {Text(check permission).width(200).height(200).backgroundColor(Color.Orange).onClick(() {//检测权限状态checkPermissions()})}.backgroundColor(Color.Red).width(100%).height(100%)}
}/** 获取accessToken* */
async function checkAccessToken(permission: Permissions): PromiseabilityAccessCtrl.GrantStatus {let atManager abilityAccessCtrl.createAtManager()let grantStatus :abilityAccessCtrl.GrantStatus//获取accessTkenIDlet tokenId: numbertry {let bundleInfo: bundleManager.BundleInfo await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION)let appInfo: bundleManager.ApplicationInfo bundleInfo.appInfotokenId appInfo.accessTokenId} catch (err) {console.log(get BundleInfo For self failed err.code messageerr.message)}//检验是否授权try {grantStatus await atManager.checkAccessToken(tokenId,permission)} catch (err) {console.log(checkAccessToken failederr.code messageerr.message)}return grantStatus
}/** 检查权限状态* */
async function checkPermissions():Promisevoid {const permissions: ArrayPermissions [ohos.permission.MICROPHONE]let grantStatus: abilityAccessCtrl.GrantStatus await checkAccessToken(permissions[0])if (grantStatus abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {console.log(permission has requeired)} else {console.log(need request permission)//请求权限requestPermission()}
}/** 请求权限* */
function requestPermission() {let context getContext(this) as common.UIAbilityContextlet atManger abilityAccessCtrl.createAtManager()let permissions: ArrayPermissions [ohos.permission.MICROPHONE]atManger.requestPermissionsFromUser(context,permissions).then((data) {let grantStatus: Arraynumber data.authResultslet grantPermissions: Arraystring data.permissionslet length: number grantStatus.lengthconsole.log(user permissoned length JSON.stringify(grantPermissions)length)for(let i 0; ilength; i) {if (grantStatus[i] 0) {console.log(user has agreed permissoned)} else {console.log(user has disagree permissoned)openPermissionSettings()return}}}).catch((err) {console.log(requestPermissionFromUserfailederr.code messageerr.message)})
}/** 跳转到权限设置页面* */
function openPermissionSettings() {let context getContext(this) as common.UIAbilityContextlet wantInfo {action: action.settings.app.info,parameters: {settingsParamBundleName: com.example.myapplication}}context.startAbility(wantInfo).then(() {console.log(open setting page)}).catch((err) {console.log(open setting page failed)})
}
三、注意事项
1、需要在module.json5文件中配置权限申请
{name: ohos.permission.MICROPHONE,usedScene : {when: inuse}
}
2、如果需要引导跳转系统的设置页面注意bundleName要正确否则不跳转