# @ohos.commonEventManager (公共事件模块) 本模块提供了公共事件相关的能力,包括发布公共事件、订阅公共事件、以及退订公共事件。 > **说明:** > > 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 ## 导入模块 ```ts import { commonEventManager } from '@kit.BasicServicesKit'; ``` ## Support 系统公共事件是指由系统服务或系统应用发布的事件,订阅这些公共事件需要特定的权限、使用相应的值,详见[系统定义的公共事件](./common_event/commonEventManager-definitions.md)。 ## commonEventManager.publish publish(event: string, callback: AsyncCallback\): void 发布公共事件。使用callback异步回调。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | -------------------- | ---- | ---------------------- | | event | string | 是 | 表示要发送的公共事件。详见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。 | | callback | AsyncCallback\ | 是 | 回调函数。当公共事件发布成功时,err为undefined,否则为错误对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed. | | 1500007 | Failed to send the message to the common event service. | | 1500008 | Failed to initialize the common event service. | | 1500009 | Failed to obtain system parameters. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // 发布公共事件回调 function publishCB(err: BusinessError) { if (err) { console.error(`Failed to publish common event. Code is ${err.code}, message is ${err.message}`); return; } console.info(`Succeeded in publishing common event.`); } // 发布公共事件 try { commonEventManager.publish('event', publishCB); } catch (error) { let err: BusinessError = error as BusinessError; console.error(`Failed to publish common event. Code is ${err.code}, message is ${err.message}`); } ``` ## commonEventManager.publish publish(event: string, options: CommonEventPublishData, callback: AsyncCallback\): void 发布公共事件。使用callback异步回调。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------- | ---- | ---------------------- | | event | string | 是 | 表示要发布的公共事件。详见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。 | | options | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是 | 表示发布公共事件的属性。 | | callback | syncCallback\ | 是 | 回调函数。当公共事件发布成功时,err为undefined,否则为错误对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed. | | 1500007 | Failed to send the message to the common event service. | | 1500008 | Failed to initialize the common event service. | | 1500009 | Failed to obtain system parameters. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // 公共事件相关信息,以发布有序公共事件为例 let options: commonEventManager.CommonEventPublishData = { code: 0, data: 'initial data', isOrdered: true // 有序公共事件 } // 发布公共事件回调 function publishCB(err: BusinessError) { if (err) { console.error(`Failed to publish common event. Code is ${err.code}, message is ${err.message}`); return; } console.info(`Succeeded in publishing common event.`); } // 发布公共事件 try { commonEventManager.publish('event', options, publishCB); } catch (error) { let err: BusinessError = error as BusinessError; console.error(`Failed to publish common event. Code is ${err.code}, message is ${err.message}`); } ``` ## commonEventManager.createSubscriber createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\): void 创建订阅者。使用callback异步回调。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------------- | ------------------------------------------------------------ | ---- | -------------------------- | | subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是 | 表示订阅信息。 | | callback | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 是 | 回调函数。当公共事件订阅者创建成功时,err为undefined,否则为错误对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 let subscriber: commonEventManager.CommonEventSubscriber; // 订阅者信息 let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { events: ['event'] }; // 创建订阅者回调 function createCB(err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) { if(!err) { console.info(`Succeeded in creating subscriber.`); subscriber = commonEventSubscriber; return; } console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); } // 创建订阅者 try { commonEventManager.createSubscriber(subscribeInfo, createCB); } catch (error) { let err: BusinessError = error as BusinessError; console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); } ``` ## commonEventManager.createSubscriber createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\ 创建订阅者。使用Promise异步回调。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------------- | ----------------------------------------------------- | ---- | -------------- | | subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是 | 表示订阅信息。 | **返回值:** | 类型 | 说明 | | --------------------------------------------------------- | ---------------- | | Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 以Promise形式返回订阅者对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | PParameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 let subscriber: commonEventManager.CommonEventSubscriber; // 订阅者信息 let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { events: ['event'] }; // 创建订阅者 commonEventManager.createSubscriber(subscribeInfo).then((commonEventSubscriber: commonEventManager.CommonEventSubscriber) => { console.info(`Succeeded in creating subscriber.`); subscriber = commonEventSubscriber; }).catch((err: BusinessError) => { console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); }); ``` ## commonEventManager.createSubscriberSync10+ createSubscriberSync(subscribeInfo: CommonEventSubscribeInfo): CommonEventSubscriber createSubscriber的同步接口。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------------- | ----------------------------------------------------- | ---- | -------------- | | subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是 | 表示订阅信息。 | **返回值:** | 类型 | 说明 | | --------------------------------------------------------- | ---------------- | | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 返回订阅者对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | PParameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 let subscriber: commonEventManager.CommonEventSubscriber; // 订阅者信息 let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { events: ['event'] }; // 创建订阅者 try { subscriber = commonEventManager.createSubscriberSync(subscribeInfo); } catch (error) { let err: BusinessError = error as BusinessError; console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); } ``` ## commonEventManager.subscribe subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\): void 订阅公共事件。使用callback异步回调。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ---------------------------------------------------- | ---- | -------------------------------- | | subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是 | 表示订阅者对象。 | | callback | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | 是 | 回调函数。当公共事件订阅成功后,事件触发时执行的回调函数;否则订阅失败时,err为错误对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | PParameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed. | | 801 | capability not supported. | | 1500007 | Failed to send the message to the common event service. | | 1500008 | Failed to initialize the common event service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 let subscriber: commonEventManager.CommonEventSubscriber; // 订阅者信息 let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { events: ['event'] }; // 订阅公共事件回调 function SubscribeCB(err: BusinessError, data: commonEventManager.CommonEventData) { if (err) { console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`); return; } console.info(`Succeeded in subscribing, data is ${JSON.stringify(data)}`); } // 创建订阅者回调 function createCB(err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) { if(!err) { console.info(`Succeeded in creating subscriber.`); subscriber = commonEventSubscriber; // 订阅公共事件 try { commonEventManager.subscribe(subscriber, SubscribeCB); } catch (error) { let err: BusinessError = error as BusinessError; console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`); } return; } console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); } // 创建订阅者 try { commonEventManager.createSubscriber(subscribeInfo, createCB); } catch (error) { let err: BusinessError = error as BusinessError; console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); } ``` ## commonEventManager.unsubscribe unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\): void 取消订阅公共事件。使用callback异步回调。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---------- | ----------------------------------------------- | ---- | ------------------------ | | subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是 | 表示订阅者对象。 | | callback | AsyncCallback\ | 否 | 回调函数。当取消公共事件订阅成功时,err为undefined,否则为错误对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 | 错误码ID | 错误信息 | | -------- | ----------------------------------- | | 401 | Parameter error. Possible causes:
1. Mandatory parameters are left unspecified.
2. Incorrect parameter types.
3. Parameter verification failed. | | 801 | capability not supported. | | 1500007 | Failed to send the message to the common event service. | | 1500008 | Failed to initialize the common event service. | **示例:** ```ts import { BusinessError } from '@kit.BasicServicesKit'; // 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 let subscriber: commonEventManager.CommonEventSubscriber | undefined; // 订阅者信息 let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { events: ['event'] }; // 订阅公共事件回调 function subscribeCB(err: BusinessError, data: commonEventManager.CommonEventData) { if (err) { console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`); return; } console.info(`Succeeded in subscribing, data is ${JSON.stringify(data)}`); } // 创建订阅者回调 function createCB(err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) { if (err) { console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); return; } console.info(`Succeeded in creating subscriber.`); subscriber = commonEventSubscriber; // 订阅公共事件 try { commonEventManager.subscribe(subscriber, subscribeCB); } catch (error) { let err: BusinessError = error as BusinessError; console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`); } } // 取消订阅公共事件回调 function unsubscribeCB(err: BusinessError) { if (err) { console.error(`Failed to unsubscribe. Code is ${err.code}, message is ${err.message}`); return; } // subscriber不再使用时需要将其置为undefined,避免内存泄露 subscriber = undefined; console.info(`Succeeded in unsubscribing.`); } // 创建订阅者 try { commonEventManager.createSubscriber(subscribeInfo, createCB); } catch (error) { let err: BusinessError = error as BusinessError; console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); } // 取消订阅公共事件 // 等待异步接口subscribe执行完毕,开发者根据实际业务选择是否需要添加setTimeout setTimeout(() => { try { commonEventManager.unsubscribe(subscriber, unsubscribeCB); } catch (error) { let err: BusinessError = error as BusinessError; console.error(`Failed to unsubscribe. Code is ${err.code}, message is ${err.message}`); } }, 500); ``` ## CommonEventData10+ type CommonEventData = _CommonEventData 公共事件的数据。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent | 类型 | 说明 | | --- | --- | | [_CommonEventData](js-apis-inner-commonEvent-commonEventData.md) | 表示公共事件的数据。 | ## CommonEventSubscriber10+ type CommonEventSubscriber = _CommonEventSubscriber 描述公共事件的订阅者。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent | 类型 | 说明 | | --- | --- | | [_CommonEventSubscriber](js-apis-inner-commonEvent-commonEventSubscriber.md) | 描述公共事件的订阅者。 | ## CommonEventSubscribeInfo10+ type CommonEventSubscribeInfo = _CommonEventSubscribeInfo 用于表示订阅者的信息。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent | 类型 | 说明 | | --- | --- | | [_CommonEventSubscribeInfo](js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 用于表示订阅者的信息。 | ## CommonEventPublishData10+ type CommonEventPublishData = _CommonEventPublishData 描述公共事件内容和属性。 **原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 **系统能力:** SystemCapability.Notification.CommonEvent | 类型 | 说明 | | --- | --- | | [_CommonEventPublishData](js-apis-inner-commonEvent-commonEventPublishData.md) | 描述公共事件内容和属性。 |