1e41f4b71Sopenharmony_ci# Subscribing to Common Events in Dynamic Mode
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## When to Use
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciIn dynamic subscription mode, an application, when it is running, subscribes to a common event and then receives the event once it is published, together with the parameters passed in the event. For example, if an application expects to be notified of low battery so that it can reduce power consumption accordingly when running, then the application can subscribe to the low-battery event. Upon receiving the event, the application can close some unnecessary tasks to reduce power consumption. Certain system common events [require specific permissions](../../security/AccessToken/determine-application-mode.md) to subscribe to. For details, see [Required Permissions](../../reference/apis-basic-services-kit/js-apis-commonEventManager.md#support).
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Available APIs
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ciFor details about the APIs, see [API Reference](../../reference/apis-basic-services-kit/js-apis-commonEventManager.md#commoneventmanagersubscribe).
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci| API| Description|
14e41f4b71Sopenharmony_ci| -------- | -------- |
15e41f4b71Sopenharmony_ci| createSubscriber(subscribeInfo: [CommonEventSubscribeInfo](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventSubscribeInfo.md), callback: AsyncCallback<[CommonEventSubscriber](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventSubscriber.md#how-to-use)>): void| Creates a subscriber. This API uses an asynchronous callback to return the result.|
16e41f4b71Sopenharmony_ci| createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise<CommonEventSubscriber> | Creates a subscriber. This API uses a promise to return the result.|
17e41f4b71Sopenharmony_ci| subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback): void | Subscribes to common events.|
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci## How to Develop
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci1. Import the **commonEventManager** module.
23e41f4b71Sopenharmony_ci   
24e41f4b71Sopenharmony_ci   ```ts
25e41f4b71Sopenharmony_ci   import Base from '@ohos.base';
26e41f4b71Sopenharmony_ci   import commonEventManager from '@ohos.commonEventManager';
27e41f4b71Sopenharmony_ci   import promptAction from '@ohos.promptAction';
28e41f4b71Sopenharmony_ci   import hilog from '@ohos.hilog';
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci   const TAG: string = 'ProcessModel';
31e41f4b71Sopenharmony_ci   const DOMAIN_NUMBER: number = 0xFF00;
32e41f4b71Sopenharmony_ci   ```
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci2. Create a **subscribeInfo** object. For details about the data types and parameters of the object, see [CommonEventSubscribeInfo](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventSubscribeInfo.md).
35e41f4b71Sopenharmony_ci   
36e41f4b71Sopenharmony_ci   ```ts
37e41f4b71Sopenharmony_ci   // Used to save the created subscriber object for subsequent subscription and unsubscription.
38e41f4b71Sopenharmony_ci   let subscriber: commonEventManager.CommonEventSubscriber | null = null;
39e41f4b71Sopenharmony_ci   //Subscriber information. Replace the event field with the actual event name.
40e41f4b71Sopenharmony_ci   let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
41e41f4b71Sopenharmony_ci       events: ['event'], // Subscribe to the common event screen-off.
42e41f4b71Sopenharmony_ci   };
43e41f4b71Sopenharmony_ci   ```
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci3. Create a subscriber object and save the returned object for subsequent operations such as subscription and unsubscription.
46e41f4b71Sopenharmony_ci   
47e41f4b71Sopenharmony_ci   ```ts
48e41f4b71Sopenharmony_ci   // Callback for subscriber creation.
49e41f4b71Sopenharmony_ci   commonEventManager.createSubscriber(subscribeInfo, (err: Base.BusinessError, data: commonEventManager.CommonEventSubscriber) => {
50e41f4b71Sopenharmony_ci     if (err) {
51e41f4b71Sopenharmony_ci       hilog.error(DOMAIN_NUMBER, TAG, `Failed to create subscriber. Code is ${err.code}, message is ${err.message}`);
52e41f4b71Sopenharmony_ci       return;
53e41f4b71Sopenharmony_ci     }
54e41f4b71Sopenharmony_ci     hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in creating subscriber.');
55e41f4b71Sopenharmony_ci     subscriber = data;
56e41f4b71Sopenharmony_ci   })
57e41f4b71Sopenharmony_ci   ```
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci4. Create a subscription callback, which is triggered when an event is received. The data returned in the subscription callback contains information such as the common event name and data carried by the publisher. For details about the data types and parameters of the common event data, see [CommonEventData](../../reference/apis-basic-services-kit/js-apis-inner-commonEvent-commonEventData.md).
60e41f4b71Sopenharmony_ci   
61e41f4b71Sopenharmony_ci   ```ts
62e41f4b71Sopenharmony_ci   // Callback for common event subscription.
63e41f4b71Sopenharmony_ci   if (subscriber !== null) {
64e41f4b71Sopenharmony_ci     commonEventManager.subscribe(subscriber, (err: Base.BusinessError, data: commonEventManager.CommonEventData) => {
65e41f4b71Sopenharmony_ci       if (err) {
66e41f4b71Sopenharmony_ci         hilog.error(DOMAIN_NUMBER, TAG, `Failed to subscribe common event. Code is ${err.code}, message is ${err.message}`);
67e41f4b71Sopenharmony_ci         return;
68e41f4b71Sopenharmony_ci       }
69e41f4b71Sopenharmony_ci       // ...
70e41f4b71Sopenharmony_ci     })
71e41f4b71Sopenharmony_ci   } else {
72e41f4b71Sopenharmony_ci     hilog.error(DOMAIN_NUMBER, TAG, `Need create subscriber`);
73e41f4b71Sopenharmony_ci   }
74e41f4b71Sopenharmony_ci   ```
75