1# AbilityMonitor 2 3The **AbilityMonitor** module provides monitors for abilities that meet specified conditions. The latest matched abilities are stored in an [AbilityMonitor](js-apis-inner-application-abilityMonitor.md#abilitymonitor-1) object. 4 5> **NOTE** 6> 7> 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. 8 9## Modules to Import 10 11```ts 12import { abilityDelegatorRegistry } from '@kit.TestKit'; 13``` 14 15## Usage 16 17**AbilityMonitor** can be used as an input parameter of [addAbilityMonitor](../apis-test-kit/js-apis-inner-application-abilityDelegator.md#addabilitymonitor9) in **abilityDelegator** to listen for lifecycle changes of an ability. 18 19## Properties 20 21**Atomic service API**: This API can be used in atomic services since API version 11. 22 23**System capability**: SystemCapability.Ability.AbilityRuntime.Core 24 25| Name | Type | Read Only| Optional| Description | 26| ------------------------------------------------------------ | -------- | ---- | ---- | ------------------------------------------------------------ | 27| abilityName | string | No | No | Name of the ability bound to the ability monitor.| 28| moduleName | string | No | Yes | Name of the module bound to the ability monitor.| 29| onAbilityCreate | (ability: [UIAbility](js-apis-app-ability-uiAbility.md)) => void | No | Yes | Called when the ability is created.<br>If this property is not set, the corresponding lifecycle callback cannot be received.| 30| onAbilityForeground | (ability: [UIAbility](js-apis-app-ability-uiAbility.md)) => void | No | Yes | Called when the ability starts to run in the foreground.<br>If this property is not set, the corresponding lifecycle callback cannot be received.| 31| onAbilityBackground | (ability: [UIAbility](js-apis-app-ability-uiAbility.md)) => void | No | Yes | Called when the ability starts to run in the background.<br>If this property is not set, the corresponding lifecycle callback cannot be received.| 32| onAbilityDestroy | (ability: [UIAbility](js-apis-app-ability-uiAbility.md)) => void | No | Yes | Called when the ability is destroyed.<br>If this property is not set, the corresponding lifecycle callback cannot be received.| 33| onWindowStageCreate | (ability: [UIAbility](js-apis-app-ability-uiAbility.md)) => void | No | Yes | Called when the window stage is created.<br>If this property is not set, the corresponding lifecycle callback cannot be received.| 34| onWindowStageRestore | (ability: [UIAbility](js-apis-app-ability-uiAbility.md)) => void | No | Yes | Called when the window stage is restored.<br>If this property is not set, the corresponding lifecycle callback cannot be received.| 35| onWindowStageDestroy | (ability: [UIAbility](js-apis-app-ability-uiAbility.md)) => void | No | Yes | Called when the window stage is destroyed.<br>If this property is not set, the corresponding lifecycle callback cannot be received.| 36 37**Example** 38 39```ts 40import { abilityDelegatorRegistry } from '@kit.TestKit'; 41import { UIAbility } from '@kit.AbilityKit'; 42import { BusinessError } from '@kit.BasicServicesKit'; 43 44function onAbilityCreateCallback(data: UIAbility) { 45 console.info(`onAbilityCreateCallback, data: ${JSON.stringify(data)}`); 46} 47 48let monitor: abilityDelegatorRegistry.AbilityMonitor = { 49 abilityName: 'abilityname', 50 moduleName: "moduleName", 51 onAbilityCreate: onAbilityCreateCallback 52} 53 54let abilityDelegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); 55abilityDelegator.addAbilityMonitor(monitor, (error: BusinessError) => { 56 if (error) { 57 console.error(`addAbilityMonitor fail, error: ${JSON.stringify(error)}`); 58 } 59}); 60``` 61