harmony 鸿蒙AbilityMonitor

  • 2022-12-22
  • 浏览 (436)

AbilityMonitor

The AbilityMonitor module provides monitors for abilities that meet specified conditions. The latest matched abilities are stored in an AbilityMonitor object.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';

Usage

AbilityMonitor can be used as an input parameter of addAbilityMonitor in abilityDelegator to listen for lifecycle changes of an ability.

AbilityMonitor

Describes an ability monitor.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Name Type Readable Writable Description
abilityName string Yes Yes Name of the ability bound to the ability monitor.
moduleName? string Yes Yes Name of the module bound to the ability monitor.
onAbilityCreate?:(data: UIAbility) function Yes Yes Called when the ability is created.
If this attribute is not set, the corresponding lifecycle callback cannot be received.
onAbilityForeground?:(data: UIAbility) function Yes Yes Called when the ability starts to run in the foreground.
If this attribute is not set, the corresponding lifecycle callback cannot be received.
onAbilityBackground?:(data: UIAbility) function Yes Yes Called when the ability starts to run in the background.
If this attribute is not set, the corresponding lifecycle callback cannot be received.
onAbilityDestroy?:(data: UIAbility) function Yes Yes Called when the ability is destroyed.
If this attribute is not set, the corresponding lifecycle callback cannot be received.
onWindowStageCreate?:(data: UIAbility) function Yes Yes Called when the window stage is created.
If this attribute is not set, the corresponding lifecycle callback cannot be received.
onWindowStageRestore?:(data: UIAbility) function Yes Yes Called when the window stage is restored.
If this attribute is not set, the corresponding lifecycle callback cannot be received.
onWindowStageDestroy?:(data: UIAbility) function Yes Yes Called when the window stage is destroyed.
If this attribute is not set, the corresponding lifecycle callback cannot be received.

Example

import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry';
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';

function onAbilityCreateCallback(data: UIAbility) {
    console.info(`onAbilityCreateCallback, data: ${JSON.stringify(data)}`);
}

let monitor: AbilityDelegatorRegistry.AbilityMonitor = {
    abilityName: 'abilityname',
    moduleName: "moduleName",
    onAbilityCreate: onAbilityCreateCallback
}

let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
abilityDelegator.addAbilityMonitor(monitor, (error : BusinessError) => {
    if (error) {
        console.error(`addAbilityMonitor fail, error: ${JSON.stringify(error)}`);
    }
});

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