1e41f4b71Sopenharmony_ci# @ohos.commonEvent (公共事件模块) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci本模块提供了公共事件的能力,包括公共事件的权限列表,发布公共事件,订阅或取消订阅公共事件,获取或修改公共事件结果代码、结果数据等。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> - 从API Version 9开始,该接口不再维护,推荐使用新接口[@ohos.commonEventManager](js-apis-commonEventManager.md)。 7e41f4b71Sopenharmony_ci> 8e41f4b71Sopenharmony_ci> - 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 导入模块 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci```ts 13e41f4b71Sopenharmony_ciimport CommonEvent from '@ohos.commonEvent'; 14e41f4b71Sopenharmony_ci``` 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Support 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci系统公共事件是指由系统服务或系统应用发布的事件,订阅这些系统公共事件需要特定的权限。发布或订阅这些事件需要使用如下链接中的枚举定义。 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci全部系统公共事件枚举定义请参见[系统公共事件定义](./common_event/commonEvent-definitions.md)。 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## CommonEvent.publish<sup>(deprecated)</sup> 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_cipublish(event: string, callback: AsyncCallback\<void>): void 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci发布公共事件(回调形式)。 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci> **说明:** 29e41f4b71Sopenharmony_ci> 从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.publish](js-apis-commonEventManager.md#commoneventmanagerpublish)替代。 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**系统能力:** `SystemCapability.Notification.CommonEvent` 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**参数:** 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 36e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------------------- | 37e41f4b71Sopenharmony_ci| event | string | 是 | 表示要发送的公共事件。 | 38e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**示例:** 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci```ts 43e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci//发布公共事件回调 46e41f4b71Sopenharmony_cifunction publishCB(err:Base.BusinessError) { 47e41f4b71Sopenharmony_ci if (err.code) { 48e41f4b71Sopenharmony_ci console.error(`publish failed, code is ${err.code}`); 49e41f4b71Sopenharmony_ci } else { 50e41f4b71Sopenharmony_ci console.info("publish"); 51e41f4b71Sopenharmony_ci } 52e41f4b71Sopenharmony_ci} 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci//发布公共事件 55e41f4b71Sopenharmony_ciCommonEvent.publish("event", publishCB); 56e41f4b71Sopenharmony_ci``` 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci## CommonEvent.publish<sup>(deprecated)</sup> 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_cipublish(event: string, options: CommonEventPublishData, callback: AsyncCallback\<void>): void 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci以回调的形式发布公共事件。 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci> **说明:** 65e41f4b71Sopenharmony_ci> 从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.publish](js-apis-commonEventManager.md#commoneventmanagerpublish-1)替代。 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**系统能力:** `SystemCapability.Notification.CommonEvent` 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**参数:** 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 72e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ---------------------- | 73e41f4b71Sopenharmony_ci| event | string | 是 | 表示要发布的公共事件。 | 74e41f4b71Sopenharmony_ci| options | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是 | 表示发布公共事件的属性。 | 75e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci**示例:** 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci```ts 81e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 82e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager'; 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci//公共事件相关信息 85e41f4b71Sopenharmony_cilet options:CommonEventManager.CommonEventPublishData = { 86e41f4b71Sopenharmony_ci code: 0, //公共事件的初始代码 87e41f4b71Sopenharmony_ci data: "initial data",//公共事件的初始数据 88e41f4b71Sopenharmony_ci isOrdered: true //有序公共事件 89e41f4b71Sopenharmony_ci} 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci//发布公共事件回调 92e41f4b71Sopenharmony_cifunction publishCB(err:Base.BusinessError) { 93e41f4b71Sopenharmony_ci if (err.code) { 94e41f4b71Sopenharmony_ci console.error(`publish failed, code is ${err.code}`); 95e41f4b71Sopenharmony_ci } else { 96e41f4b71Sopenharmony_ci console.info("publish"); 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci} 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci//发布公共事件 101e41f4b71Sopenharmony_ciCommonEvent.publish("event", options, publishCB); 102e41f4b71Sopenharmony_ci``` 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci## CommonEvent.createSubscriber<sup>(deprecated)</sup> 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_cicreateSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\<CommonEventSubscriber>): void 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci以回调形式创建订阅者。 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci> **说明:** 111e41f4b71Sopenharmony_ci>从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.createSubscriber](js-apis-commonEventManager.md#commoneventmanagercreatesubscriber)替代。 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci**系统能力:** `SystemCapability.Notification.CommonEvent` 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**参数:** 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 118e41f4b71Sopenharmony_ci| ------------- | ------------------------------------------------------------ | ---- | -------------------------- | 119e41f4b71Sopenharmony_ci| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是 | 表示订阅信息。 | 120e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 是 | 表示创建订阅者的回调方法。 | 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci**示例:** 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci```ts 126e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 127e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager'; 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_cilet subscriber:CommonEventManager.CommonEventSubscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci// 订阅者信息 132e41f4b71Sopenharmony_cilet subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 133e41f4b71Sopenharmony_ci events: ["event"] 134e41f4b71Sopenharmony_ci}; 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci// 创建订阅者回调 137e41f4b71Sopenharmony_cifunction createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) { 138e41f4b71Sopenharmony_ci if (err.code) { 139e41f4b71Sopenharmony_ci console.error(`createSubscriber failed, code is ${err.code}`); 140e41f4b71Sopenharmony_ci } else { 141e41f4b71Sopenharmony_ci console.info("createSubscriber"); 142e41f4b71Sopenharmony_ci subscriber = commonEventSubscriber; 143e41f4b71Sopenharmony_ci } 144e41f4b71Sopenharmony_ci} 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci// 创建订阅者 147e41f4b71Sopenharmony_ciCommonEvent.createSubscriber(subscribeInfo, createCB); 148e41f4b71Sopenharmony_ci``` 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci## CommonEvent.createSubscriber<sup>(deprecated)</sup> 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_cicreateSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventSubscriber> 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci以Promise形式创建订阅者。 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci> **说明:** 157e41f4b71Sopenharmony_ci>从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.createSubscriber](js-apis-commonEventManager.md#commoneventmanagercreatesubscriber-1)替代。 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci**系统能力:** `SystemCapability.Notification.CommonEvent` 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci**参数:** 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 164e41f4b71Sopenharmony_ci| ------------- | ----------------------------------------------------- | ---- | -------------- | 165e41f4b71Sopenharmony_ci| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是 | 表示订阅信息。 | 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci**返回值:** 168e41f4b71Sopenharmony_ci| 类型 | 说明 | 169e41f4b71Sopenharmony_ci| --------------------------------------------------------- | ---------------- | 170e41f4b71Sopenharmony_ci| Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 返回订阅者对象。 | 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci**示例:** 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci```ts 175e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 176e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager'; 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_cilet subscriber:CommonEventManager.CommonEventSubscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci// 订阅者信息 181e41f4b71Sopenharmony_cilet subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 182e41f4b71Sopenharmony_ci events: ["event"] 183e41f4b71Sopenharmony_ci}; 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci// 创建订阅者 186e41f4b71Sopenharmony_ciCommonEvent.createSubscriber(subscribeInfo).then((commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => { 187e41f4b71Sopenharmony_ci console.info("createSubscriber"); 188e41f4b71Sopenharmony_ci subscriber = commonEventSubscriber; 189e41f4b71Sopenharmony_ci}).catch((err:Base.BusinessError) => { 190e41f4b71Sopenharmony_ci console.error(`createSubscriber failed, code is ${err.code}`); 191e41f4b71Sopenharmony_ci}); 192e41f4b71Sopenharmony_ci``` 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci## CommonEvent.subscribe<sup>(deprecated)</sup> 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_cisubscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEventData>): void 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci以回调形式订阅公共事件。 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci> **说明:** 201e41f4b71Sopenharmony_ci>从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.subscribe](js-apis-commonEventManager.md#commoneventmanagersubscribe)替代。 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci**系统能力:** `SystemCapability.Notification.CommonEvent` 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci**参数:** 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 208e41f4b71Sopenharmony_ci| ---------- | ---------------------------------------------------- | ---- | -------------------------------- | 209e41f4b71Sopenharmony_ci| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是 | 表示订阅者对象。 | 210e41f4b71Sopenharmony_ci| callback | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | 是 | 表示接收公共事件数据的回调函数。 | 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci**示例:** 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci```ts 215e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 216e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager'; 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_cilet subscriber:CommonEventManager.CommonEventSubscriber;// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci// 订阅者信息 221e41f4b71Sopenharmony_cilet subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 222e41f4b71Sopenharmony_ci events: ["event"] 223e41f4b71Sopenharmony_ci}; 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci// 订阅公共事件回调 226e41f4b71Sopenharmony_cifunction subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) { 227e41f4b71Sopenharmony_ci if (err.code) { 228e41f4b71Sopenharmony_ci console.error(`subscribe failed, code is ${err.code}`); 229e41f4b71Sopenharmony_ci } else { 230e41f4b71Sopenharmony_ci console.info("subscribe " + JSON.stringify(data)); 231e41f4b71Sopenharmony_ci } 232e41f4b71Sopenharmony_ci} 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ci// 创建订阅者回调 235e41f4b71Sopenharmony_cifunction createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) { 236e41f4b71Sopenharmony_ci if (err.code) { 237e41f4b71Sopenharmony_ci console.error(`createSubscriber failed, code is ${err.code}`); 238e41f4b71Sopenharmony_ci } else { 239e41f4b71Sopenharmony_ci console.info("createSubscriber"); 240e41f4b71Sopenharmony_ci subscriber = commonEventSubscriber; 241e41f4b71Sopenharmony_ci // Subscribe to a common event. 242e41f4b71Sopenharmony_ci CommonEvent.subscribe(subscriber, subscribeCB); 243e41f4b71Sopenharmony_ci } 244e41f4b71Sopenharmony_ci} 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci// 创建订阅者 247e41f4b71Sopenharmony_ciCommonEvent.createSubscriber(subscribeInfo, createCB); 248e41f4b71Sopenharmony_ci``` 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci## CommonEvent.unsubscribe<sup>(deprecated)</sup> 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ciunsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci以回调形式取消订阅公共事件。 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci> **说明:** 257e41f4b71Sopenharmony_ci>从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.subscribe](js-apis-commonEventManager.md#commoneventmanagerunsubscribe)替代。 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci**系统能力:** `SystemCapability.Notification.CommonEvent` 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci**参数:** 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 264e41f4b71Sopenharmony_ci| ---------- | ----------------------------------------------- | ---- | ------------------------ | 265e41f4b71Sopenharmony_ci| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是 | 表示订阅者对象。 | 266e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 否 | 表示取消订阅的回调方法。 | 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci**示例:** 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci```ts 271e41f4b71Sopenharmony_ciimport Base from '@ohos.base'; 272e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager'; 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_cilet subscriber:CommonEventManager.CommonEventSubscriber; // 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci// 订阅者信息 277e41f4b71Sopenharmony_cilet subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = { 278e41f4b71Sopenharmony_ci events: ["event"] 279e41f4b71Sopenharmony_ci}; 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci// 订阅公共事件回调 282e41f4b71Sopenharmony_cifunction subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) { 283e41f4b71Sopenharmony_ci if (err.code) { 284e41f4b71Sopenharmony_ci console.error(`subscribe failed, code is ${err.code}`); 285e41f4b71Sopenharmony_ci } else { 286e41f4b71Sopenharmony_ci console.info("subscribe " + JSON.stringify(data)); 287e41f4b71Sopenharmony_ci } 288e41f4b71Sopenharmony_ci} 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci// 创建订阅者回调 291e41f4b71Sopenharmony_cifunction createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) { 292e41f4b71Sopenharmony_ci if (err.code) { 293e41f4b71Sopenharmony_ci console.error(`createSubscriber failed, code is ${err.code}`); 294e41f4b71Sopenharmony_ci } else { 295e41f4b71Sopenharmony_ci console.info("createSubscriber"); 296e41f4b71Sopenharmony_ci subscriber = commonEventSubscriber; 297e41f4b71Sopenharmony_ci // Subscribe to a common event. 298e41f4b71Sopenharmony_ci CommonEvent.subscribe(subscriber, subscribeCB); 299e41f4b71Sopenharmony_ci } 300e41f4b71Sopenharmony_ci} 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci// 取消订阅公共事件回调 303e41f4b71Sopenharmony_cifunction unsubscribeCB(err:Base.BusinessError) { 304e41f4b71Sopenharmony_ci if (err.code) { 305e41f4b71Sopenharmony_ci console.error(`unsubscribe failed, code is ${err.code}`); 306e41f4b71Sopenharmony_ci } else { 307e41f4b71Sopenharmony_ci console.info("unsubscribe"); 308e41f4b71Sopenharmony_ci } 309e41f4b71Sopenharmony_ci} 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci// 创建订阅者 312e41f4b71Sopenharmony_ciCommonEvent.createSubscriber(subscribeInfo, createCB); 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci// 取消订阅公共事件 315e41f4b71Sopenharmony_ciCommonEvent.unsubscribe(subscriber, unsubscribeCB); 316e41f4b71Sopenharmony_ci```