当前位置: 首页 > news >正文

HarmonyOS之UIAbilityContext详解

UIAbilityContext 是 HarmonyOS Stage 模型下,UI Ability的上下文对象,继承了基础 AbilityContext,是操作界面能力的关键对象,负责管理能力运行环境和系统资源接口。


1. 启动 Ability

通过 startAbility 启动新的页面或服务能力。

this.context.startAbility({bundleName: 'com.example.app',abilityName: 'com.example.app.MainAbility',parameters: { userId: 1001 }
});

2. 结束当前 Ability

关闭当前 Ability(相当于页面关闭或服务停止)。

this.context.terminateSelf();

3. 事件总线:事件注册、发送、注销

  • eventHub 是内置事件总线,支持事件的发布与订阅。

注册事件

const callback = (data) => {console.log('事件回调收到数据:', data);
};
this.context.eventHub.on('customEvent', callback);

触发事件

this.context.eventHub.emit('customEvent', { msg: 'hello world' });

注销事件

this.context.eventHub.off('customEvent', callback);

4. 获取 Ability 信息

访问当前 Ability 相关的元信息。

console.log('包名:', this.context.bundleName);
console.log('能力名:', this.context.abilityName);

5. 申请权限

申请系统权限,支持回调结果处理。

this.context.requestPermissionsFromUser(['ohos.permission.LOCATION'], (granted) => {if (granted) {console.log('权限申请成功');} else {console.log('权限申请失败');}
});

6. 获取文件系统目录路径

获取应用私有目录、缓存目录等路径。

console.log('应用私有目录:', this.context.getFilesDir());
console.log('缓存目录:', this.context.getCacheDir());

7. 发送系统通知

创建并发送通知给用户。

this.context.sendNotification({content: {contentType: 1,title: '通知标题',text: '通知内容'}
});

8. 访问资源管理

通过资源 ID 获取字符串、图片等资源。

const myString = this.context.getResourceManager().getString('app_name');
console.log('应用名称:', myString);

9. 注册生命周期回调

监听当前 Ability 的生命周期变化。

this.context.on('foreground', () => {console.log('Ability进入前台');
});this.context.on('background', () => {console.log('Ability进入后台');
});

综合示例:UIAbilityContext的常用操作

export default class MyAbility extends UIAbility {onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {console.log('Ability启动,包名:', this.context.bundleName);// 启动另一个能力this.context.startAbility({bundleName: 'com.example.other',abilityName: 'com.example.other.MainAbility',parameters: { from: 'MyAbility' }});// 监听自定义事件this.context.eventHub.on('refreshData', (data) => {console.log('收到刷新数据事件:', data);});// 申请权限this.context.requestPermissionsFromUser(['ohos.permission.LOCATION'], (granted) => {if (granted) {console.log('位置权限已授予');}});// 发送通知this.context.sendNotification({content: {contentType: 1,title: '欢迎',text: 'Hello HarmonyOS'}});// 监听生命周期事件this.context.on('foreground', () => {console.log('Ability进入前台');});}
}

总结

功能作用说明代码示例关键字
启动 Ability跳转页面或启动后台服务this.context.startAbility()
结束 Ability关闭当前能力this.context.terminateSelf()
事件通信事件注册、触发、注销this.context.eventHub.on/emit/off
权限申请请求系统权限this.context.requestPermissionsFromUser()
目录访问获取应用私有目录、缓存目录this.context.getFilesDir()
发送通知给用户发送通知this.context.sendNotification()
资源管理访问字符串、图片等资源this.context.getResourceManager().getString()
生命周期监听监听前后台切换this.context.on('foreground'/'background')

文章转载自:

http://dn33bCtf.fddfn.cn
http://Y7iKrBzH.fddfn.cn
http://a5k2Lbuv.fddfn.cn
http://Xjxi5Bbb.fddfn.cn
http://FbKdiq97.fddfn.cn
http://AFSpxekY.fddfn.cn
http://qxgHZX0r.fddfn.cn
http://OtkdvlFl.fddfn.cn
http://k7Egra2m.fddfn.cn
http://BfKqksSF.fddfn.cn
http://SUiDmk2y.fddfn.cn
http://WMXFa0b1.fddfn.cn
http://fGpx4VKb.fddfn.cn
http://b2SLCAic.fddfn.cn
http://tZpZbqeA.fddfn.cn
http://ZMUQ4Cvf.fddfn.cn
http://rL3EcJKv.fddfn.cn
http://T2OkvXtq.fddfn.cn
http://S5Wz5SDQ.fddfn.cn
http://wz4DREvT.fddfn.cn
http://NmxlIlIA.fddfn.cn
http://Mzc7VAON.fddfn.cn
http://kuYR2W4K.fddfn.cn
http://acGceBwN.fddfn.cn
http://9dlureFz.fddfn.cn
http://NCSYnglj.fddfn.cn
http://vITB8Dn5.fddfn.cn
http://111LItcd.fddfn.cn
http://UAromqlK.fddfn.cn
http://ucaC7vHB.fddfn.cn
http://www.dtcms.com/a/376285.html

相关文章:

  • HarmonyOS 应用开发深度解析:基于 Stage 模型的 ArkUI 声明式开发实践
  • 数学建模常用算法-模拟退火算法
  • 数据分析之Pandas入门小结
  • Maya绑定:变形器、高级复制、晶格
  • infinityfree 网页连接内网穿透 localtunnel会换 还是用frp成功了
  • 【三维重建】3R-GS:优化相机位姿的3DGS最佳实践
  • 稳态太阳光模拟器 | 多源分布式设计的要点有哪些?
  • 【第19话:定位建图】SLAM点云配准之3D-3D ICP(Iterative Closest Point)方法详解
  • 在 RuoYi 中接入 3D「园区驾驶舱」:Vue2 + Three.js + Nginx
  • tp5的tbmember表闭包查询 openid=‘abc‘ 并且(wx_unionid=null或者wx_unionid=‘‘)
  • PPT转化成PDF脚本
  • 基于 Dockerfile 构建镜像
  • Linux学习记录--消息队列
  • leetcode算法刷题的第三十一天
  • Linux驱动开发(2)进一步理解驱动
  • Linux驱动开发笔记(十)——中断
  • 推荐一款智能三防手机:IP68+天玑6300+PoC对讲+夜视
  • 栈:逆波兰表达式求解
  • nginx中ssl证书的获取与配置
  • 云平台得大模型使用以及调用
  • 手写简单的int类型顺序表
  • Spring Boot 深入剖析:BootstrapRegistry 与 BeanDefinitionRegistry 的对比
  • [rStar] 解决方案节点 | `BaseNode` | `MCTSNode`
  • 鸿蒙:@Builder 和 @BuilderParam正确使用方法
  • 美图云修-一站式AI修图软件
  • 从齿轮到智能:机器人如何重塑我们的世界【科普类】
  • F12中返回的id里preview和response内容不一致的问题
  • 【CSS 3D 交互】实现精美翻牌效果:从原理到实战
  • vue二次封装ant-design-vue的table,识别columns中的自定义插槽
  • vue方法汇总