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