1e41f4b71Sopenharmony_ci# AbilityMonitor 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **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. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 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. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## Modules to Import 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```ts 12e41f4b71Sopenharmony_ciimport { abilityDelegatorRegistry } from '@kit.TestKit'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## Usage 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci**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. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## Properties 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci| Name | Type | Read Only| Optional| Description | 26e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | -------- | ---- | ---- | ------------------------------------------------------------ | 27e41f4b71Sopenharmony_ci| abilityName | string | No | No | Name of the ability bound to the ability monitor.| 28e41f4b71Sopenharmony_ci| moduleName | string | No | Yes | Name of the module bound to the ability monitor.| 29e41f4b71Sopenharmony_ci| 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.| 30e41f4b71Sopenharmony_ci| 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.| 31e41f4b71Sopenharmony_ci| 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.| 32e41f4b71Sopenharmony_ci| 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.| 33e41f4b71Sopenharmony_ci| 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.| 34e41f4b71Sopenharmony_ci| 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.| 35e41f4b71Sopenharmony_ci| 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.| 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**Example** 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci```ts 40e41f4b71Sopenharmony_ciimport { abilityDelegatorRegistry } from '@kit.TestKit'; 41e41f4b71Sopenharmony_ciimport { UIAbility } from '@kit.AbilityKit'; 42e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit'; 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_cifunction onAbilityCreateCallback(data: UIAbility) { 45e41f4b71Sopenharmony_ci console.info(`onAbilityCreateCallback, data: ${JSON.stringify(data)}`); 46e41f4b71Sopenharmony_ci} 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_cilet monitor: abilityDelegatorRegistry.AbilityMonitor = { 49e41f4b71Sopenharmony_ci abilityName: 'abilityname', 50e41f4b71Sopenharmony_ci moduleName: "moduleName", 51e41f4b71Sopenharmony_ci onAbilityCreate: onAbilityCreateCallback 52e41f4b71Sopenharmony_ci} 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_cilet abilityDelegator: abilityDelegatorRegistry.AbilityDelegator = abilityDelegatorRegistry.getAbilityDelegator(); 55e41f4b71Sopenharmony_ciabilityDelegator.addAbilityMonitor(monitor, (error: BusinessError) => { 56e41f4b71Sopenharmony_ci if (error) { 57e41f4b71Sopenharmony_ci console.error(`addAbilityMonitor fail, error: ${JSON.stringify(error)}`); 58e41f4b71Sopenharmony_ci } 59e41f4b71Sopenharmony_ci}); 60e41f4b71Sopenharmony_ci``` 61