a23ff739创建于 2024年11月18日历史提交

Subscribing to Common Events in Dynamic Mode

When to Use

In 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 to subscribe to. For details, see System Common Events.

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<CommonEventData>): void Subscribes to common events.

How to Develop

  1. Import the commonEventManager module.

    import { BusinessError, commonEventManager } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    
    const TAG: string = 'ProcessModel';
    const DOMAIN_NUMBER: number = 0xFF00;
    
  2. 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. Replace the event field with the actual event name.
    let subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
        events: ['event'], // Subscribe to the common event screen-off.
    };
    
  3. 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: BusinessError, data: commonEventManager.CommonEventSubscriber) => {
      if (err) {
        hilog.error(DOMAIN_NUMBER, TAG, `Failed to create subscriber. Code is ${err.code}, message is ${err.message}`);
        return;
      }
      hilog.info(DOMAIN_NUMBER, TAG, 'Succeeded in creating subscriber.');
      subscriber = data;
    })
    
  4. 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: BusinessError, data: commonEventManager.CommonEventData) => {
        if (err) {
          hilog.error(DOMAIN_NUMBER, TAG, `Failed to subscribe common event. Code is ${err.code}, message is ${err.message}`);
          return;
        }
        // ...
      })
    } else {
      hilog.error(DOMAIN_NUMBER, TAG, `Need create subscriber`);
    }