文档管理中心
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明
API参考系统基础功能Basic Services Kit(基础服务)ArkTS API已停止维护的接口@ohos.commonEvent (公共事件模块)

@ohos.commonEvent (公共事件模块)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

本模块提供了公共事件的能力,包括公共事件的权限列表,发布公共事件,订阅或取消订阅公共事件,获取或修改公共事件结果代码、结果数据等。

说明

从API version 7 开始支持,从API version 9 开始废弃,建议使用@ohos.commonEventManager替代。

导入模块

收起
自动换行
深色代码主题
复制
  1. import commonEvent from '@ohos.commonEvent';

Support

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

系统公共事件是指由系统服务或系统应用发布的事件,订阅这些系统公共事件需要特定的权限。发布或订阅这些事件需要使用如下链接中的枚举定义。

全部系统公共事件枚举定义请参见系统公共事件定义

commonEvent.publish(deprecated)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

publish(event: string, callback: AsyncCallback<void>): void

以回调形式发布公共事件。

说明

从API version 7 开始支持,从API version 9 开始废弃,建议使用commonEventManager.publish替代。

系统能力: SystemCapability.Notification.CommonEvent

参数:

展开
参数名 类型 必填 说明
event string 表示要发布的公共事件。
callback AsyncCallback<void> 表示发布公共事件的回调方法。

示例:

收起
自动换行
深色代码主题
复制
  1. import Base from '@ohos.base';
  2. // 发布公共事件回调
  3. let publishCallBack = (err: Base.BusinessError) => {
  4. if (err.code) {
  5. console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
  6. } else {
  7. console.info('publish');
  8. }
  9. }
  10. // 发布公共事件
  11. commonEvent.publish("event", publishCallBack);

commonEvent.publish(deprecated)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

publish(event: string, options: CommonEventPublishData, callback: AsyncCallback<void>): void

以回调形式发布公共事件。

说明

从API version 7 开始支持,从API version 9 开始废弃,建议使用commonEventManager.publish替代。

系统能力: SystemCapability.Notification.CommonEvent

参数:

展开
参数名 类型 必填 说明
event string 表示要发布的公共事件。
options CommonEventPublishData 表示发布公共事件的属性。
callback AsyncCallback<void> 表示发布公共事件的回调方法。

示例:

收起
自动换行
深色代码主题
复制
  1. import Base from '@ohos.base';
  2. import CommonEventManager from '@ohos.commonEventManager';
  3. // 公共事件相关信息
  4. let options:CommonEventManager.CommonEventPublishData = {
  5. code: 0, // 公共事件的初始代码
  6. data: "initial data", // 公共事件的初始数据
  7. isOrdered: true // 有序公共事件
  8. };
  9. // 发布公共事件回调
  10. let publishCallBack = (err: Base.BusinessError) => {
  11. if (err.code) {
  12. console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
  13. } else {
  14. console.info("publish");
  15. }
  16. }
  17. // 发布公共事件
  18. commonEvent.publish("event", options, publishCallBack);

commonEvent.createSubscriber(deprecated)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback<CommonEventSubscriber>): void

以回调形式创建订阅者。

说明

从API version 7 开始支持,从API version 9 开始废弃,建议使用commonEventManager.createSubscriber替代。

系统能力: SystemCapability.Notification.CommonEvent

参数:

展开
参数名 类型 必填 说明
subscribeInfo CommonEventSubscribeInfo 表示订阅信息。
callback AsyncCallback<CommonEventSubscriber> 表示创建订阅者的回调方法。

示例:

收起
自动换行
深色代码主题
复制
  1. import Base from '@ohos.base';
  2. import CommonEventManager from '@ohos.commonEventManager';
  3. let subscriber:CommonEventManager.CommonEventSubscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及取消订阅的动作
  4. // 订阅者信息
  5. let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
  6. events: ["event"]
  7. };
  8. // 创建订阅者回调
  9. let createCallBack = (err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => {
  10. if (err.code) {
  11. console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
  12. } else {
  13. console.info("createSubscriber");
  14. subscriber = commonEventSubscriber;
  15. }
  16. }
  17. // 创建订阅者
  18. commonEvent.createSubscriber(subscribeInfo, createCallBack);

commonEvent.createSubscriber(deprecated)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise<CommonEventSubscriber>

以Promise形式创建订阅者。

说明

从API version 7 开始支持,从API version 9 开始废弃,建议使用commonEventManager.createSubscriber替代。

系统能力: SystemCapability.Notification.CommonEvent

参数:

展开
参数名 类型 必填 说明
subscribeInfo CommonEventSubscribeInfo 表示订阅信息。

返回值:

展开
类型 说明
Promise<CommonEventSubscriber> 返回订阅者对象。

示例:

收起
自动换行
深色代码主题
复制
  1. import Base from '@ohos.base';
  2. import CommonEventManager from '@ohos.commonEventManager';
  3. let subscriber:CommonEventManager.CommonEventSubscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及取消订阅的动作
  4. // 订阅者信息
  5. let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
  6. events: ["event"]
  7. };
  8. // 创建订阅者
  9. commonEvent.createSubscriber(subscribeInfo).then((commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => {
  10. console.info("createSubscriber");
  11. subscriber = commonEventSubscriber;
  12. }).catch((err:Base.BusinessError) => {
  13. console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
  14. });

commonEvent.subscribe(deprecated)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback<CommonEventData>): void

