当前位置:网站首页>【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
边栏推荐
- Ali YunTianChi competition problem (machine learning) - O2O coupons prediction (complete code)
- Gopacket source code analysis
- 2022下半年深圳信息系统项目管理师认证招生简章
- JS-DOM-全局、局部、隐式变量,数组()\函数、 prompt输入对话框、confirm(确定用户的决定-弹出对话框)
- y91.第六章 微服务、服务网格及Envoy实战 -- 服务网格基础(二)
- 整除性质1
- B. Arrays Sum
- 【暑期每日一题】洛谷 P5724 【深基4.习5】求极差 / 最大跨度值
- HP路由器和交换机日志分析
- [OpenCV] - Find and draw contours
猜你喜欢

容易混淆的指针知识点

如何剪裁svg并压缩

杰理之SD卡切回蓝牙没有作用【篇】

杰理之ANC OFF语音没有作用【篇】
![换座位[异或巧妙的让奇偶互换]](/img/af/75964462af6fa550dc16eaf087e354.png)
换座位[异或巧妙的让奇偶互换]
![[Server data recovery] A case of data recovery when the Ext4 file system cannot be mounted and an error is reported after fsck](/img/65/f16bb466967993934719022145f78c.png)
[Server data recovery] A case of data recovery when the Ext4 file system cannot be mounted and an error is reported after fsck

The influence law of genes for disease - read the paper

杰理之一拖二 另一台手机超距 通话会无声【篇】

337. Robbery III

使用ceph-deploycep集群部署,并用3个磁盘作为专用osd
随机推荐
AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘
OKR管理过程中,如何运用CFR实现组织的高效对话、反馈和认可?
perl基础语法归纳
2022 High Voltage Electrician Exam Questions and Answers
Masked AutoEncoder论文及实现
mysql内容不存在的报错
2022 Security Officer-A Certificate Special Work Permit Exam Question Bank and Online Mock Exam
杰理之播放最大音量提示音播不出来【篇】
AttributeError: partially initialized module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'
EDI对接 New York & Company案例
JS-DOM--全局DOM对象document-history-location-navigator-screen-window
2022 High-altitude installation, maintenance, and demolition exam practice questions and mock exams
BaseDexClassLoader的正确使用方式
TASSEL软件导入plink格式文件报错
软件质效领航者 | 优秀案例•国金证券DevOps建设项目
杰理之电话打入,远端听不到声音【篇】
【开发者必看】【push kit】推送服务服务典型问题合集2
Ridge regression and LASSO regression
MySQL:已提交读和可重复读的实现原理 | MVCC(多版本并发控制)——笔记自用
Divisible property 1