1# @ohos.commonEventManager (公共事件模块) 2 3本模块提供了公共事件相关的能力,包括发布公共事件、订阅公共事件、以及退订公共事件。 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```ts 12import { commonEventManager } from '@kit.BasicServicesKit'; 13``` 14 15## Support 16 17系统公共事件是指由系统服务或系统应用发布的事件,订阅这些公共事件需要特定的权限、使用相应的值,详见[系统定义的公共事件](./common_event/commonEventManager-definitions.md)。 18 19## commonEventManager.publish 20 21publish(event: string, callback: AsyncCallback\<void>): void 22 23发布公共事件。使用callback异步回调。 24 25**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 26 27**系统能力:** SystemCapability.Notification.CommonEvent 28 29**参数:** 30 31| 参数名 | 类型 | 必填 | 说明 | 32| -------- | -------------------- | ---- | ---------------------- | 33| event | string | 是 | 表示要发送的公共事件。详见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。 | 34| callback | AsyncCallback\<void> | 是 | 回调函数。当公共事件发布成功时,err为undefined,否则为错误对象。 | 35 36**错误码:** 37 38以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 39 40| 错误码ID | 错误信息 | 41| -------- | ----------------------------------- | 42| 1500003 | The common event sending frequency too high. | 43| 1500007 | Failed to send the message to the common event service. | 44| 1500008 | Failed to initialize the common event service. | 45| 1500009 | Failed to obtain system parameters. | 46 47**示例:** 48 49```ts 50import { BusinessError } from '@kit.BasicServicesKit'; 51 52// 发布公共事件 53try { 54 commonEventManager.publish('event', (err: BusinessError) => { 55 if (err) { 56 console.error(`Failed to publish common event. Code is ${err.code}, message is ${err.message}`); 57 return; 58 } 59 console.info(`Succeeded in publishing common event.`); 60 }); 61} catch (error) { 62 let err: BusinessError = error as BusinessError; 63 console.error(`Failed to publish common event. Code is ${err.code}, message is ${err.message}`); 64} 65``` 66 67## commonEventManager.publish 68 69publish(event: string, options: CommonEventPublishData, callback: AsyncCallback\<void>): void 70 71发布公共事件。使用callback异步回调。 72 73**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 74 75**系统能力:** SystemCapability.Notification.CommonEvent 76 77**参数:** 78 79| 参数名 | 类型 | 必填 | 说明 | 80| -------- | ---------------------- | ---- | ---------------------- | 81| event | string | 是 | 表示要发布的公共事件。详见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。 | 82| options | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是 | 表示发布公共事件的属性。 | 83| callback | AsyncCallback\<void> | 是 | 回调函数。当公共事件发布成功时,err为undefined,否则为错误对象。 | 84 85**错误码:** 86 87以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 88 89| 错误码ID | 错误信息 | 90| -------- | ----------------------------------- | 91| 1500003 | The common event sending frequency too high. | 92| 1500007 | Failed to send the message to the common event service. | 93| 1500008 | Failed to initialize the common event service. | 94| 1500009 | Failed to obtain system parameters. | 95 96**示例:** 97 98```ts 99import { BusinessError } from '@kit.BasicServicesKit'; 100 101// 公共事件相关信息,以发布有序公共事件为例 102let options: commonEventManager.CommonEventPublishData = { 103 code: 0, 104 data: 'initial data', 105 isOrdered: true // 有序公共事件 106} 107 108// 发布公共事件 109try { 110 commonEventManager.publish('event', options, (err: BusinessError) => { 111 if (err) { 112 console.error(`Failed to publish common event. Code is ${err.code}, message is ${err.message}`); 113 return; 114 } 115 console.info(`Succeeded in publishing common event.`); 116 }); 117} catch (error) { 118 let err: BusinessError = error as BusinessError; 119 console.error(`Failed to publish common event. Code is ${err.code}, message is ${err.message}`); 120} 121``` 122 123## commonEventManager.createSubscriber 124 125createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\<CommonEventSubscriber>): void 126 127创建订阅者。使用callback异步回调。 128 129**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 130 131**系统能力:** SystemCapability.Notification.CommonEvent 132 133**参数:** 134 135| 参数名 | 类型 | 必填 | 说明 | 136| ------------- | ------------------------------------------------------------ | ---- | -------------------------- | 137| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是 | 表示订阅信息。 | 138| callback | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 是 | 回调函数。当公共事件订阅者创建成功时,err为undefined,否则为错误对象。 | 139 140**错误码:** 141 142以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 143 144| 错误码ID | 错误信息 | 145| -------- | ----------------------------------- | 146| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 147 148**示例:** 149 150```ts 151import { BusinessError } from '@kit.BasicServicesKit'; 152 153// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 154let subscriber: commonEventManager.CommonEventSubscriber; 155// 订阅者信息 156let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { 157 events: ['event'] 158}; 159 160// 创建订阅者 161try { 162 commonEventManager.createSubscriber(subscribeInfo, 163 (err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) => { 164 if(!err) { 165 console.info(`Succeeded in creating subscriber.`); 166 subscriber = commonEventSubscriber; 167 return; 168 } 169 console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); 170 }); 171} catch (error) { 172 let err: BusinessError = error as BusinessError; 173 console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); 174} 175``` 176 177## commonEventManager.createSubscriber 178 179createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventSubscriber> 180 181创建订阅者。使用Promise异步回调。 182 183**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 184 185**系统能力:** SystemCapability.Notification.CommonEvent 186 187**参数:** 188 189| 参数名 | 类型 | 必填 | 说明 | 190| ------------- | ----------------------------------------------------- | ---- | -------------- | 191| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是 | 表示订阅信息。 | 192 193**返回值:** 194| 类型 | 说明 | 195| --------------------------------------------------------- | ---------------- | 196| Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 以Promise形式返回订阅者对象。 | 197 198**错误码:** 199 200以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 201 202| 错误码ID | 错误信息 | 203| -------- | ----------------------------------- | 204| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 205 206**示例:** 207 208```ts 209import { BusinessError } from '@kit.BasicServicesKit'; 210 211// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 212let subscriber: commonEventManager.CommonEventSubscriber; 213// 订阅者信息 214let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { 215 events: ['event'] 216}; 217// 创建订阅者 218commonEventManager.createSubscriber(subscribeInfo).then((commonEventSubscriber: commonEventManager.CommonEventSubscriber) => { 219 console.info(`Succeeded in creating subscriber.`); 220 subscriber = commonEventSubscriber; 221}).catch((err: BusinessError) => { 222 console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); 223}); 224``` 225 226## commonEventManager.createSubscriberSync<sup>10+</sup> 227 228createSubscriberSync(subscribeInfo: CommonEventSubscribeInfo): CommonEventSubscriber 229 230createSubscriber的同步接口。 231 232**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 233 234**系统能力:** SystemCapability.Notification.CommonEvent 235 236**参数:** 237 238| 参数名 | 类型 | 必填 | 说明 | 239| ------------- | ----------------------------------------------------- | ---- | -------------- | 240| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是 | 表示订阅信息。 | 241 242**返回值:** 243| 类型 | 说明 | 244| --------------------------------------------------------- | ---------------- | 245| [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 返回订阅者对象。 | 246 247**错误码:** 248 249以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 250 251| 错误码ID | 错误信息 | 252| -------- | ----------------------------------- | 253| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 254 255**示例:** 256 257```ts 258import { BusinessError } from '@kit.BasicServicesKit'; 259 260// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 261let subscriber: commonEventManager.CommonEventSubscriber; 262// 订阅者信息 263let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { 264 events: ['event'] 265}; 266// 创建订阅者 267try { 268 subscriber = commonEventManager.createSubscriberSync(subscribeInfo); 269} catch (error) { 270 let err: BusinessError = error as BusinessError; 271 console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); 272} 273``` 274 275## commonEventManager.subscribe 276 277subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEventData>): void 278 279订阅公共事件。使用callback异步回调。 280 281**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 282 283**系统能力:** SystemCapability.Notification.CommonEvent 284 285**参数:** 286 287| 参数名 | 类型 | 必填 | 说明 | 288| ---------- | ---------------------------------------------------- | ---- | -------------------------------- | 289| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是 | 表示订阅者对象。 | 290| callback | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | 是 | 回调函数。当公共事件订阅成功后,事件触发时执行的回调函数;否则订阅失败时,err为错误对象。 | 291 292**错误码:** 293 294以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 295 296| 错误码ID | 错误信息 | 297| -------- | ----------------------------------- | 298| 801 | capability not supported. | 299| 1500007 | Failed to send the message to the common event service. | 300| 1500008 | Failed to initialize the common event service. | 301| 1500010 | The count of subscriber exceed system specification. | 302 303**示例:** 304 305```ts 306import { BusinessError } from '@kit.BasicServicesKit'; 307 308// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 309let subscriber: commonEventManager.CommonEventSubscriber; 310// 订阅者信息 311let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { 312 events: ['event'] 313}; 314 315// 创建订阅者 316try { 317 commonEventManager.createSubscriber(subscribeInfo, 318 (err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) => { 319 if(!err) { 320 console.info(`Succeeded in creating subscriber.`); 321 subscriber = commonEventSubscriber; 322 // 订阅公共事件 323 try { 324 commonEventManager.subscribe(subscriber, (err: BusinessError, data: commonEventManager.CommonEventData) => { 325 if (err) { 326 console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`); 327 return; 328 } 329 console.info(`Succeeded in subscribing, data is ${JSON.stringify(data)}`); 330 }); 331 } catch (error) { 332 let err: BusinessError = error as BusinessError; 333 console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`); 334 } 335 return; 336 } 337 console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); 338 }); 339} catch (error) { 340 let err: BusinessError = error as BusinessError; 341 console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); 342} 343``` 344 345## commonEventManager.unsubscribe 346 347unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void 348 349取消订阅公共事件。使用callback异步回调。 350 351**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 352 353**系统能力:** SystemCapability.Notification.CommonEvent 354 355**参数:** 356 357| 参数名 | 类型 | 必填 | 说明 | 358| ---------- | ----------------------------------------------- | ---- | ------------------------ | 359| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是 | 表示订阅者对象。 | 360| callback | AsyncCallback\<void> | 否 | 回调函数。当取消公共事件订阅成功时,err为undefined,否则为错误对象。 | 361 362**错误码:** 363 364以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 365 366| 错误码ID | 错误信息 | 367| -------- | ----------------------------------- | 368| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 369| 801 | capability not supported. | 370| 1500007 | Failed to send the message to the common event service. | 371| 1500008 | Failed to initialize the common event service. | 372 373**示例:** 374 375```ts 376import { BusinessError } from '@kit.BasicServicesKit'; 377 378// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 379let subscriber: commonEventManager.CommonEventSubscriber | undefined; 380// 订阅者信息 381let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { 382 events: ['event'] 383}; 384 385// 创建订阅者 386try { 387 commonEventManager.createSubscriber(subscribeInfo, 388 (err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) => { 389 if(!err) { 390 console.info(`Succeeded in creating subscriber.`); 391 subscriber = commonEventSubscriber; 392 // 订阅公共事件 393 try { 394 commonEventManager.subscribe(subscriber, (err: BusinessError, data: commonEventManager.CommonEventData) => { 395 if (err) { 396 console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`); 397 return; 398 } 399 console.info(`Succeeded in subscribing, data is ${JSON.stringify(data)}`); 400 }); 401 } catch (error) { 402 let err: BusinessError = error as BusinessError; 403 console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`); 404 } 405 return; 406 } 407 console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); 408 }); 409} catch (error) { 410 let err: BusinessError = error as BusinessError; 411 console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); 412} 413 414// 取消订阅公共事件 415// 等待异步接口subscribe执行完毕,开发者根据实际业务选择是否需要添加setTimeout 416setTimeout(() => { 417 try { 418 commonEventManager.unsubscribe(subscriber, (err: BusinessError) => { 419 if (err) { 420 console.error(`Failed to unsubscribe. Code is ${err.code}, message is ${err.message}`); 421 return; 422 } 423 // subscriber不再使用时需要将其置为undefined,避免内存泄露 424 subscriber = undefined; 425 console.info(`Succeeded in unsubscribing.`); 426 }); 427 } catch (error) { 428 let err: BusinessError = error as BusinessError; 429 console.error(`Failed to unsubscribe. Code is ${err.code}, message is ${err.message}`); 430 } 431}, 500); 432``` 433 434## commonEventManager.subscribeToEvent<sup>20+</sup> 435 436subscribeToEvent(subscriber: CommonEventSubscriber, callback: Callback\<CommonEventData>): Promise\<void> 437 438订阅公共事件,并返回订阅成功或失败信息。使用Promise异步回调。 439 440**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 441 442**系统能力:** SystemCapability.Notification.CommonEvent 443 444**参数:** 445 446| 参数名 | 类型 | 必填 | 说明 | 447| ---------- | ---------------------------------------------------- | ---- | -------------------------------- | 448| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是 | 表示订阅者对象。 | 449| callback | Callback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | 是 | 表示接收公共事件数据的回调函数。 | 450 451**返回值:** 452| 类型 | 说明 | 453| --------------------------------------------------------- | ---------------- | 454| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 455 456**错误码:** 457 458以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 459 460| 错误码ID | 错误信息 | 461| -------- | ----------------------------------- | 462| 801 | Capability not supported. | 463| 1500007 | Failed to send the message to the common event service. | 464| 1500008 | Failed to initialize the common event service. | 465| 1500010 | The count of subscriber exceed system specification. | 466 467**示例:** 468 469```ts 470import { BusinessError } from '@kit.BasicServicesKit'; 471 472// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 473let subscriber: commonEventManager.CommonEventSubscriber; 474// 订阅者信息 475let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = { 476 events: ["event"] 477}; 478 479// 创建订阅者 480try { 481 commonEventManager.createSubscriber(subscribeInfo, 482 (err: BusinessError, commonEventSubscriber: commonEventManager.CommonEventSubscriber) => { 483 if (err) { 484 console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); 485 } else { 486 console.info(`Succeeded in creating subscriber.`); 487 subscriber = commonEventSubscriber; 488 // 订阅公共事件 489 try { 490 commonEventManager.subscribeToEvent(subscriber, (data: commonEventManager.CommonEventData) => { 491 console.info(`Succeeded to receive common event, data is ` + JSON.stringify(data)); 492 }).then(() => { 493 console.info(`Succeeded to subscribe.`); 494 }).catch((err: BusinessError) => { 495 console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`); 496 }); 497 } catch (error) { 498 let err: BusinessError = error as BusinessError; 499 console.error(`Failed to subscribe. Code is ${err.code}, message is ${err.message}`); 500 } 501 } 502 }); 503} catch (error) { 504 let err: BusinessError = error as BusinessError; 505 console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`); 506} 507``` 508 509## CommonEventData<sup>10+</sup> 510 511type CommonEventData = _CommonEventData 512 513表示公共事件的数据。 514 515**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 516 517**系统能力:** SystemCapability.Notification.CommonEvent 518 519| 类型 | 说明 | 520| --- | --- | 521| [_CommonEventData](js-apis-inner-commonEvent-commonEventData.md) | 表示公共事件的数据。 | 522 523## CommonEventSubscriber<sup>10+</sup> 524 525type CommonEventSubscriber = _CommonEventSubscriber 526 527描述公共事件的订阅者。 528 529**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 530 531**系统能力:** SystemCapability.Notification.CommonEvent 532 533| 类型 | 说明 | 534| --- | --- | 535| [_CommonEventSubscriber](js-apis-inner-commonEvent-commonEventSubscriber.md) | 描述公共事件的订阅者。 | 536 537## CommonEventSubscribeInfo<sup>10+</sup> 538 539type CommonEventSubscribeInfo = _CommonEventSubscribeInfo 540 541用于表示订阅者的信息。 542 543**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 544 545**系统能力:** SystemCapability.Notification.CommonEvent 546 547| 类型 | 说明 | 548| --- | --- | 549| [_CommonEventSubscribeInfo](js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 用于表示订阅者的信息。 | 550 551## CommonEventPublishData<sup>10+</sup> 552 553type CommonEventPublishData = _CommonEventPublishData 554 555描述公共事件内容和属性。 556 557**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 558 559**系统能力:** SystemCapability.Notification.CommonEvent 560 561| 类型 | 说明 | 562| --- | --- | 563| [_CommonEventPublishData](js-apis-inner-commonEvent-commonEventPublishData.md) | 描述公共事件内容和属性。 | 564 565