当前位置:网站首页>[Harmony OS] [ARK UI] ETS context basic operations
[Harmony OS] [ARK UI] ETS context basic operations
2022-08-03 04:42:00 【Huawei developers BBS】
在HarmonyOS开发中,‘权限申请’,‘权限检查’,‘获取版本信息’,‘获取包名’都是基本操作,今天学习一下怎么实现如下功能,主要分为‘Api说明’,‘代码实现’,‘运行效果’三个步骤进行描述
1. Api说明
1.1参考Ability上下文
1.2 context.verifyPermission
verifyPermission(permission: string, options?: PermissionOptions): Promise
检查指定进程是否存在指定的权限,options为可选参数,不设置时表示检查自身权限,使用Promise方式作为异步方法.
1.2.1请求参数
参数一permission:需要校验的权限
参数二options:包含pid,uid(常规应用使用不到,这里不做详细讲解)
1.2.2 返回类型
Promise:Promise形式返回结果.返回-1表示不具备当前检查权限,0表示有权限
1.2.3示例:
import ability_featureAbility from '@ohos.ability.featureAbility'
var context = ability_featureAbility.getContext();
let permission = "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS";
context.verifyPermission(permission,(error, data)=>{
if (error) {
console.error('Operation failed. Cause: ' + JSON.stringify(error));
return;
}
console.info('Operation successful. Data:' + JSON.stringify(data))
})
1.3 context.requestPermissionsFromUser
requestPermissionsFromUser(permissions: Array, requestCode: number): Promise
向用户请求某些权限,在申请权限之前查询自身进程是否已被授予该权限(verifyPermission),如果已有权限,则无需申请,否则需要申请权限.使用Promise方式作为异步方法.
1.3.1参数详解
参数一:permissions:请求授予的权限
参二:requestCode :请求状态码 与匹配应用程序对应的特定请求代码,取值范围:大于等于0
1.3.2返回值:Promise:回调函数,可以在回调函数中处理接口返回值,返回权限请求结果
1.3.3PermissionRequestResult属性详解
requestCode:得到返回的请求码,主要作用用于判断是哪个请求的权限的作用
permissions:请求权限集合
authResults:权限验证结果,返回-1表示不具备当前检查权限,0表示有权限
1.3.4示例
import ability_featureAbility from '@ohos.ability.featureAbility'
var context = ability_featureAbility.getContext();
let permissions = ["ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS","ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS_EXTENSION"];
let requestCode = 123
context.requestPermissionsFromUser(permissions, requestCode)
.then((data) => {
console.info('Operation successful. Data: ' + JSON.stringify(data));
}).catch((error) => {
console.error('Operation failed. Cause: ' + JSON.stringify(error));
})
1.4.1 context.getAppVersionInfo
getAppVersionInfo():Promise
获取应用的版本信息,使用Promise方式作为异步方法.
1.4.2返回结果:Promise:返回应用版本信息.
AppVersionInfo参数详解
appName:应用名称
versionCode:应用版本号
versionName:应用版本名称
1.4.3准备资料需要在config.json中查找version标签,如下图所示
1.4.4 示例代码
import ability_featureAbility from '@ohos.ability.featureAbility'
var context = ability_featureAbility.getContext();
context.getAppVersionInfo()
.then((data) => {
console.info('Operation successful. Data: ' + JSON.stringify(data));
}).catch((error) => {
console.error('Operation failed. Cause: ' + JSON.stringify(error));
})
1.5.1 context.getBundleName
getBundleName(): Promise
获取Ability的所属的包名信息,使用Promise方式作为异步方法.
返回值:Promise:Promise形式返回Ability的所属的包名信息.
1.5.2参考配置文件的元素的bundleName.效果如如下
2. 代码实现
2.1需要在config.json 注册权限,参考配置文件的元素的reqPermissions
2.2目前申请权限如下
2.2.1 ohos.permission.READ_USER_STORAGE
2.2.2 ohos.permission.CAMERA
2.2.3 全部代码
import ability_featureAbility from '@ohos.ability.featureAbility'
@Entry
@Component
struct MyFeatureAbilityPage {
private myVerifyPermission() {
var context = ability_featureAbility.getContext();
let permission = "ohos.permission.CAMERA";
context.verifyPermission(permission, null)
.then((data) => {
if(data===-1){
console.log('当前没有权限 ' );
}else{
console.log('当前已具备权限 ' );
}
}).catch((error) => {
console.log('Operation failed. Cause: ' + JSON.stringify(error));
})
}
private MyRequestPermissionsFromUser() {
var context = ability_featureAbility.getContext();
let permissions = ["ohos.permission.CAMERA","ohos.permission.READ_USER_STORAGE"];
let requestCode = 123
context.requestPermissionsFromUser(permissions, requestCode)
.then((data) => {
console.log("请求码"+data.requestCode)
console.log("请求权限"+data.permissions.toString())
if(requestCode===data.requestCode){//用于判断返回的请求码和申请的请求是否相同
for(var i=0;i<data.permissions.length;i++){
if(data.authResults[i]==-1){
console.log("请求权限:"+data.permissions[i]+"==>请求状态是拒绝")
}else{
console.log("请求权限:"+data.permissions[i]+"==>请求状态是同意")
}
}
}
}).catch((error) => {
console.log('Operation failed. Cause: ' + JSON.stringify(error));
})
}
private MyGetAppVersionInfo() {
var context = ability_featureAbility.getContext();
context.getAppVersionInfo()
.then((data) => {
console.log("getAppVersionInfo===>应用名称:"+data.appName)
console.log("getAppVersionInfo===>versionCode:"+data.versionCode)
console.log("getAppVersionInfo===>versionName:"+data.versionName)
}).catch((error) => {
console.log('Operation failed. Cause: ' + JSON.stringify(error));
})
}
private myGetBundleName() {
var context = ability_featureAbility.getContext();
context.getBundleName()
.then((data) => {
console.log('getBundleName包名: ' + JSON.stringify(data));
}).catch((error) => {
console.log('Operation failed. Cause: ' + JSON.stringify(error));
})
}
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
Text('检查当前权限')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(this.myVerifyPermission.bind(this))
Text('申请权限')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(this.MyRequestPermissionsFromUser.bind(this))
.backgroundColor(Color.Red)
Text('获取版本信息')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(this.MyGetAppVersionInfo.bind(this))
Text('获取包名')
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(this.myGetBundleName.bind(this))
.backgroundColor(Color.Red)
}
.width('100%')
.height('100%')
}
}
3. 运行效果
欲了解更多更全技术文章,欢迎访问:https://developer.huawei.com/consumer/cn/forum/topic/0204809977852700206?fid=18?ha_source=zzh |
边栏推荐
- 汇编书摘抄
- C#异步和多线程
- 12.机器学习基础:评估机器学习模型
- js的垃圾回收机制
- Harmony OS Date ano UI 】 【 】 the basic operation
- 【Harmony OS】【FAQ】鸿蒙问题合集1
- 传统企业如何转型社交电商,泰山众筹的玩法有哪些?
- Kotlin-Flow常用封装类:StateFlow的使用
- Shenzhen Offline Registration|StarRocks on AWS: How to conduct rapid and unified analysis of real-time data warehouses
- Can Oracle EMCC be installed independently?Or does it have to be installed on the database server?
猜你喜欢
随机推荐
Flink state
Interface testing framework combat (3) | JSON request and response assertion
2022 the first of the new league henan (4) : zhengzhou university of light industry G - maze
Where is the value of testers
4.深度学习的几何解释与梯度的优化
[Developers must see] [push kit] Collection of typical problems of push service service 2
js中的闭包
closures in js
GIS数据漫谈(五)— 地理坐标系统
1.一个神经网络示例
StarRocks July Community Update
mysql 创建索引的三种方式
接口测试框架实战(二)| 接口请求断言
MOSN 反向通道详解
【Harmony OS】【ARK UI】Date 基本操作
浏览器监听标签页关闭
好消息!北京、珠海PMP考试时间来啦
我将GuiLite移植到了STM32F4开发板上
【开发者必看】【push kit】推送服务服务典型问题合集2
MCM箱模型建模方法及大气O3来源解析