harmony 鸿蒙Unsubscribing from Common Events in Dynamic Mode

2023-02-03 浏览 (782)

Unsubscribing from Common Events in Dynamic Mode

When to Use

You can call unsubscribe() to unsubscribe from a common event that is no longer required in dynamic mode.

Available APIs

APIDescription
unsubscribe(subscriber: CommonEventSubscriber, callback?: AsyncCallback)Unsubscribes from a common event.

How to Develop

  1. Import the commonEventManager module.

    import commonEventManager from '@ohos.commonEventManager';
    import Base from '@ohos.base';
    
  2. Subscribe to an event by following the procedure described in Subscribing to Common Events in Dynamic Mode.

  3. Call unsubscribe in CommonEvent to unsubscribe from the common event.

    // The subscriber object is created during event subscription.
    if (subscriber !== null) {
        commonEventManager.unsubscribe(subscriber, (err: Base.BusinessError) => {
            if (err) {
                console.error(`[CommonEvent] UnsubscribeCallBack err=${JSON.stringify(err)}`);
            } else {
                console.info(`[CommonEvent] Unsubscribe`);
                subscriber = null;
            }
        })
    }
    

你可能感兴趣的鸿蒙文章

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

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/377e855333f44452bebd70d54c090fa0