1e41f4b71Sopenharmony_ci# @ohos.commonEventManager (公共事件模块)(系统应用) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci本模块提供了公共事件相关的能力,包括发布公共事件、订阅公共事件以及退订公共事件。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> 当前界面仅包含本模块的系统接口,其他公开接口参见[CommonEventManager](./js-apis-commonEventManager.md)。 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## 导入模块 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_ciimport { commonEventManager } from '@kit.BasicServicesKit'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Support 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci系统公共事件是指由系统服务或系统应用发布的事件,订阅这些系统公共事件需要特定的权限。发布或订阅这些事件需要使用如下链接中的枚举定义。 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci全部系统公共事件枚举定义请参见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## commonEventManager.publishAsUser<sup> 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_cipublishAsUser(event: string, userId: number, callback: AsyncCallback\<void>): void 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci以回调的形式向指定用户发布公共事件。 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**系统API**:此接口为系统接口,三方应用不支持调用。 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**参数:** 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 36e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------------------------------- | 37e41f4b71Sopenharmony_ci| event | string | 是 | 表示要发送的公共事件。详见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。 | 38e41f4b71Sopenharmony_ci| userId | number | 是 | 表示指定向该用户ID发送此公共事件。 | 39e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**错误码:** 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 46e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 47e41f4b71Sopenharmony_ci| 202 | not system app. | 48e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 49e41f4b71Sopenharmony_ci| 1500004 | A third-party application cannot send system common events. | 50e41f4b71Sopenharmony_ci| 1500007 | error sending message to Common Event Service. | 51e41f4b71Sopenharmony_ci| 1500008 | Common Event Service does not complete initialization. | 52e41f4b71Sopenharmony_ci| 1500009 | error obtaining system parameters. | 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci**示例:** 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci```ts 57e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci// 发布公共事件回调 60e41f4b71Sopenharmony_cifunction publishCB(err: BusinessError) { 61e41f4b71Sopenharmony_ci if (err) { 62e41f4b71Sopenharmony_ci console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`); 63e41f4b71Sopenharmony_ci } else { 64e41f4b71Sopenharmony_ci console.info("publishAsUser"); 65e41f4b71Sopenharmony_ci } 66e41f4b71Sopenharmony_ci} 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci//指定发送的用户 69e41f4b71Sopenharmony_cilet userId = 100; 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci//发布公共事件 72e41f4b71Sopenharmony_citry { 73e41f4b71Sopenharmony_ci commonEventManager.publishAsUser("event", userId, publishCB); 74e41f4b71Sopenharmony_ci} catch (error) { 75e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 76e41f4b71Sopenharmony_ci console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`); 77e41f4b71Sopenharmony_ci} 78e41f4b71Sopenharmony_ci``` 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci## commonEventManager.publishAsUser 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_cipublishAsUser(event: string, userId: number, options: CommonEventPublishData, callback: AsyncCallback\<void>): void 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci以回调形式向指定用户发布公共事件并指定发布信息。 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci**系统API**:此接口为系统接口,三方应用不支持调用。 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci**参数:** 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 93e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ---------------------- | 94e41f4b71Sopenharmony_ci| event | string | 是 | 表示要发布的公共事件。详见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。 | 95e41f4b71Sopenharmony_ci| userId | number | 是 | 表示指定向该用户ID发送此公共事件。 | 96e41f4b71Sopenharmony_ci| options | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是 | 表示发布公共事件的属性。 | 97e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 表示被指定的回调方法。 | 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci**错误码:** 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 104e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 105e41f4b71Sopenharmony_ci| 202 | not system app. | 106e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 107e41f4b71Sopenharmony_ci| 1500004 | A third-party application cannot send system common events. | 108e41f4b71Sopenharmony_ci| 1500007 | error sending message to Common Event Service. | 109e41f4b71Sopenharmony_ci| 1500008 | Common Event Service does not complete initialization. | 110e41f4b71Sopenharmony_ci| 1500009 | error obtaining system parameters. | 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci**示例:** 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci```ts 115e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci// 公共事件相关信息 118e41f4b71Sopenharmony_cilet options:commonEventManager.CommonEventPublishData = { 119e41f4b71Sopenharmony_ci code: 0, // 公共事件的初始代码 120e41f4b71Sopenharmony_ci data: "initial data",// 公共事件的初始数据 121e41f4b71Sopenharmony_ci} 122e41f4b71Sopenharmony_ci// 发布公共事件回调 123e41f4b71Sopenharmony_cifunction publishCB(err: BusinessError) { 124e41f4b71Sopenharmony_ci if (err) { 125e41f4b71Sopenharmony_ci console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`); 126e41f4b71Sopenharmony_ci } else { 127e41f4b71Sopenharmony_ci console.info("publishAsUser"); 128e41f4b71Sopenharmony_ci } 129e41f4b71Sopenharmony_ci} 130e41f4b71Sopenharmony_ci// 指定发送的用户 131e41f4b71Sopenharmony_cilet userId = 100; 132e41f4b71Sopenharmony_ci// 发布公共事件 133e41f4b71Sopenharmony_citry { 134e41f4b71Sopenharmony_ci commonEventManager.publishAsUser("event", userId, options, publishCB); 135e41f4b71Sopenharmony_ci} catch (error) { 136e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 137e41f4b71Sopenharmony_ci console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`); 138e41f4b71Sopenharmony_ci} 139e41f4b71Sopenharmony_ci``` 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci## commonEventManager.removeStickyCommonEvent<sup>10+</sup> 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ciremoveStickyCommonEvent(event: string, callback: AsyncCallback\<void>): void 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci以回调形式移除粘性公共事件。 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci**需要权限**: ohos.permission.COMMONEVENT_STICKY 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci**系统API**:此接口为系统接口,三方应用不支持调用。 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci**参数:** 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 156e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | -------------------------------- | 157e41f4b71Sopenharmony_ci| event | string | 是 | 表示被移除的粘性公共事件。详见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。 | 158e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 表示移除粘性公共事件的回调方法。 | 159e41f4b71Sopenharmony_ci 160e41f4b71Sopenharmony_ci**错误码:** 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 165e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 166e41f4b71Sopenharmony_ci| 201 | The application dose not have permission to call the interface. | 167e41f4b71Sopenharmony_ci| 202 | not system app. | 168e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 169e41f4b71Sopenharmony_ci| 1500004 | A third-party application cannot send system common events. | 170e41f4b71Sopenharmony_ci| 1500007 | error sending message to Common Event Service. | 171e41f4b71Sopenharmony_ci| 1500008 | Common Event Service does not complete initialization. | 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci**示例:** 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci```ts 176e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_cicommonEventManager.removeStickyCommonEvent("sticky_event", (err: BusinessError) => { 179e41f4b71Sopenharmony_ci if (err) { 180e41f4b71Sopenharmony_ci console.error(`removeStickyCommonEvent failed, errCode: ${err.code}, errMes: ${err.message}`); 181e41f4b71Sopenharmony_ci return; 182e41f4b71Sopenharmony_ci } 183e41f4b71Sopenharmony_ci console.info(`removeStickyCommonEvent success`); 184e41f4b71Sopenharmony_ci}); 185e41f4b71Sopenharmony_ci``` 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci## commonEventManager.removeStickyCommonEvent<sup>10+</sup> 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciremoveStickyCommonEvent(event: string): Promise\<void> 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci以Promise形式移除粘性公共事件。 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci**需要权限**: ohos.permission.COMMONEVENT_STICKY 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci**系统API**:此接口为系统接口,三方应用不支持调用。 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci**参数:** 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 202e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 203e41f4b71Sopenharmony_ci| event | string | 是 | 表示被移除的粘性公共事件。详见[系统公共事件定义](./common_event/commonEventManager-definitions.md)。 | 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci**返回值:** 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci| 类型 | 说明 | 208e41f4b71Sopenharmony_ci| -------------- | ---------------------------- | 209e41f4b71Sopenharmony_ci| Promise\<void> | 表示移除粘性公共事件的对象。 | 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci**错误码:** 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 216e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 217e41f4b71Sopenharmony_ci| 201 | The application dose not have permission to call the interface. | 218e41f4b71Sopenharmony_ci| 202 | not system app. | 219e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 220e41f4b71Sopenharmony_ci| 1500004 | A third-party application cannot send system common events. | 221e41f4b71Sopenharmony_ci| 1500007 | error sending message to Common Event Service. | 222e41f4b71Sopenharmony_ci| 1500008 | Common Event Service does not complete initialization. | 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci**示例:** 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci```ts 227e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_cicommonEventManager.removeStickyCommonEvent("sticky_event").then(() => { 230e41f4b71Sopenharmony_ci console.info(`removeStickyCommonEvent success`); 231e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 232e41f4b71Sopenharmony_ci console.info(`removeStickyCommonEvent failed, errCode: ${err.code}, errMes: ${err.message}`); 233e41f4b71Sopenharmony_ci}); 234e41f4b71Sopenharmony_ci``` 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci## commonEventManager.setStaticSubscriberState<sup>10+</sup> 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_cisetStaticSubscriberState(enable: boolean, callback: AsyncCallback\<void>): void; 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci方法介绍:为当前应用设置静态订阅事件使能或去使能状态。使用callback异步回调。 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci**模型约束**:此接口仅可在Stage模型下使用。 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci**系统API**:此接口为系统接口,三方应用不支持调用。 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci**参数:** 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 251e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 252e41f4b71Sopenharmony_ci| enable | boolean | 是 | 表示静态订阅事件使能状态。 true:使能 false:去使能。 | 253e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是 | 表示设置静态订阅事件使能状态的回调方法。 | 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci**错误码:** 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 260e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 261e41f4b71Sopenharmony_ci| 202 | not system app. | 262e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 263e41f4b71Sopenharmony_ci| 1500007 | error sending message to Common Event Service. | 264e41f4b71Sopenharmony_ci| 1500008 | Common Event Service does not complete initialization. | 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci**示例:** 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci```ts 269e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_cicommonEventManager.setStaticSubscriberState(true, (err: BusinessError) => { 272e41f4b71Sopenharmony_ci if (!err) { 273e41f4b71Sopenharmony_ci console.info(`setStaticSubscriberState failed, err is null.`); 274e41f4b71Sopenharmony_ci return; 275e41f4b71Sopenharmony_ci } 276e41f4b71Sopenharmony_ci if (err.code !== undefined && err.code != null) { 277e41f4b71Sopenharmony_ci console.info(`setStaticSubscriberState failed, errCode: ${err.code}, errMes: ${err.message}`); 278e41f4b71Sopenharmony_ci return; 279e41f4b71Sopenharmony_ci } 280e41f4b71Sopenharmony_ci console.info(`setStaticSubscriberState success`); 281e41f4b71Sopenharmony_ci}); 282e41f4b71Sopenharmony_ci``` 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci## commonEventManager.setStaticSubscriberState<sup>10+</sup> 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_cisetStaticSubscriberState(enable: boolean): Promise\<void>; 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci方法介绍:为当前应用设置静态订阅事件使能或去使能状态。使用Promise异步回调。 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci**模型约束**:此接口仅可在Stage模型下使用。 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Notification.CommonEvent 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci**系统API**:此接口为系统接口,三方应用不支持调用。 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci**参数:** 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 299e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------------------- | 300e41f4b71Sopenharmony_ci| enable | boolean | 是 | 表示静态订阅事件使能状态。 true:使能 false:去使能。 | 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci**返回值:** 303e41f4b71Sopenharmony_ci 304e41f4b71Sopenharmony_ci| 类型 | 说明 | 305e41f4b71Sopenharmony_ci| -------------- | ---------------------------- | 306e41f4b71Sopenharmony_ci| Promise\<void> | Promise对象。无返回结果的Promise对象。| 307e41f4b71Sopenharmony_ci 308e41f4b71Sopenharmony_ci**错误码:** 309e41f4b71Sopenharmony_ci 310e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 313e41f4b71Sopenharmony_ci| -------- | ----------------------------------- | 314e41f4b71Sopenharmony_ci| 202 | not system app. | 315e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 316e41f4b71Sopenharmony_ci| 1500007 | error sending message to Common Event Service. | 317e41f4b71Sopenharmony_ci| 1500008 | Common Event Service does not complete initialization. | 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ci**示例:** 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci```ts 323e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_cicommonEventManager.setStaticSubscriberState(false).then(() => { 326e41f4b71Sopenharmony_ci console.info(`setStaticSubscriberState success`); 327e41f4b71Sopenharmony_ci}).catch ((err: BusinessError) => { 328e41f4b71Sopenharmony_ci console.info(`setStaticSubscriberState failed, errCode: ${err.code}, errMes: ${err.message}`); 329e41f4b71Sopenharmony_ci}); 330e41f4b71Sopenharmony_ci``` 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci## commonEventManager.setStaticSubscriberState<sup>12+</sup> 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_cisetStaticSubscriberState(enable: boolean, events?: Array\<string>): Promise\<void> 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci为当前应用设置静态订阅事件的使能状态,并且记录事件名称。使用Promise异步回调。 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci**模型约束**:此接口仅可在Stage模型下使用。 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Notification.CommonEvent 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci**系统接口**:此接口为系统接口。 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci**参数:** 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 347e41f4b71Sopenharmony_ci| ------ | ------------- | ---- | ---------------------------------------------------- | 348e41f4b71Sopenharmony_ci| enable | boolean | 是 | 表示静态订阅事件使能状态。 true:使能 false:去使能。| 349e41f4b71Sopenharmony_ci| events | array\<string> | 否 | 表示记录事件名称。 | 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci**返回值:** 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci| 类型 | 说明 | 354e41f4b71Sopenharmony_ci| -------------- | ------------------------------------ | 355e41f4b71Sopenharmony_ci| Promise\<void> | Promise对象。无返回结果的Promise对象。 | 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci**错误码:** 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[事件错误码](./errorcode-CommonEventService.md)。。 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci| 错误码ID | 错误信息 | 362e41f4b71Sopenharmony_ci| -------- | ------------------------------------------------------ | 363e41f4b71Sopenharmony_ci| 202 | not system app. | 364e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3.Parameter verification failed. | 365e41f4b71Sopenharmony_ci| 1500007 | error sending message to Common Event Service. | 366e41f4b71Sopenharmony_ci| 1500008 | Common Event Service does not complete initialization. | 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci**示例:** 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci```ts 371e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 372e41f4b71Sopenharmony_ciimport { promptAction } from '@kit.ArkUI'; 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_cilet evenName: string[] = ['usual.event.SEND_DATA']; 375e41f4b71Sopenharmony_cicommonEventManager.setStaticSubscriberState(true, evenName).then(() => { 376e41f4b71Sopenharmony_ci try { 377e41f4b71Sopenharmony_ci promptAction.showToast({ 378e41f4b71Sopenharmony_ci message: 'app.string.static_subscribe_enabled', 379e41f4b71Sopenharmony_ci duration: 2000, 380e41f4b71Sopenharmony_ci }); 381e41f4b71Sopenharmony_ci } catch (error) { 382e41f4b71Sopenharmony_ci console.error(`showToast error code is ${error.code}, message is ${error.message}`); 383e41f4b71Sopenharmony_ci } 384e41f4b71Sopenharmony_ci console.info(`setStaticSubscriberState success, state is ${true}`); 385e41f4b71Sopenharmony_ci}).catch((err: BusinessError) => { 386e41f4b71Sopenharmony_ci console.error(`setStaticSubscriberState failed, errCode: ${err.code}, errMes: ${err.message}`); 387e41f4b71Sopenharmony_ci}); 388e41f4b71Sopenharmony_ci``` 389