• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Subscribing to Common Events in Dynamic Mode
2
3
4## When to Use
5
6In dynamic subscription mode, an application subscribes to a common event when it is running. If the subscribed event is published during the running period, the subscriber application will receive the event, 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-guidelines.md) to subscribe to. For details, see [Required Permissions](../reference/apis/js-apis-commonEventManager.md#support).
7
8
9## Available APIs
10
11For details about the APIs, see [API Reference](../reference/apis/js-apis-commonEvent.md#commoneventcreatesubscriber).
12
13| API| Description|
14| -------- | -------- |
15| createSubscriber(subscribeInfo: [CommonEventSubscribeInfo](../reference/apis/js-apis-commonEventManager.md#commoneventsubscribeinfo), callback: AsyncCallback<[CommonEventData](../reference/apis/js-apis-commonEventManager.md#commoneventdata)>): void | Creates a subscriber. This API uses an asynchronous callback to return the result.|
16| createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise<CommonEventSubscriber> | Creates a subscriber. This API uses a promise to return the result.|
17| subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback): void | Subscribes to common events.|
18
19
20## How to Develop
21
221. Import the **commonEvent** module.
23
24   ```ts
25   import commonEvent from '@ohos.commonEventManager';
26   ```
27
282. Create a **subscribeInfo** object. For details about the data types and parameters of the object, see [CommonEventSubscribeInfo](../reference/apis/js-apis-commonEventManager.md#commoneventsubscribeinfo).
29
30   ```ts
31   // Used to save the created subscriber object for subsequent subscription and unsubscription.
32   let subscriber = null;
33   // Subscriber information.
34   let subscribeInfo = {
35       events: ["usual.event.SCREEN_OFF"], // Subscribe to the common event screen-off.
36   }
37   ```
38
393. Create a subscriber object and save the returned object for subsequent operations such as subscription and unsubscription.
40
41   ```ts
42   // Callback for subscriber creation.
43   commonEvent.createSubscriber(subscribeInfo, (err, data) => {
44       if (err) {
45           console.error(`[CommonEvent] CreateSubscriberCallBack err=${JSON.stringify(err)}`);
46       } else {
47           console.info(`[CommonEvent] CreateSubscriber success`);
48           subscriber = data;
49           // Callback for common event subscription.
50       }
51   })
52   ```
53
544. 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/js-apis-commonEventManager.md#commoneventdata).
55
56   ```ts
57   // Callback for common event subscription.
58   if (subscriber !== null) {
59       commonEvent.subscribe(subscriber, (err, data) => {
60           if (err) {
61               console.error(`[CommonEvent] SubscribeCallBack err=${JSON.stringify(err)}`);
62           } else {
63               console.info(`[CommonEvent] SubscribeCallBack data=${JSON.stringify(data)}`);
64           }
65       })
66   } else {
67       console.error(`[CommonEvent] Need create subscriber`);
68   }
69   ```
70