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