当前位置:网站首页>【Harmony OS】【ARK UI】公共事件模块
【Harmony OS】【ARK UI】公共事件模块
2022-08-09 04:44:00 【华为开发者论坛】
今天讲解一下公共事件模块功能的实现,主要从“参考资料”,“Api说明”,“代码实现”,“运行效果”,几个方面进行描述
参考文档
参考资料 公共事件模块
Api说明
commonEvent.createSubscriber
createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback): void
以异步方法构造CommonEventSubscriber对象,创建公共事件订阅者对象,使用callback形式返回。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
subscribeInfo | CommonEventSubscribeInfo | 是 | 公共事件订阅者信息。 |
callback | AsyncCallback | 是 | 创建公共事件订阅者的回调函数。 |
示例代码
var CommonEventSubscribeInfo = {events: ['publish_event']};commonEvent.createSubscriber(CommonEventSubscribeInfo, (error, subscriber) => { if (error != null) { console.error('Operation failed. Cause:' + JSON.stringify(error)); } else { var mySubscriber = subscriber; console.info('createSubscriber success. ' + JSON.stringify(subscriber)); } });
commonEvent.subscribe
subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback): void
以异步方法订阅公共事件,使用callback形式返回。在调用subscriber接口订阅公共事件前,需要先通过createSubscriber接口创建一个CommonEventSubscriber对象。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
subscriber | CommonEventSubscriber | 是 | 公共事件订阅者对象 |
callback | AsyncCallback | 是 | 事件监听回调函数,当收到订阅的事件时,回调订阅的事件数据。 |
示例代码
commonEvent.subscribe(mySubscriber, (error, commonEventData) => { //mySubscriber为创建的CommonEventSubscriber对象 if (error != null) { console.error('Operation failed. Cause: ' + JSON.stringify(error)); } else { console.info('subscribe callback: ' + commonEventData); }});
commonEvent.unsubscribe
unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback): void
以异步方法取消订阅公共事件,使用callback形式返回。与subscribe接口相同,需要传入一个已创建的CommonEventSubscriber对象。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
subscriber | CommonEventSubscriber | 是 | 公共事件订阅者对象 |
callback | AsyncCallback | 否 | 取消订阅回调函数 |
示例:
commonEvent.unsubscribe(mySubscriber, (error, commonEventData) => { //mySubscriber为创建的CommonEventSubscriber对象 if (error != null) { console.error('Operation failed. Cause: ' + JSON.stringify(error)); } else { console.info('unsubscribe success'); }});
补充说明
CommonEventSubscribeInfo
订阅公共事件可选的配置信息。
名称 | 可读 | 可写 | 类型 | 必填 | 描述 |
---|---|---|---|---|---|
events | 是 | 是 | Array | 是 | 需要订阅的公共事件。 |
publisherPermission | 是 | 是 | string | 否 | 发布者的权限。(暂不支持) |
publisherDeviceId | 是 | 是 | string | 否 | 设备ID。(暂不支持) |
userId | 是 | 是 | number | 否 | 用户ID。 |
priority | 是 | 是 | number | 否 | 订阅者优先级,仅用于有序公共事件场景,优先级较高的订阅者将首先收到此类事件。范围为:[-100,1000]。 |
示例
var CommonEventSubscribeInfo = {events: ['test_event']};var CommonEventSubscribeInfo = {events: ['test_event1', 'test_event2'], userId:1, priority:100};
CommonEventData
公共事件的回调数据。
名称 | 可读 | 可写 | 类型 | 必填 | 描述 |
---|---|---|---|---|---|
event | 是 | 是 | string | 是 | 公共事件名称。 |
bundleName | 是 | 是 | string | 否 | 包名称。 |
code | 是 | 是 | number | 否 | 有序公共事件的结果码,默认值为0。 |
data | 是 | 是 | string | 否 | 有序公共事件的结果数据,默认值为空字符串。 |
parameters | 是 | 是 | object | 否 | 公共事件的参数说明。 |
代码实现
// @ts-nocheckimport commonEvent from '@ohos.commonEvent';@[email protected] MyCommonEvent { @State result:string="结果:"; mysubscriber:CommonEventSubscriber;//todo 订阅通知 private aboutToAppear(){ var that=this; var CommonEventSubscribeInfo = {events: ['publish_event']}; //todo 创建订阅 commonEvent.createSubscriber(CommonEventSubscribeInfo, (error, subscriber) => { if (error != null) { console.error('Operation failed. Cause:' + JSON.stringify(error)); } else { that.mysubscriber=subscriber; console.info('createSubscriber success. ' + JSON.stringify(subscriber)); //todo 开始订阅 commonEvent.subscribe(subscriber, (error, commonEventData) => { //mySubscriber为创建的CommonEventSubscriber对象 if (error != null) { console.error('Operation failed. Cause: ' + JSON.stringify(error)); } else { console.log('subscribe callback: ' + JSON.stringify(commonEventData)); that.result= JSON.stringify(commonEventData) } }); } }); } private aboutToDisappear() { //取消订阅 commonEvent.unsubscribe(this.mysubscriber, (error, commonEventData) => { //mySubscriber为创建的CommonEventSubscriber对象 if (error != null) { console.error('Operation failed. Cause: ' + JSON.stringify(error)); } else { console.log('unsubscribe success'); } }); } build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Button('Send Common').onClick(event => { this.broadcastInternal() }) Text(this.result).width("100%").height("30%").fontSize(20).textAlign(TextAlign.Center) } .width('100%') .height('100%') } //todo 发送信息 private broadcastInternal() { // 自定义事件,只支持string类型数据 var data = { code: 1, data: "111111", isOrdered: true } commonEvent.publish("publish_event", data, (error, value) => { if (error != null) { console.error('broadcast ---> publish failed. Cause: ' + JSON.stringify(error)); } else { console.log('broadcast ---> publish common event success. ' + JSON.stringify(value)); } }); }}
运行效果
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh
边栏推荐
- JS-DOM--全局DOM对象document-history-location-navigator-screen-window
- Dingding conflicts with RStudio shortcuts--Dingding shortcut settings
- ddr系统检验
- 杰理之SD卡切回蓝牙没有作用【篇】
- 基于ABP和Magicodes实现Excel导出操作
- 【luogu U142356】勇者的后缀(SA)(主席树)(二分)
- 2022 High Voltage Electrician Exam Questions and Answers
- 自动化测试-图片中添加文字注释,添加到allure测试报告中
- 2022年低压电工练习题及模拟考试
- Query the size of the total points obtained in a certain time period to sort
猜你喜欢
[OpenCV] - Find and draw contours
容易混淆的指针知识点
Alibaba Cloud Tianchi Contest Question (Machine Learning) - Repeat Purchase Prediction of Tmall Users (Complete Code)
阿里云天池大赛赛题(机器学习)——O2O优惠券预测(完整代码)
软件质效领航者 | 优秀案例•国金证券DevOps建设项目
TASSEL软件导入plink格式文件报错
JS-全局dom对象的使用---使用htm样式和js函数动作的完全分离
MySQL:redo log日志——笔记自用
Construction and practice of full stack code test coverage and use case discovery system
2022R1快开门式压力容器操作考试模拟100题及在线模拟考试
随机推荐
Golang 常见知识点整理
数量遗传学遗传力计算1:亲子回归方法
杰理之智能充电仓低电发码关机 触摸不开机【篇】
MySQL: Implementation Principles of Submitted Read and Repeatable Read | MVCC (Multi-Version Concurrency Control) - Notes for Your Own Use
【学习笔记】AGC044
Gopacket source code analysis
2022年低压电工练习题及模拟考试
JS-DOM-全局、局部、隐式变量,数组()\函数、 prompt输入对话框、confirm(确定用户的决定-弹出对话框)
【OpenCV】-查找并绘制轮廓
【暑期每日一题】洛谷 P1048 [NOIP2005 普及组] 采药
【开发者必看】【push kit】推送服务服务典型问题合集2
自动化测试-图片中添加文字注释,添加到allure测试报告中
杰理之播歌曲前后音量大小不一样【篇】
2022 Security Officer-B Certificate Exam Practice Questions and Online Mock Exam
杰理之播放最大音量提示音播不出来【篇】
AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘
【数学建模绘图系列教程】绘图模板总结
etcd Study Notes - Getting Started
OKR管理过程中,如何运用CFR实现组织的高效对话、反馈和认可?
ceph create pool, map, delete exercises