1# CommonEventSubscriber 2 3描述公共事件的订阅者。 4 5> **说明:** 6> 7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 使用说明 10 11在使用CommonEventSubscriber的功能前,需要通过commonEventManager.createSubscriber获取subscriber对象。 12 13```ts 14import { commonEventManager } from '@kit.BasicServicesKit'; 15import { BusinessError } from '@kit.BasicServicesKit'; 16 17// 定义订阅者,用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作 18let subscriber:commonEventManager.CommonEventSubscriber; 19// 订阅者信息 20let subscribeInfo:commonEventManager.CommonEventSubscribeInfo = { 21 events: ["event"] 22}; 23// 创建订阅者回调 24function createCB(err: BusinessError, commonEventSubscriber:commonEventManager.CommonEventSubscriber) { 25 if (err != null) { 26 console.error(`createSubscriber failed, code is ${err.code}`); 27 } else { 28 console.info("createSubscriber success"); 29 subscriber = commonEventSubscriber; 30 } 31} 32// 创建订阅者 33commonEventManager.createSubscriber(subscribeInfo, createCB); 34``` 35 36## getCode 37 38getCode(callback: AsyncCallback\<number>): void 39 40获取有序公共事件代码。使用callback异步回调。 41 42**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 43 44**系统能力**:`SystemCapability.Notification.CommonEvent` 45 46**参数:** 47 48| 参数名 | 类型 | 必填 | 说明 | 49| -------- | ---------------------- | ---- | ------------------ | 50| callback | AsyncCallback\<number\> | 是 | 回调函数。返回公共事件代码。 | 51 52**错误码:** 53 54以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 55 56| 错误码ID | 错误信息 | 57| -------- | ----------------------------------- | 58| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 59 60**示例:** 61 62```ts 63// 获取有序公共事件代码回调 64function getCodeCallback(err: BusinessError, code:number) { 65 if (err != null) { 66 console.error(`getCode failed, code is ${err.code}, message is ${err.message}`); 67 } else { 68 console.info("getCode " + JSON.stringify(code)); 69 } 70} 71subscriber.getCode(getCodeCallback); 72``` 73 74## getCode 75 76getCode(): Promise\<number> 77 78获取有序公共事件代码。使用Promise异步回调。 79 80**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 81 82**系统能力**:`SystemCapability.Notification.CommonEvent` 83 84**返回值:** 85 86| 类型 | 说明 | 87| ---------------- | -------------------- | 88| Promise\<number> | Promise对象。返回公共事件代码。 | 89 90**示例:** 91 92```ts 93subscriber.getCode().then((code:number) => { 94 console.info("getCode " + JSON.stringify(code)); 95}).catch((err: BusinessError) => { 96 console.error(`getCode failed, code is ${err.code}, message is ${err.message}`); 97}); 98``` 99 100## getCodeSync<sup>10+</sup> 101 102getCodeSync(): number 103 104获取有序公共事件代码。 105 106**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 107 108**系统能力**:`SystemCapability.Notification.CommonEvent` 109 110**返回值:** 111 112| 类型 | 说明 | 113| ---------------- | -------------------- | 114| number | 公共事件代码。 | 115 116**示例:** 117 118```ts 119let code = subscriber.getCodeSync(); 120console.info("getCodeSync " + JSON.stringify(code)); 121``` 122 123## setCode 124 125setCode(code: number, callback: AsyncCallback\<void>): void 126 127设置有序公共事件的代码。使用callback异步回调。 128 129**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 130 131**系统能力**:`SystemCapability.Notification.CommonEvent` 132 133**参数:** 134 135| 参数名 | 类型 | 必填 | 说明 | 136| -------- | -------------------- | ---- | ---------------------- | 137| code | number | 是 | 公共事件的代码。 | 138| callback | AsyncCallback\<void> | 是 | 回调函数。表示被指定的回调方法。 | 139 140**错误码:** 141 142以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 143 144| 错误码ID | 错误信息 | 145| -------- | ----------------------------------- | 146| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 147 148**示例:** 149 150```ts 151// 设置有序公共事件的代码回调 152function setCodeCallback(err: BusinessError) { 153 if (err != null) { 154 console.error(`setCode failed, code is ${err.code}, message is ${err.message}`); 155 } else { 156 console.info("setCode success"); 157 } 158} 159subscriber.setCode(1, setCodeCallback); 160``` 161 162## setCode 163 164setCode(code: number): Promise\<void> 165 166设置有序公共事件的代码。使用Promise异步回调。 167 168**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 169 170**系统能力**:`SystemCapability.Notification.CommonEvent` 171 172**参数:** 173 174| 参数名 | 类型 | 必填 | 说明 | 175| ------ | ------ | ---- | ------------------ | 176| code | number | 是 | 公共事件的代码。 | 177 178**返回值:** 179 180| 类型 | 说明 | 181| ---------------- | -------------------- | 182| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 183 184**错误码:** 185 186以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 187 188| 错误码ID | 错误信息 | 189| -------- | ----------------------------------- | 190| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 191 192**示例:** 193 194```ts 195subscriber.setCode(1).then(() => { 196 console.info("setCode success"); 197}).catch((err: BusinessError) => { 198 console.error(`setCode failed, code is ${err.code}, message is ${err.message}`); 199}); 200``` 201 202## setCodeSync<sup>10+</sup> 203 204setCodeSync(code: number): void 205 206设置有序公共事件的代码。 207 208**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 209 210**系统能力**:`SystemCapability.Notification.CommonEvent` 211 212**参数:** 213 214| 参数名 | 类型 | 必填 | 说明 | 215| ------ | ------ | ---- | ------------------ | 216| code | number | 是 | 公共事件的代码。 | 217 218**错误码:** 219 220以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 221 222| 错误码ID | 错误信息 | 223| -------- | ----------------------------------- | 224| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 225 226**示例:** 227 228```ts 229try { 230 subscriber.setCodeSync(1); 231} catch (error) { 232 let err: BusinessError = error as BusinessError; 233 console.error(`setCodeSync failed, code is ${err.code}, message is ${err.message}`); 234} 235``` 236 237## getData 238 239getData(callback: AsyncCallback\<string>): void 240 241获取有序公共事件的数据。使用callback异步回调。 242 243**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 244 245**系统能力**:`SystemCapability.Notification.CommonEvent` 246 247**参数:** 248 249| 参数名 | 类型 | 必填 | 说明 | 250| -------- | ---------------------- | ---- | -------------------- | 251| callback | AsyncCallback\<string> | 是 | 回调函数。返回公共事件的数据。 | 252 253**错误码:** 254 255以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 256 257| 错误码ID | 错误信息 | 258| -------- | ----------------------------------- | 259| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 260 261**示例:** 262 263```ts 264// 获取有序公共事件代码数据回调 265function getDataCallback(err: BusinessError, data:string) { 266 if (err != null) { 267 console.error(`getData failed, code is ${err.code}, message is ${err.message}`); 268 } else { 269 console.info("getData " + JSON.stringify(data)); 270 } 271} 272subscriber.getData(getDataCallback); 273``` 274 275## getData 276 277getData(): Promise\<string> 278 279获取有序公共事件的数据。使用Promise异步回调。 280 281**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 282 283**系统能力**:`SystemCapability.Notification.CommonEvent` 284 285**返回值:** 286 287| 类型 | 说明 | 288| ---------------- | ------------------ | 289| Promise\<string> | Promise对象。返回公共事件的数据。 | 290 291**示例:** 292 293```ts 294subscriber.getData().then((data:string) => { 295 console.info("getData " + JSON.stringify(data)); 296}).catch((err: BusinessError) => { 297 console.error(`getData failed, code is ${err.code}, message is ${err.message}`); 298}); 299``` 300 301## getDataSync<sup>10+</sup> 302 303getDataSync(): string 304 305获取有序公共事件的数据。 306 307**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 308 309**系统能力**:`SystemCapability.Notification.CommonEvent` 310 311**返回值:** 312 313| 类型 | 说明 | 314| ---------------- | ------------------ | 315| string | 公共事件的数据。 | 316 317**示例:** 318 319```ts 320let data = subscriber.getDataSync(); 321console.info("getDataSync " + JSON.stringify(data)); 322``` 323 324## setData 325 326setData(data: string, callback: AsyncCallback\<void>): void 327 328设置有序公共事件的数据。使用callback异步回调。 329 330**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 331 332**系统能力**:`SystemCapability.Notification.CommonEvent` 333 334**参数:** 335 336| 参数名 | 类型 | 必填 | 说明 | 337| -------- | -------------------- | ---- | -------------------- | 338| data | string | 是 | 公共事件的数据。 | 339| callback | AsyncCallback\<void> | 是 | 回调函数。表示被指定的回调方法。 | 340 341**错误码:** 342 343以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 344 345| 错误码ID | 错误信息 | 346| -------- | ----------------------------------- | 347| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 348 349**示例:** 350 351```ts 352// 设置有序公共事件的结果数据回调 353function setDataCallback(err: BusinessError) { 354 if (err != null) { 355 console.error(`setData failed, code is ${err.code}, message is ${err.message}`); 356 } else { 357 console.info("setData success"); 358 } 359} 360subscriber.setData("publish_data_changed", setDataCallback); 361``` 362 363## setData 364 365setData(data: string): Promise\<void> 366 367设置有序公共事件的数据。使用Promise异步回调。 368 369**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 370 371**系统能力**:`SystemCapability.Notification.CommonEvent` 372 373**参数:** 374 375| 参数名 | 类型 | 必填 | 说明 | 376| ------ | ------ | ---- | -------------------- | 377| data | string | 是 | 公共事件的数据。 | 378 379**返回值:** 380 381| 类型 | 说明 | 382| ---------------- | -------------------- | 383| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 384 385**错误码:** 386 387以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 388 389| 错误码ID | 错误信息 | 390| -------- | ----------------------------------- | 391| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 392 393**示例:** 394 395```ts 396subscriber.setData("publish_data_changed").then(() => { 397 console.info("setData success"); 398}).catch((err: BusinessError) => { 399 console.error(`setData failed, code is ${err.code}, message is ${err.message}`); 400}); 401``` 402 403## setDataSync<sup>10+</sup> 404 405setDataSync(data: string): void 406 407设置有序公共事件的数据。 408 409**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 410 411**系统能力**:`SystemCapability.Notification.CommonEvent` 412 413**参数:** 414 415| 参数名 | 类型 | 必填 | 说明 | 416| ------ | ------ | ---- | -------------------- | 417| data | string | 是 | 公共事件的数据。 | 418 419**错误码:** 420 421以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 422 423| 错误码ID | 错误信息 | 424| -------- | ----------------------------------- | 425| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 426 427**示例:** 428 429```ts 430try { 431 subscriber.setDataSync("publish_data_changed"); 432} catch (error) { 433 let err: BusinessError = error as BusinessError; 434 console.error(`setDataSync failed, code is ${err.code}, message is ${err.message}`); 435} 436``` 437 438## setCodeAndData 439 440setCodeAndData(code: number, data: string, callback:AsyncCallback\<void>): void 441 442设置有序公共事件代码和数据。使用callback异步回调。 443 444**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 445 446**系统能力**:`SystemCapability.Notification.CommonEvent` 447 448**参数:** 449 450| 参数名 | 类型 | 必填 | 说明 | 451| -------- | -------------------- | ---- | ---------------------- | 452| code | number | 是 | 公共事件的代码。 | 453| data | string | 是 | 公共事件的数据。 | 454| callback | AsyncCallback\<void> | 是 | 回调函数。表示被指定的回调方法。 | 455 456**错误码:** 457 458以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 459 460| 错误码ID | 错误信息 | 461| -------- | ----------------------------------- | 462| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 463 464**示例:** 465 466```ts 467// 设置有序公共事件的代码和数据回调 468function setCodeAndDataCallback(err: BusinessError) { 469 if (err != null) { 470 console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`); 471 } else { 472 console.info("setCodeAndData success"); 473 } 474} 475subscriber.setCodeAndData(1, "publish_data_changed", setCodeAndDataCallback); 476``` 477 478## setCodeAndData 479 480setCodeAndData(code: number, data: string): Promise\<void> 481 482设置有序公共事件的代码和数据。使用Promise异步回调。 483 484**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 485 486**系统能力**:`SystemCapability.Notification.CommonEvent` 487 488**参数:** 489 490| 参数名 | 类型 | 必填 | 说明 | 491| ------ | ------ | ---- | -------------------- | 492| code | number | 是 | 公共事件的代码。 | 493| data | string | 是 | 公共事件的数据。 | 494 495**返回值:** 496 497| 类型 | 说明 | 498| ---------------- | -------------------- | 499| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 500 501**错误码:** 502 503以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 504 505| 错误码ID | 错误信息 | 506| -------- | ----------------------------------- | 507| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 508 509**示例:** 510 511```ts 512subscriber.setCodeAndData(1, "publish_data_changed").then(() => { 513 console.info("setCodeAndData success"); 514}).catch((err: BusinessError) => { 515 console.error(`setCodeAndData failed, code is ${err.code}, message is ${err.message}`); 516}); 517``` 518 519## setCodeAndDataSync<sup>10+</sup> 520 521setCodeAndDataSync(code: number, data: string): void 522 523设置有序公共事件的代码和数据。 524 525**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 526 527**系统能力**:`SystemCapability.Notification.CommonEvent` 528 529**参数:** 530 531| 参数名 | 类型 | 必填 | 说明 | 532| ------ | ------ | ---- | -------------------- | 533| code | number | 是 | 公共事件的代码。 | 534| data | string | 是 | 公共事件的数据。 | 535 536**错误码:** 537 538以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 539 540| 错误码ID | 错误信息 | 541| -------- | ----------------------------------- | 542| 401 | Parameter error. Possible causes:<br>1. Mandatory parameters are left unspecified.<br>2. Incorrect parameter types.<br>3. Parameter verification failed. | 543 544**示例:** 545 546```ts 547try { 548 subscriber.setCodeAndDataSync(1, "publish_data_changed"); 549} catch (error) { 550 let err: BusinessError = error as BusinessError; 551 console.error(`setCodeAndDataSync failed, code is ${err.code}, message is ${err.message}`); 552} 553 554``` 555 556## isOrderedCommonEvent 557 558isOrderedCommonEvent(callback: AsyncCallback\<boolean>): void 559 560查询当前公共事件是否为有序公共事件。使用callback异步回调。 561 562**系统能力**:`SystemCapability.Notification.CommonEvent` 563 564**参数:** 565 566| 参数名 | 类型 | 必填 | 说明 | 567| -------- | ----------------------- | ---- | ---------------------------------- | 568| callback | AsyncCallback\<boolean> | 是 | 回调函数。返回true表示有序公共事件;false表示无序公共事件。 | 569 570**错误码:** 571 572以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 573 574| 错误码ID | 错误信息 | 575| -------- | ----------------------------------- | 576| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 577 578**示例:** 579 580```ts 581// 获取当前公共事件是否为有序事件的回调 582function isOrderedCommonEventCallback(err: BusinessError, isOrdered:boolean) { 583 if (err != null) { 584 console.error(`isOrderedCommonEvent failed, code is ${err.code}, message is ${err.message}`); 585 } else { 586 console.info("isOrderedCommonEvent " + JSON.stringify(isOrdered)); 587 } 588} 589subscriber.isOrderedCommonEvent(isOrderedCommonEventCallback); 590``` 591 592## isOrderedCommonEvent 593 594isOrderedCommonEvent(): Promise\<boolean> 595 596查询当前公共事件是否为有序公共事件。使用Promise异步回调。 597 598**系统能力**:`SystemCapability.Notification.CommonEvent` 599 600**返回值:** 601 602| 类型 | 说明 | 603| ----------------- | -------------------------------- | 604| Promise\<boolean> | Promise对象。返回true表示有序公共事件;false表示无序公共事件。 | 605 606**示例:** 607 608```ts 609subscriber.isOrderedCommonEvent().then((isOrdered:boolean) => { 610 console.info("isOrderedCommonEvent " + JSON.stringify(isOrdered)); 611}).catch((err: BusinessError) => { 612 console.error(`isOrderedCommonEvent failed, code is ${err.code}, message is ${err.message}`); 613}); 614``` 615 616## isOrderedCommonEventSync<sup>10+</sup> 617 618isOrderedCommonEventSync(): boolean 619 620查询当前公共事件是否为有序公共事件。 621 622**系统能力**:`SystemCapability.Notification.CommonEvent` 623 624**返回值:** 625 626| 类型 | 说明 | 627| ----------------- | -------------------------------- | 628| boolean | 返回true表示有序公共事件;false表示无序公共事件。 | 629 630**示例:** 631 632```ts 633let isOrdered = subscriber.isOrderedCommonEventSync(); 634console.info("isOrderedCommonEventSync " + JSON.stringify(isOrdered)); 635``` 636 637## isStickyCommonEvent 638 639isStickyCommonEvent(callback: AsyncCallback\<boolean>): void 640 641检查当前公共事件是否为一个粘性事件。使用callback异步回调。 642 643**系统能力**:`SystemCapability.Notification.CommonEvent` 644 645**参数:** 646 647| 参数名 | 类型 | 必填 | 说明 | 648| -------- | ----------------------- | ---- | ---------------------------------- | 649| callback | AsyncCallback\<boolean> | 是 | 回调函数。返回true表示是粘性公共事件;false表示不是粘性公共事件。 | 650 651**错误码:** 652 653以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 654 655| 错误码ID | 错误信息 | 656| -------- | ----------------------------------- | 657| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 658 659**示例:** 660 661```ts 662// 获取当前公共事件是否为粘性事件的回调 663function isStickyCommonEventCallback(err: BusinessError, isSticky:boolean) { 664 if (err != null) { 665 console.error(`isStickyCommonEvent failed, code is ${err.code}, message is ${err.message}`); 666 } else { 667 console.info("isStickyCommonEvent " + JSON.stringify(isSticky)); 668 } 669} 670subscriber.isStickyCommonEvent(isStickyCommonEventCallback); 671``` 672 673## isStickyCommonEvent 674 675isStickyCommonEvent(): Promise\<boolean> 676 677检查当前公共事件是否为一个粘性事件。使用Promise异步回调。 678 679**系统能力**:`SystemCapability.Notification.CommonEvent` 680 681**返回值:** 682 683| 类型 | 说明 | 684| ----------------- | -------------------------------- | 685| Promise\<boolean> | Promise对象。返回true表示是粘性公共事件;false表示不是粘性公共事件。 | 686 687**示例:** 688 689```ts 690subscriber.isStickyCommonEvent().then((isSticky:boolean) => { 691 console.info("isStickyCommonEvent " + JSON.stringify(isSticky)); 692}).catch((err: BusinessError) => { 693 console.error(`isStickyCommonEvent failed, code is ${err.code}, message is ${err.message}`); 694}); 695``` 696 697## isStickyCommonEventSync<sup>10+</sup> 698 699isStickyCommonEventSync(): boolean 700 701检查当前公共事件是否为一个粘性事件。 702 703**系统能力**:`SystemCapability.Notification.CommonEvent` 704 705**返回值:** 706 707| 类型 | 说明 | 708| ----------------- | -------------------------------- | 709| boolean | 返回true表示是粘性公共事件;false表示不是粘性公共事件。 | 710 711**示例:** 712 713```ts 714let isSticky = subscriber.isStickyCommonEventSync(); 715console.info("isStickyCommonEventSync " + JSON.stringify(isSticky)); 716``` 717 718## abortCommonEvent 719 720abortCommonEvent(callback: AsyncCallback\<void>): void 721 722该接口与[finishCommonEvent](#finishcommonevent9)配合使用,可以中止当前的有序公共事件,使该公共事件不再向下一个订阅者传递。使用callback异步回调。 723 724**系统能力**:`SystemCapability.Notification.CommonEvent` 725 726**参数:** 727 728| 参数名 | 类型 | 必填 | 说明 | 729| -------- | -------------------- | ---- | -------------------- | 730| callback | AsyncCallback\<void> | 是 | 回调函数。表示被指定的回调方法。 | 731 732**错误码:** 733 734以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 735 736| 错误码ID | 错误信息 | 737| -------- | ----------------------------------- | 738| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 739 740**示例:** 741 742```ts 743// 中止当前的有序公共事件的回调 744function abortCommonEventCallback(err: BusinessError) { 745 if (err != null) { 746 console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 747 } else { 748 console.info("abortCommonEvent success"); 749 } 750} 751function finishCommonEventCallback(err: BusinessError) { 752 if (err != null) { 753 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 754 } else { 755 console.info("finishCommonEvent success"); 756 } 757} 758subscriber.abortCommonEvent(abortCommonEventCallback); 759subscriber.finishCommonEvent(finishCommonEventCallback); 760``` 761 762## abortCommonEvent 763 764abortCommonEvent(): Promise\<void> 765 766该接口与[finishCommonEvent](#finishcommonevent9)配合使用,可以中止当前的有序公共事件,使该公共事件不再向下一个订阅者传递。使用Promise异步回调。 767 768**系统能力**:`SystemCapability.Notification.CommonEvent` 769 770**返回值:** 771 772| 类型 | 说明 | 773| ---------------- | -------------------- | 774| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 775 776**示例:** 777 778```ts 779subscriber.abortCommonEvent().then(() => { 780 console.info("abortCommonEvent success"); 781}).catch((err: BusinessError) => { 782 console.error(`abortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 783}); 784subscriber.finishCommonEvent().then(() => { 785 console.info("finishCommonEvent success"); 786}).catch((err: BusinessError) => { 787 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 788}); 789``` 790 791## abortCommonEventSync<sup>10+</sup> 792 793abortCommonEventSync(): void 794 795该接口与[finishCommonEvent](#finishcommonevent9)配合使用,可以中止当前的有序公共事件,使该公共事件不再向下一个订阅者传递。 796 797**系统能力**:`SystemCapability.Notification.CommonEvent` 798 799**示例:** 800 801```ts 802subscriber.abortCommonEventSync(); 803subscriber.finishCommonEvent().then(() => { 804 console.info("finishCommonEvent success"); 805}).catch((err: BusinessError) => { 806 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 807}); 808``` 809 810## clearAbortCommonEvent 811 812clearAbortCommonEvent(callback: AsyncCallback\<void>): void 813 814该接口与[finishCommonEvent](#finishcommonevent9)配合使用,可以取消当前有序公共事件的中止状态,使该公共事件继续向下一个订阅者传递。使用callback异步回调。 815 816**系统能力**:`SystemCapability.Notification.CommonEvent` 817 818**参数:** 819 820| 参数名 | 类型 | 必填 | 说明 | 821| -------- | -------------------- | ---- | -------------------- | 822| callback | AsyncCallback\<void> | 是 | 回调函数。表示被指定的回调方法。 | 823 824**错误码:** 825 826以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 827 828| 错误码ID | 错误信息 | 829| -------- | ----------------------------------- | 830| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 831 832**示例:** 833 834```ts 835// 取消当前有序公共事件的中止状态的回调 836function clearAbortCommonEventCallback(err: BusinessError) { 837 if (err != null) { 838 console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 839 } else { 840 console.info("clearAbortCommonEvent success"); 841 } 842} 843function finishCommonEventCallback(err: BusinessError) { 844 if (err != null) { 845 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 846 } else { 847 console.info("finishCommonEvent success"); 848 } 849} 850subscriber.clearAbortCommonEvent(clearAbortCommonEventCallback); 851subscriber.finishCommonEvent(finishCommonEventCallback); 852``` 853 854## clearAbortCommonEvent 855 856clearAbortCommonEvent(): Promise\<void> 857 858该接口与[finishCommonEvent](#finishcommonevent9)配合使用,可以取消当前有序公共事件的中止状态,使该公共事件继续向下一个订阅者传递。使用Promise异步回调。 859 860**系统能力**:`SystemCapability.Notification.CommonEvent` 861 862**返回值:** 863 864| 类型 | 说明 | 865| ---------------- | -------------------- | 866| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 867 868**示例:** 869 870```ts 871subscriber.clearAbortCommonEvent().then(() => { 872 console.info("clearAbortCommonEvent success"); 873}).catch((err: BusinessError) => { 874 console.error(`clearAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 875}); 876subscriber.finishCommonEvent().then(() => { 877 console.info("finishCommonEvent success"); 878}).catch((err: BusinessError) => { 879 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 880}); 881``` 882 883## clearAbortCommonEventSync<sup>10+</sup> 884 885clearAbortCommonEventSync(): void 886 887该接口与[finishCommonEvent](#finishcommonevent9)配合使用,可以取消当前有序公共事件的中止状态,使该公共事件继续向下一个订阅者传递。 888 889**系统能力**:`SystemCapability.Notification.CommonEvent` 890 891**示例:** 892 893```ts 894subscriber.clearAbortCommonEventSync(); 895subscriber.finishCommonEvent().then(() => { 896 console.info("finishCommonEvent success"); 897}).catch((err: BusinessError) => { 898 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 899}); 900``` 901 902## getAbortCommonEvent 903 904getAbortCommonEvent(callback: AsyncCallback\<boolean>): void 905 906获取当前有序公共事件是否处于中止状态。使用callback异步回调。 907 908**系统能力**:`SystemCapability.Notification.CommonEvent` 909 910**参数:** 911 912| 参数名 | 类型 | 必填 | 说明 | 913| -------- | ----------------------- | ---- | ---------------------------------- | 914| callback | AsyncCallback\<boolean> | 是 | 回调函数。返回true表示当前有序公共事件处于中止状态;false表示当前有序公共事件没有处于中止状态。 | 915 916**错误码:** 917 918以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 919 920| 错误码ID | 错误信息 | 921| -------- | ----------------------------------- | 922| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 923 924**示例:** 925 926```ts 927// 获取当前有序公共事件是否处于中止状态回调 928function getAbortCommonEventCallback(err: BusinessError, abortEvent:boolean) { 929 if (err != null) { 930 console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 931 } else { 932 console.info("getAbortCommonEvent " + JSON.stringify(abortEvent)); 933 } 934} 935subscriber.getAbortCommonEvent(getAbortCommonEventCallback); 936``` 937 938## getAbortCommonEvent 939 940getAbortCommonEvent(): Promise\<boolean> 941 942获取当前有序公共事件是否处于中止状态。使用Promise异步回调。 943 944**系统能力**:`SystemCapability.Notification.CommonEvent` 945 946**返回值:** 947 948| 类型 | 说明 | 949| ----------------- | ---------------------------------- | 950| Promise\<boolean> | Promise对象。返回true表示当前有序公共事件处于中止状态;false表示当前有序公共事件没有处于中止状态。 | 951 952**示例:** 953 954```ts 955subscriber.getAbortCommonEvent().then((abortEvent:boolean) => { 956 console.info("getAbortCommonEvent " + JSON.stringify(abortEvent)); 957}).catch((err: BusinessError) => { 958 console.error(`getAbortCommonEvent failed, code is ${err.code}, message is ${err.message}`); 959}); 960``` 961 962## getAbortCommonEventSync<sup>10+</sup> 963 964getAbortCommonEventSync(): boolean 965 966获取当前有序公共事件是否处于中止状态。 967 968**系统能力**:`SystemCapability.Notification.CommonEvent` 969 970**返回值:** 971 972| 类型 | 说明 | 973| ----------------- | ---------------------------------- | 974| boolean | 返回true表示当前有序公共事件处于中止状态;false表示当前有序公共事件没有处于中止状态。 | 975 976**示例:** 977 978```ts 979let abortEvent = subscriber.getAbortCommonEventSync(); 980console.info("getAbortCommonEventSync " + JSON.stringify(abortEvent)); 981``` 982 983## getSubscribeInfo 984 985getSubscribeInfo(callback: AsyncCallback\<CommonEventSubscribeInfo>): void 986 987获取订阅者的订阅信息。使用callback异步回调。 988 989**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 990 991**系统能力**:`SystemCapability.Notification.CommonEvent` 992 993**参数:** 994 995| 参数名 | 类型 | 必填 | 说明 | 996| -------- | ------------------------------------------------------------ | ---- | ---------------------- | 997| callback | AsyncCallback\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | 是 | 回调函数。返回订阅者的订阅信息。 | 998 999**错误码:** 1000 1001以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1002 1003| 错误码ID | 错误信息 | 1004| -------- | ----------------------------------- | 1005| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 1006 1007**示例:** 1008 1009```ts 1010// 获取订阅者信息回调 1011function getSubscribeInfoCallback(err: BusinessError, subscribeInfo:commonEventManager.CommonEventSubscribeInfo) { 1012 if (err != null) { 1013 console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`); 1014 } else { 1015 console.info("getSubscribeInfo " + JSON.stringify(subscribeInfo)); 1016 } 1017} 1018subscriber.getSubscribeInfo(getSubscribeInfoCallback); 1019``` 1020 1021## getSubscribeInfo 1022 1023getSubscribeInfo(): Promise\<CommonEventSubscribeInfo> 1024 1025获取订阅者的订阅信息。使用Promise异步回调。 1026 1027**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1028 1029**系统能力**:`SystemCapability.Notification.CommonEvent` 1030 1031**返回值:** 1032 1033| 类型 | 说明 | 1034| ------------------------------------------------------------ | ---------------------- | 1035| Promise\<[CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)> | Promise对象。返回订阅者的订阅信息。 | 1036 1037**示例:** 1038 1039```ts 1040subscriber.getSubscribeInfo().then((subscribeInfo:commonEventManager.CommonEventSubscribeInfo) => { 1041 console.info("getSubscribeInfo " + JSON.stringify(subscribeInfo)); 1042}).catch((err: BusinessError) => { 1043 console.error(`getSubscribeInfo failed, code is ${err.code}, message is ${err.message}`); 1044}); 1045``` 1046 1047## getSubscribeInfoSync<sup>10+</sup> 1048 1049getSubscribeInfoSync(): CommonEventSubscribeInfo 1050 1051获取订阅者的订阅信息。 1052 1053**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 1054 1055**系统能力**:`SystemCapability.Notification.CommonEvent` 1056 1057**返回值:** 1058 1059| 类型 | 说明 | 1060| ------------------------------------------------------------ | ---------------------- | 1061| [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 表示订阅者的订阅信息。 | 1062 1063**示例:** 1064 1065```ts 1066let subscribeInfo = subscriber.getSubscribeInfoSync(); 1067console.info("getSubscribeInfoSync " + JSON.stringify(subscribeInfo)); 1068``` 1069 1070## finishCommonEvent<sup>9+</sup> 1071 1072finishCommonEvent(callback: AsyncCallback\<void>): void 1073 1074用于订阅者结束对当前有序公共事件的处理。使用callback异步回调。 1075 1076**系统能力**:`SystemCapability.Notification.CommonEvent` 1077 1078**参数:** 1079 1080| 参数名 | 类型 | 必填 | 说明 | 1081| -------- | -------------------- | ---- | -------------------------------- | 1082| callback | AsyncCallback\<void> | 是 | 回调函数。表示被指定的回调方法。 | 1083 1084**错误码:** 1085 1086以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 1087 1088| 错误码ID | 错误信息 | 1089| -------- | ----------------------------------- | 1090| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 1091 1092**示例:** 1093 1094```ts 1095// 结束订阅者对当前有序公共事件处理的回调 1096function finishCommonEventCallback(err: BusinessError) { 1097 if (err != null) { 1098 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 1099 } else { 1100 console.info("finishCommonEvent success"); 1101 } 1102} 1103subscriber.finishCommonEvent(finishCommonEventCallback); 1104``` 1105 1106## finishCommonEvent<sup>9+</sup> 1107 1108finishCommonEvent(): Promise\<void> 1109 1110用于订阅者结束对当前有序公共事件的处理。使用Promise异步回调。 1111 1112**系统能力**:`SystemCapability.Notification.CommonEvent` 1113 1114**返回值:** 1115 1116| 类型 | 说明 | 1117| ---------------- | -------------------- | 1118| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 1119 1120**示例:** 1121 1122```ts 1123subscriber.finishCommonEvent().then(() => { 1124 console.info("finishCommonEvent success"); 1125}).catch((err: BusinessError) => { 1126 console.error(`finishCommonEvent failed, code is ${err.code}, message is ${err.message}`); 1127}); 1128```