1e41f4b71Sopenharmony_ci# @ohos.commonEvent (公共事件模块)(系统应用)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci本模块提供了公共事件的能力,包括公共事件的权限列表,发布公共事件,订阅或取消订阅公共事件,获取或修改公共事件结果代码、结果数据等。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci> 从API Version 9开始,该接口不再维护,推荐使用新接口[@ohos.commonEventManager](js-apis-commonEventManager.md)。
7e41f4b71Sopenharmony_ci>
8e41f4b71Sopenharmony_ci> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
9e41f4b71Sopenharmony_ci>
10e41f4b71Sopenharmony_ci> 当前界面仅包含本模块的系统接口,其他公开接口参见[CommonEvent](./js-apis-commonEvent.md)。
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci## 导入模块
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci```ts
15e41f4b71Sopenharmony_ciimport CommonEvent from '@ohos.commonEvent';
16e41f4b71Sopenharmony_ci```
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## Support
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci系统公共事件是指由系统服务或系统应用发布的事件,订阅这些系统公共事件需要特定的权限。发布或订阅这些事件需要使用如下链接中的枚举定义。
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci全部系统公共事件枚举定义请参见[系统公共事件定义](./common_event/commonEvent-definitions.md)。
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci## CommonEvent.publishAsUser<sup>(deprecated)</sup>
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_cipublishAsUser(event: string, userId: number, callback: AsyncCallback\<void>): void
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci以回调的形式向指定用户发布公共事件。
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci> **说明:**
31e41f4b71Sopenharmony_ci> 从 API version 8开始支持,从API version 9开始废弃。建议使用[commonEventManager.publishAsUser](js-apis-commonEventManager-sys.md#commoneventmanagerpublishasuser)替代。
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci**系统能力:** `SystemCapability.Notification.CommonEvent`
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci**系统API**:此接口为系统接口,三方应用不支持调用。
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**参数:**
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci| 参数名     | 类型                 | 必填 | 说明                               |
40e41f4b71Sopenharmony_ci| -------- | -------------------- | ---- | ---------------------------------- |
41e41f4b71Sopenharmony_ci| event    | string               | 是   | 表示要发送的公共事件。             |
42e41f4b71Sopenharmony_ci| userId   | number               | 是   | 表示指定向该用户ID发送此公共事件。 |
43e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void> | 是   | 表示被指定的回调方法。             |
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci**示例:**
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci```ts
48e41f4b71Sopenharmony_ciimport Base from '@ohos.base';
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci// 发布公共事件回调
51e41f4b71Sopenharmony_cifunction publishCB(err:Base.BusinessError) {
52e41f4b71Sopenharmony_ci	if (err.code) {
53e41f4b71Sopenharmony_ci        console.error(`publishAsUser failed, code is ${err.code}`);
54e41f4b71Sopenharmony_ci    } else {
55e41f4b71Sopenharmony_ci        console.info("publishAsUser");
56e41f4b71Sopenharmony_ci    }
57e41f4b71Sopenharmony_ci}
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci// 指定发送的用户
60e41f4b71Sopenharmony_cilet userId = 100;
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci// 发布公共事件
63e41f4b71Sopenharmony_ciCommonEvent.publishAsUser("event", userId, publishCB);
64e41f4b71Sopenharmony_ci```
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci## CommonEvent.publishAsUser<sup>(deprecated)</sup>
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_cipublishAsUser(event: string, userId: number, options: CommonEventPublishData, callback: AsyncCallback\<void>): void
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci以回调形式向指定用户发布公共事件并指定发布信息。
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci> **说明:**
73e41f4b71Sopenharmony_ci> 从 API version 8开始支持,从API version 9开始废弃。建议使用[commonEventManager.publishAsUser](js-apis-commonEventManager-sys.md#commoneventmanagerpublishasuser-1)替代。
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci**系统能力:** `SystemCapability.Notification.CommonEvent`
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci**系统API**:此接口为系统接口,三方应用不支持调用。
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci**参数:**
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci| 参数名     | 类型                   | 必填 | 说明                   |
82e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ---------------------- |
83e41f4b71Sopenharmony_ci| event    | string                 | 是   | 表示要发布的公共事件。  |
84e41f4b71Sopenharmony_ci| userId   | number | 是 | 表示指定向该用户ID发送此公共事件。 |
85e41f4b71Sopenharmony_ci| options  | [CommonEventPublishData](./js-apis-inner-commonEvent-commonEventPublishData.md) | 是   | 表示发布公共事件的属性。 |
86e41f4b71Sopenharmony_ci| callback | AsyncCallback\<void>   | 是   | 表示被指定的回调方法。  |
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**示例:**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci```ts
92e41f4b71Sopenharmony_ciimport Base from '@ohos.base';
93e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager';
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci// 公共事件相关信息
96e41f4b71Sopenharmony_cilet options:CommonEventManager.CommonEventPublishData = {
97e41f4b71Sopenharmony_ci	code: 0,			 // 公共事件的初始代码
98e41f4b71Sopenharmony_ci	data: "initial data",// 公共事件的初始数据
99e41f4b71Sopenharmony_ci}
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci// 发布公共事件回调
102e41f4b71Sopenharmony_cifunction publishCB(err:Base.BusinessError) {
103e41f4b71Sopenharmony_ci    if (err.code) {
104e41f4b71Sopenharmony_ci        console.error(`publishAsUser failed, code is ${err.code}`);
105e41f4b71Sopenharmony_ci    } else {
106e41f4b71Sopenharmony_ci        console.info("publishAsUser");
107e41f4b71Sopenharmony_ci    }
108e41f4b71Sopenharmony_ci}
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci// 指定发送的用户
111e41f4b71Sopenharmony_cilet userId = 100;
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci// 发布公共事件
114e41f4b71Sopenharmony_ciCommonEvent.publishAsUser("event", userId, options, publishCB);
115e41f4b71Sopenharmony_ci```
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ciunsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback\<void>): void
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci以回调形式取消订阅公共事件。
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci> **说明:** 
123e41f4b71Sopenharmony_ci>从 API version 7开始支持,从API version 9开始废弃。建议使用[commonEventManager.subscribe](js-apis-commonEventManager.md#commoneventmanagerunsubscribe)替代。
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci**系统能力:** `SystemCapability.Notification.CommonEvent`
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci**参数:**
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci| 参数名       | 类型                                             | 必填 | 说明                     |
130e41f4b71Sopenharmony_ci| ---------- | ----------------------------------------------- | ---- | ------------------------ |
131e41f4b71Sopenharmony_ci| subscriber | [CommonEventSubscriber](./js-apis-inner-commonEvent-commonEventSubscriber.md) | 是   | 表示订阅者对象。         |
132e41f4b71Sopenharmony_ci| callback   | AsyncCallback\<void>                            | 否   | 表示取消订阅的回调方法。 |
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci**示例:**
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci```ts
137e41f4b71Sopenharmony_ciimport Base from '@ohos.base';
138e41f4b71Sopenharmony_ciimport CommonEventManager from '@ohos.commonEventManager';
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_cilet subscriber:CommonEventManager.CommonEventSubscriber;	// 用于保存创建成功的订阅者对象,后续使用其完成订阅及退订的动作
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci// 订阅者信息
143e41f4b71Sopenharmony_cilet subscribeInfo:CommonEventManager.CommonEventSubscribeInfo = {
144e41f4b71Sopenharmony_ci    events: ["event"]
145e41f4b71Sopenharmony_ci};
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci// 订阅公共事件回调
148e41f4b71Sopenharmony_cifunction subscribeCB(err:Base.BusinessError, data:CommonEventManager.CommonEventData) {
149e41f4b71Sopenharmony_ci    if (err.code) {
150e41f4b71Sopenharmony_ci        console.error(`subscribe failed, code is ${err.code}`);
151e41f4b71Sopenharmony_ci    } else {
152e41f4b71Sopenharmony_ci        console.info("subscribe " + JSON.stringify(data));
153e41f4b71Sopenharmony_ci    }
154e41f4b71Sopenharmony_ci}
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci// 创建订阅者回调
157e41f4b71Sopenharmony_cifunction createCB(err:Base.BusinessError, commonEventSubscriber:CommonEventManager.CommonEventSubscriber) {
158e41f4b71Sopenharmony_ci    if (err.code) {
159e41f4b71Sopenharmony_ci        console.error(`createSubscriber failed, code is ${err.code}`);
160e41f4b71Sopenharmony_ci    } else {
161e41f4b71Sopenharmony_ci        console.info("createSubscriber");
162e41f4b71Sopenharmony_ci        subscriber = commonEventSubscriber;
163e41f4b71Sopenharmony_ci        // Subscribe to a common event.
164e41f4b71Sopenharmony_ci        CommonEvent.subscribe(subscriber, subscribeCB);
165e41f4b71Sopenharmony_ci    }
166e41f4b71Sopenharmony_ci}
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci// 取消订阅公共事件回调
169e41f4b71Sopenharmony_cifunction unsubscribeCB(err:Base.BusinessError) {
170e41f4b71Sopenharmony_ci    if (err.code) {
171e41f4b71Sopenharmony_ci        console.error(`unsubscribe failed, code is ${err.code}`);
172e41f4b71Sopenharmony_ci    } else {
173e41f4b71Sopenharmony_ci        console.info("unsubscribe");
174e41f4b71Sopenharmony_ci    }
175e41f4b71Sopenharmony_ci}
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci// 创建订阅者
178e41f4b71Sopenharmony_ciCommonEvent.createSubscriber(subscribeInfo, createCB);
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci// 取消订阅公共事件
181e41f4b71Sopenharmony_ciCommonEvent.unsubscribe(subscriber, unsubscribeCB);
182e41f4b71Sopenharmony_ci```