harmony 鸿蒙AbilityMonitor

  • 2025-06-12
  • 浏览 (3)

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 '@kit.TestKit';

Usage

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

Properties

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Name Type Read Only Optional Description
abilityName string No No Name of the ability bound to the ability monitor.
moduleName string No Yes Name of the module bound to the ability monitor.
onAbilityCreate (ability: UIAbility) => void No Yes Called when the ability is created.
If this property is not set, the corresponding lifecycle callback cannot be received.
onAbilityForeground (ability: UIAbility) => void No Yes Called when the ability starts to run in the foreground.
If this property is not set, the corresponding lifecycle callback cannot be received.
onAbilityBackground (ability: UIAbility) => void No Yes Called when the ability starts to run in the background.
If this property is not set, the corresponding lifecycle callback cannot be received.
onAbilityDestroy (ability: UIAbility) => void No Yes Called when the ability is destroyed.
If this property is not set, the corresponding lifecycle callback cannot be received.
onWindowStageCreate (ability: UIAbility) => void No Yes Called when the window stage is created.
If this property is not set, the corresponding lifecycle callback cannot be received.
onWindowStageRestore (ability: UIAbility) => void No Yes Called when the window stage is restored.
If this property is not set, the corresponding lifecycle callback cannot be received.
onWindowStageDestroy (ability: UIAbility) => void No Yes Called when the window stage is destroyed.
If this property is not set, the corresponding lifecycle callback cannot be received.

Example

import { abilityDelegatorRegistry } from '@kit.TestKit';
import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

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 鸿蒙Ability Kit

harmony 鸿蒙AbilityAccessControl

harmony 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_access_control.h

harmony 鸿蒙ability_base_common.h

0  赞