以回调形式订阅公共事件。

说明

从API version 7 开始支持,从API version 9 开始废弃,建议使用commonEventManager.subscribe替代。

系统能力: SystemCapability.Notification.CommonEvent

参数:

展开
参数名 类型 必填 说明
subscriber CommonEventSubscriber 表示订阅者对象。
callback AsyncCallback<CommonEventData> 表示接收公共事件数据的回调方法。

示例:

收起
自动换行
深色代码主题
复制
  1. import Base from '@ohos.base';
  2. import CommonEventManager from '@ohos.commonEventManager';
  3. let subscriber:CommonEventManager.CommonEventSubscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及取消订阅的动作
  4. // 订阅者信息
  5. let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
  6. events: ["event"]
  7. };
  8. // 订阅公共事件回调
  9. let subscribeCallBack = (err:Base.BusinessError, data:CommonEventManager.CommonEventData) => {
  10. if (err.code) {
  11. console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  12. } else {
  13. console.info("subscribe " + JSON.stringify(data));
  14. }
  15. }
  16. // 创建订阅者回调
  17. let createCallBack = (err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => {
  18. if (err.code) {
  19. console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
  20. } else {
  21. console.info("createSubscriber");
  22. subscriber = commonEventSubscriber;
  23. // 订阅公共事件
  24. commonEvent.subscribe(subscriber, subscribeCallBack);
  25. }
  26. }
  27. // 创建订阅者
  28. commonEvent.createSubscriber(subscribeInfo, createCallBack);

commonEvent.unsubscribe(deprecated)

Phone12+PC/2in113+Tablet12+TV19+Wearable18+

unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback<void>): void

以回调形式取消订阅公共事件。

说明

从API version 7 开始支持,从API version 9 开始废弃,建议使用commonEventManager.unsubscribe替代。

系统能力: SystemCapability.Notification.CommonEvent

参数:

展开
参数名 类型 必填 说明
subscriber CommonEventSubscriber 表示订阅者对象。
callback AsyncCallback<void> 表示取消订阅的回调方法。

示例:

收起
自动换行
深色代码主题
复制
  1. import Base from '@ohos.base';
  2. import CommonEventManager from '@ohos.commonEventManager';
  3. let subscriber:CommonEventManager.CommonEventSubscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及取消订阅的动作
  4. // 订阅者信息
  5. let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
  6. events: ["event"]
  7. };
  8. // 订阅公共事件回调
  9. let subscribeCallBack = (err:Base.BusinessError, data:CommonEventManager.CommonEventData) => {
  10. if (err.code) {
  11. console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
  12. } else {
  13. console.info("subscribe " + JSON.stringify(data));
  14. }
  15. }
  16. // 创建订阅者回调
  17. let createCallBack = (err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => {
  18. if (err.code) {
  19. console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
  20. } else {
  21. console.info("createSubscriber");
  22. subscriber = commonEventSubscriber;
  23. // 订阅公共事件
  24. commonEvent.subscribe(subscriber, subscribeCallBack);
  25. }
  26. }
  27. // 取消订阅公共事件回调
  28. let unsubscribeCallback = (err: Base.BusinessError) => {
  29. if (err.code) {
  30. console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
  31. } else {
  32. console.info("unsubscribe");
  33. }
  34. }
  35. // 创建订阅者
  36. commonEvent.createSubscriber(subscribeInfo, createCallBack);
  37. // 取消订阅公共事件
  38. // 注意:需在subscriber创建成功后(即createCallBack回调执行后)调用,此处仅展示API用法
  39. commonEvent.unsubscribe(subscriber, unsubscribeCallback);
在 API参考 中进行搜索
请输入您想要搜索的关键词