• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.commonEventManager (公共事件模块)
2
3本模块提供了公共事件相关的能力,包括发布公共事件、订阅公共事件、以及退订公共事件。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import CommonEventManager from '@ohos.commonEventManager';
13```
14
15## Support
16
17系统公共事件是指由系统服务或系统应用发布的事件,订阅这些系统公共事件需要特定的权限。发布或订阅这些事件需要使用如下链接中的枚举定义。
18
19全部系统公共事件枚举定义请参见[系统公共事件定义](./commonEventManager-definitions.md)。
20
21## CommonEventManager.publish
22
23publish(event: string, callback: AsyncCallback\<void>): void
24
25发布公共事件,并在发布后执行相应的回调函数。
26
27**系统能力:** SystemCapability.Notification.CommonEvent
28
29**参数:**
30
31| 参数名     | 类型                 | 必填 | 说明                   |
32| -------- | -------------------- | ---- | ---------------------- |
33| event    | string               | 是   | 表示要发送的公共事件。 |
34| callback | AsyncCallback\<void> | 是   | 表示事件发布后将要执行的回调函数。 |
35
36**错误码:**
37
38错误码介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
39
40| 错误码ID | 错误信息                            |
41| -------- | ----------------------------------- |
42| 1500004  | not System services.                |
43| 1500007  | error sending message to Common Event Service. |
44| 1500008  | Common Event Service does not complete initialization. |
45| 1500009  | error obtaining system parameters.  |
46
47**示例:**
48
49```ts
50import Base from '@ohos.base';
51
52//发布公共事件回调
53function publishCB(err:Base.BusinessError) {
54    if (err) {
55        console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
56    } else {
57        console.info("publish");
58    }
59}
60
61//发布公共事件
62try {
63    CommonEventManager.publish("event", publishCB);
64} catch (error) {
65    let err:Base.BusinessError = error as Base.BusinessError;
66    console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
67}
68```
69
70## CommonEventManager.publish
71
72publish(event: string, options: CommonEventPublishData, callback: AsyncCallback\<void>): void
73
74以回调的形式发布公共事件。
75
76**系统能力:** SystemCapability.Notification.CommonEvent
77
78**参数:**
79
80| 参数名     | 类型                   | 必填 | 说明                   |
81| -------- | ---------------------- | ---- | ---------------------- |
82| event    | string                 | 是   | 表示要发布的公共事件。  |
83| options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是   | 表示发布公共事件的属性。 |
84| callback | syncCallback\<void>   | 是   | 表示被指定的回调方法。  |
85
86**错误码:**
87
88错误码介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
89
90| 错误码ID | 错误信息                            |
91| -------- | ----------------------------------- |
92| 1500004  | not System services.                |
93| 1500007  | error sending message to Common Event Service. |
94| 1500008  | Common Event Service does not complete initialization. |
95| 1500009  | error obtaining system parameters.  |
96
97**示例:**
98
99```ts
100import Base from '@ohos.base';
101
102//公共事件相关信息
103let options:CommonEventManager.CommonEventPublishData = {
104	code: 0,			 //公共事件的初始代码
105	data: "initial data",//公共事件的初始数据
106	isOrdered: true	 //有序公共事件
107}
108
109//发布公共事件回调
110function publishCB(err:Base.BusinessError) {
111	if (err) {
112        console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
113    } else {
114        console.info("publish");
115    }
116}
117
118//发布公共事件
119try {
120    CommonEventManager.publish("event", options, publishCB);
121} catch (error) {
122    let err:Base.BusinessError = error as Base.BusinessError;
123    console.error(`publish failed, code is ${err.code}, message is ${err.message}`);
124}
125```
126
127## CommonEventManager.publishAsUser<sup>
128
129publishAsUser(event: string, userId: number, callback: AsyncCallback\<void>): void
130
131以回调的形式向指定用户发布公共事件。
132
133**系统能力:** SystemCapability.Notification.CommonEvent
134
135**系统API**:此接口为系统接口,三方应用不支持调用。
136
137**参数:**
138
139| 参数名     | 类型                 | 必填 | 说明                               |
140| -------- | -------------------- | ---- | ---------------------------------- |
141| event    | string               | 是   | 表示要发送的公共事件。             |
142| userId   | number               | 是   | 表示指定向该用户ID发送此公共事件。 |
143| callback | AsyncCallback\<void> | 是   | 表示被指定的回调方法。             |
144
145**错误码:**
146
147错误码介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
148
149| 错误码ID | 错误信息                            |
150| -------- | ----------------------------------- |
151| 1500004  | not System services.                |
152| 1500007  | error sending message to Common Event Service. |
153| 1500008  | Common Event Service does not complete initialization. |
154| 1500009  | error obtaining system parameters.  |
155
156**示例:**
157
158```ts
159import Base from '@ohos.base';
160
161//发布公共事件回调
162function publishCB(err:Base.BusinessError) {
163	if (err) {
164        console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
165    } else {
166        console.info("publishAsUser");
167    }
168}
169
170//指定发送的用户
171let userId = 100;
172
173//发布公共事件
174try {
175    CommonEventManager.publishAsUser("event", userId, publishCB);
176} catch (error) {
177    let err:Base.BusinessError = error as Base.BusinessError;
178    console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
179}
180```
181
182## CommonEventManager.publishAsUser
183
184publishAsUser(event: string, userId: number, options: CommonEventPublishData, callback: AsyncCallback\<void>): void
185
186以回调形式向指定用户发布公共事件并指定发布信息。
187
188**系统能力:** SystemCapability.Notification.CommonEvent
189
190**系统API**:此接口为系统接口,三方应用不支持调用。
191
192**参数:**
193
194| 参数名     | 类型                   | 必填 | 说明                   |
195| -------- | ---------------------- | ---- | ---------------------- |
196| event    | string                 | 是   | 表示要发布的公共事件。  |
197| userId   | number | 是 | 表示指定向该用户ID发送此公共事件。 |
198| options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是   | 表示发布公共事件的属性。 |
199| callback | AsyncCallback\<void>   | 是   | 表示被指定的回调方法。  |
200
201**错误码:**
202
203错误码介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
204
205| 错误码ID | 错误信息                            |
206| -------- | ----------------------------------- |
207| 1500004  | not System services or System app.                |
208| 1500007  | error sending message to Common Event Service. |
209| 1500008  | Common Event Service does not complete initialization. |
210| 1500009  | error obtaining system parameters.  |
211
212**示例:**
213
214
215```ts
216import Base from '@ohos.base';
217
218//公共事件相关信息
219let options:CommonEventManager.CommonEventPublishData = {
220	code: 0,			 //公共事件的初始代码
221	data: "initial data",//公共事件的初始数据
222}
223
224//发布公共事件回调
225function publishCB(err:Base.BusinessError) {
226	if (err) {
227        console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
228    } else {
229        console.info("publishAsUser");
230    }
231}
232
233//指定发送的用户
234let userId = 100;
235
236//发布公共事件
237try {
238    CommonEventManager.publishAsUser("event", userId, options, publishCB);
239} catch (error) {
240    let err:Base.BusinessError = error as Base.BusinessError;
241    console.error(`publishAsUser failed, code is ${err.code}, message is ${err.message}`);
242}
243```
244
245## CommonEventManager.createSubscriber
246
247createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback\<CommonEventSubscriber>): void
248
249以回调形式创建订阅者。
250
251**系统能力:** SystemCapability.Notification.CommonEvent
252
253**参数:**
254
255| 参数名          | 类型                                                         | 必填 | 说明                       |
256| ------------- | ------------------------------------------------------------ | ---- | -------------------------- |
257| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md)        | 是   | 表示订阅信息。             |
258| callback      | AsyncCallback\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 是   | 表示创建订阅者的回调方法。 |
259
260**示例:**
261
262```ts
263import Base from '@ohos.base';
264
265let subscriber:CommonEventManager.CommonEventSubscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
266
267//订阅者信息
268let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
269    events: ["event"]
270};
271
272//创建订阅者回调
273function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
274    if(!err) {
275        console.info("createSubscriber");
276        subscriber = commonEventSubscriber;
277    } else {
278        console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
279    }
280}
281
282//创建订阅者
283try {
284    CommonEventManager.createSubscriber(subscribeInfo, createCB);
285} catch (error) {
286    let err:Base.BusinessError = error as Base.BusinessError;
287    console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
288}
289```
290
291## CommonEventManager.createSubscriber
292
293createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise\<CommonEventSubscriber>
294
295以Promise形式创建订阅者。
296
297**系统能力:** SystemCapability.Notification.CommonEvent
298
299**参数:**
300
301| 参数名          | 类型                                                  | 必填 | 说明           |
302| ------------- | ----------------------------------------------------- | ---- | -------------- |
303| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是   | 表示订阅信息。 |
304
305**返回值:**
306| 类型                                                      | 说明             |
307| --------------------------------------------------------- | ---------------- |
308| Promise\<[CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)> | 返回订阅者对象。 |
309
310**示例:**
311
312```ts
313import Base from '@ohos.base';
314
315let subscriber:CommonEventManager.CommonEventSubscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
316
317//订阅者信息
318let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
319	events: ["event"]
320};
321
322//创建订阅者
323CommonEventManager.createSubscriber(subscribeInfo).then((commonEventSubscriber:CommonEventManager.CommonEventSubscriber) => {
324    console.info("createSubscriber");
325    subscriber = commonEventSubscriber;
326}).catch((err:Base.BusinessError) => {
327    console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
328});
329
330```
331
332## CommonEventManager.createSubscriberSync<sup>10+</sup>
333
334createSubscriberSync(subscribeInfo: CommonEventSubscribeInfo): CommonEventSubscriber
335
336createSubscriber的同步接口。
337
338**系统能力:** SystemCapability.Notification.CommonEvent
339
340**参数:**
341
342| 参数名          | 类型                                                  | 必填 | 说明           |
343| ------------- | ----------------------------------------------------- | ---- | -------------- |
344| subscribeInfo | [CommonEventSubscribeInfo](./js-apis-inner-commonEvent-commonEventSubscribeInfo.md) | 是   | 表示订阅信息。 |
345
346**返回值:**
347| 类型                                                      | 说明             |
348| --------------------------------------------------------- | ---------------- |
349| [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 返回订阅者对象。 |
350
351**示例:**
352
353```ts
354import Base from '@ohos.base';
355
356let subscriber: CommonEventManager.CommonEventSubscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
357
358//订阅者信息
359let subscribeInfo: CommonEventManager.CommonEventSubscribeInfo = {
360  events: ["event"]
361};
362
363//创建订阅者
364try {
365    subscriber = CommonEventManager.createSubscriberSync(subscribeInfo);
366} catch (error) {
367    let err:Base.BusinessError = error as Base.BusinessError;
368    console.error(`createSubscriberSync failed, code is ${err.code}, message is ${err.message}`);
369}
370
371```
372
373## CommonEventManager.subscribe
374
375subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback\<CommonEventData>): void
376
377以回调形式订阅公共事件。
378
379**系统能力:** SystemCapability.Notification.CommonEvent
380
381**参数:**
382
383| 参数名       | 类型                                                | 必填 | 说明                             |
384| ---------- | ---------------------------------------------------- | ---- | -------------------------------- |
385| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md)     | 是   | 表示订阅者对象。                 |
386| callback   | AsyncCallback\<[CommonEventData](./js-apis-inner-commonEvent-commonEventData.md)> | 是   | 表示接收公共事件数据的回调函数。 |
387
388**错误码:**
389
390错误码介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
391
392| 错误码ID | 错误信息                            |
393| -------- | ----------------------------------- |
394| 801  | capability not supported.               |
395| 1500007  | error sending message to Common Event Service. |
396| 1500008  | Common Event Service does not complete initialization. |
397
398**示例:**
399
400```ts
401import Base from '@ohos.base';
402
403//订阅者信息
404let subscriber:CommonEventManager.CommonEventSubscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
405
406//订阅者信息
407let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
408    events: ["event"]
409};
410
411//订阅公共事件回调
412function SubscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
413    if (err) {
414        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
415    } else {
416        console.info("subscribe ");
417    }
418}
419
420//创建订阅者回调
421function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
422    if(!err) {
423        console.info("createSubscriber");
424        subscriber = commonEventSubscriber;
425        //订阅公共事件
426        try {
427            CommonEventManager.subscribe(subscriber, SubscribeCB);
428        } catch (error) {
429            let err:Base.BusinessError = error as Base.BusinessError;
430            console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
431        }
432    } else {
433        console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
434    }
435}
436
437//创建订阅者
438try {
439    CommonEventManager.createSubscriber(subscribeInfo, createCB);
440} catch (error) {
441    let err:Base.BusinessError = error as Base.BusinessError;
442    console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
443}
444```
445
446## CommonEventManager.unsubscribe
447
448unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void
449
450以回调形式取消订阅公共事件。
451
452**系统能力:** SystemCapability.Notification.CommonEvent
453
454**参数:**
455
456| 参数名       | 类型                                             | 必填 | 说明                     |
457| ---------- | ----------------------------------------------- | ---- | ------------------------ |
458| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是   | 表示订阅者对象。         |
459| callback   | AsyncCallback\<void>                            | 否   | 表示取消订阅的回调方法。 |
460
461**错误码:**
462
463错误码介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
464
465| 错误码ID | 错误信息                            |
466| -------- | ----------------------------------- |
467| 801  | capability not supported.               |
468| 1500007  | error sending message to Common Event Service. |
469| 1500008  | Common Event Service does not complete initialization. |
470
471**示例:**
472
473```ts
474import Base from '@ohos.base';
475
476let subscriber:CommonEventManager.CommonEventSubscriber; //用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
477//订阅者信息
478let subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
479    events: ["event"]
480};
481//订阅公共事件回调
482function subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
483    if (err) {
484        console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
485    } else {
486        console.info("subscribe");
487    }
488}
489//创建订阅者回调
490function createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
491    if (err) {
492        console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
493    } else {
494        console.info("createSubscriber");
495        subscriber = commonEventSubscriber;
496        //订阅公共事件
497        try {
498            CommonEventManager.subscribe(subscriber, subscribeCB);
499        } catch (error) {
500            let err:Base.BusinessError = error as Base.BusinessError;
501            console.error(`subscribe failed, code is ${err.code}, message is ${err.message}`);
502        }
503    }
504}
505//取消订阅公共事件回调
506function unsubscribeCB(err:Base.BusinessError) {
507    if (err) {
508        console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
509    } else {
510        console.info("unsubscribe");
511    }
512}
513//创建订阅者
514try {
515    CommonEventManager.createSubscriber(subscribeInfo, createCB);
516} catch (error) {
517    let err:Base.BusinessError = error as Base.BusinessError;
518    console.error(`createSubscriber failed, code is ${err.code}, message is ${err.message}`);
519}
520
521//取消订阅公共事件
522try {
523    CommonEventManager.unsubscribe(subscriber, unsubscribeCB);
524} catch (error) {
525    let err:Base.BusinessError = error as Base.BusinessError;
526    console.error(`unsubscribe failed, code is ${err.code}, message is ${err.message}`);
527}
528```
529
530## CommonEventManager.removeStickyCommonEvent<sup>10+</sup>
531
532removeStickyCommonEvent(event: string, callback: AsyncCallback\<void>): void
533
534以回调形式移除粘性公共事件。
535
536**系统能力:** SystemCapability.Notification.CommonEvent
537
538**需要权限**:  ohos.permission.COMMONEVENT_STICKY
539
540**系统API**:此接口为系统接口,三方应用不支持调用。
541
542**参数:**
543
544| 参数名   | 类型                 | 必填 | 说明                             |
545| -------- | -------------------- | ---- | -------------------------------- |
546| event    | string               | 是   | 表示被移除的粘性公共事件。       |
547| callback | AsyncCallback\<void> | 是   | 表示移除粘性公共事件的回调方法。 |
548
549**错误码:**
550
551错误码介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
552
553| 错误码ID | 错误信息                            |
554| -------- | ----------------------------------- |
555| 1500004  | not system service.                 |
556| 1500007  | error sending message to Common Event Service.             |
557| 1500008  | Common Event Service does not complete initialization.     |
558
559**示例:**
560
561
562```ts
563import Base from '@ohos.base';
564
565CommonEventManager.removeStickyCommonEvent("sticky_event", (err:Base.BusinessError) => {
566    if (err) {
567        console.info(`Remove sticky event AsyncCallback failed, errCode: ${err.code}, errMes: ${err.message}`);
568        return;
569    }
570    console.info(`Remove sticky event AsyncCallback success`);
571});
572```
573
574## CommonEventManager.removeStickyCommonEvent<sup>10+</sup>
575
576removeStickyCommonEvent(event: string): Promise\<void>
577
578以Promise形式移除粘性公共事件。
579
580**系统能力:** SystemCapability.Notification.CommonEvent
581
582**需要权限**:  ohos.permission.COMMONEVENT_STICKY
583
584**系统API**:此接口为系统接口,三方应用不支持调用。
585
586**参数:**
587
588| 参数名 | 类型   | 必填 | 说明                       |
589| ------ | ------ | ---- | -------------------------- |
590| event  | string | 是   | 表示被移除的粘性公共事件。 |
591
592**返回值:**
593
594| 类型           | 说明                         |
595| -------------- | ---------------------------- |
596| Promise\<void> | 表示移除粘性公共事件的对象。 |
597
598**错误码:**
599
600错误码介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
601
602| 错误码ID | 错误信息                            |
603| -------- | ----------------------------------- |
604| 1500004  | not system service.                 |
605| 1500007  | error sending message to Common Event Service.             |
606| 1500008  | Common Event Service does not complete initialization.     |
607
608**示例:**
609
610
611```ts
612import Base from '@ohos.base';
613
614CommonEventManager.removeStickyCommonEvent("sticky_event").then(() => {
615    console.info(`Remove sticky event AsyncCallback success`);
616}).catch ((err:Base.BusinessError) => {
617    console.info(`Remove sticky event AsyncCallback failed, errCode: ${err.code}, errMes: ${err.message}`);
618});
619```
620
621## CommonEventManager.setStaticSubscriberState<sup>10+</sup>
622
623setStaticSubscriberState(enable: boolean, callback: AsyncCallback\<void>): void;
624
625方法介绍:为当前应用设置静态订阅事件使能或去使能状态。使用callback异步回调。
626
627**模型约束**:此接口仅可在Stage模型下使用。
628
629**系统能力:** SystemCapability.Notification.CommonEvent
630
631**系统API**:此接口为系统接口,三方应用不支持调用。
632
633**参数:**
634
635| 参数名 | 类型   | 必填 | 说明                       |
636| ------ | ------ | ---- | -------------------------- |
637| enable  | boolean | 是   | 表示静态订阅事件使能状态。 true:使能 false:去使能。 |
638| callback  | AsyncCallback\<void> | 是   | 表示设置静态订阅事件使能状态的回调方法。 |
639
640**错误码:**
641
642错误码介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
643
644| 错误码ID | 错误信息                            |
645| -------- | ----------------------------------- |
646| 1500007  | error sending message to Common Event Service.             |
647| 1500008  | Common Event Service does not complete initialization.     |
648
649**示例:**
650
651
652```ts
653import Base from '@ohos.base';
654
655CommonEventManager.setStaticSubscriberState(true, (err:Base.BusinessError) => {
656    if (!err) {
657        console.info(`Set static subscriber state callback failed, err is null.`);
658        return;
659    }
660    if (err.code !== undefined && err.code != null) {
661        console.info(`Set static subscriber state callback failed, errCode: ${err.code}, errMes: ${err.message}`);
662        return;
663    }
664    console.info(`Set static subscriber state callback success`);
665});
666```
667
668## CommonEventManager.setStaticSubscriberState<sup>10+</sup>
669
670setStaticSubscriberState(enable: boolean): Promise\<void>;
671
672方法介绍:为当前应用设置静态订阅事件使能或去使能状态。使用Promise异步回调。
673
674**模型约束**:此接口仅可在Stage模型下使用。
675
676**系统能力:** SystemCapability.Notification.CommonEvent
677
678**系统API**:此接口为系统接口,三方应用不支持调用。
679
680**参数:**
681
682| 参数名 | 类型   | 必填 | 说明                       |
683| ------ | ------ | ---- | -------------------------- |
684| enable  | boolean | 是   | 表示静态订阅事件使能状态。 true:使能 false:去使能。 |
685
686**返回值:**
687
688| 类型           | 说明                         |
689| -------------- | ---------------------------- |
690| Promise\<void> | 表示设置静态订阅事件使能状态的对象。 |
691
692**错误码:**
693
694错误码介绍请参考[@ohos.commonEventManager(事件)](../errorcodes/errorcode-CommonEventService.md)
695
696| 错误码ID | 错误信息                            |
697| -------- | ----------------------------------- |
698| 1500007  | error sending message to Common Event Service.             |
699| 1500008  | Common Event Service does not complete initialization.     |
700
701**示例:**
702
703
704```ts
705import Base from '@ohos.base';
706
707CommonEventManager.setStaticSubscriberState(false).then(() => {
708    console.info(`Set static subscriber state promise success`);
709}).catch ((err:Base.BusinessError) => {
710    console.info(`Set static subscriber state promise failed, errCode: ${err.code}, errMes: ${err.message}`);
711});
712```
713