harmony 鸿蒙Subscribing to Common Events in Dynamic Mode

  • 2023-02-03
  • 浏览 (680)

Subscribing to Common Events in Dynamic Mode

When to Use

In 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 to subscribe to. For details, see Required Permissions.

Available APIs

For details about the APIs, see API Reference.

API Description
createSubscriber(subscribeInfo: CommonEventSubscribeInfo, callback: AsyncCallback<CommonEventSubscriber>): void Creates a subscriber. This API uses an asynchronous callback to return the result.
createSubscriber(subscribeInfo: CommonEventSubscribeInfo): Promise<CommonEventSubscriber> Creates a subscriber. This API uses a promise to return the result.
subscribe(subscriber: CommonEventSubscriber, callback: AsyncCallback): void Subscribes to common events.

How to Develop

  1. Import the commonEventManager module.
   import commonEventManager from '@ohos.commonEventManager';
   import Base from '@ohos.base';
  1. Create a subscribeInfo object. For details about the data types and parameters of the object, see CommonEventSubscribeInfo.
   // Used to save the created subscriber object for subsequent subscription and unsubscription.
   let subscriber: commonEventManager.CommonEventSubscriber|null = null;
   // Subscriber information.
   let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
     events: ["usual.event.SCREEN_OFF"], // Subscribe to the common event screen-off.
   }
  1. Create a subscriber object and save the returned object for subsequent operations such as subscription and unsubscription.
   // Callback for subscriber creation.
   commonEventManager.createSubscriber(subscribeInfo, (err: Base.BusinessError, data: commonEventManager.CommonEventSubscriber) => {
     if (err) {
       console.error(`Failed to create subscriber. Code is ${err.code}, message is ${err.message}`);
       return;
     }
     console.info('Succeeded in creating subscriber.');
     subscriber = data;
     // Callback for common event subscription.
   })
  1. 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.
   // Callback for common event subscription.
   if (subscriber !== null) {
     commonEventManager.subscribe(subscriber, (err: Base.BusinessError, data: commonEventManager.CommonEventData) => {
       if (err) {
         console.error(`Failed to subscribe common event. Code is ${err.code}, message is ${err.message}`);
         return;
       }
     })
   } else {
     console.error(`Need create subscriber`);
   }

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Application Models

harmony 鸿蒙Using Explicit Want to Start an Application Component

harmony 鸿蒙Using Implicit Want to Open a Website

harmony 鸿蒙AbilityStage Component Container

harmony 鸿蒙Accessing a DataAbility

harmony 鸿蒙Accessing a DataShareExtensionAbility from the FA Model

harmony 鸿蒙AccessibilityExtensionAbility

harmony 鸿蒙Common action and entities Values

harmony 鸿蒙API Switching Overview

harmony 鸿蒙Switching of app and deviceConfig

0  赞