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 '@ohos.app.ability.abilityDelegatorRegistry'; 13``` 14 15## Usage 16 17**AbilityMonitor** can be used as an input parameter of [addAbilityMonitor](js-apis-inner-application-abilityDelegator.md#addabilitymonitor9) in **abilityDelegator** to listen for lifecycle changes of an ability. 18 19## AbilityMonitor 20 21Describes an ability monitor. 22 23**System capability**: SystemCapability.Ability.AbilityRuntime.Core 24 25| Name | Type | Readable| Writable| Description | 26| ------------------------------------------------------------ | -------- | ---- | ---- | ------------------------------------------------------------ | 27| abilityName | string | Yes | Yes | Name of the ability bound to the ability monitor.| 28| moduleName? | string | Yes | Yes | Name of the module bound to the ability monitor.| 29| onAbilityCreate?:(data: [UIAbility](js-apis-app-ability-uiAbility.md)) | function | Yes | Yes | Called when the ability is created.<br>If this attribute is not set, the corresponding lifecycle callback cannot be received.| 30| onAbilityForeground?:(data: [UIAbility](js-apis-app-ability-uiAbility.md)) | function | Yes | Yes | Called when the ability starts to run in the foreground.<br>If this attribute is not set, the corresponding lifecycle callback cannot be received.| 31| onAbilityBackground?:(data: [UIAbility](js-apis-app-ability-uiAbility.md)) | function | Yes | Yes | Called when the ability starts to run in the background.<br>If this attribute is not set, the corresponding lifecycle callback cannot be received.| 32| onAbilityDestroy?:(data: [UIAbility](js-apis-app-ability-uiAbility.md)) | function | Yes | Yes | Called when the ability is destroyed.<br>If this attribute is not set, the corresponding lifecycle callback cannot be received.<br>| 33| onWindowStageCreate?:(data: [UIAbility](js-apis-app-ability-uiAbility.md)) | function | Yes | Yes | Called when the window stage is created.<br>If this attribute is not set, the corresponding lifecycle callback cannot be received.<br>| 34| onWindowStageRestore?:(data: [UIAbility](js-apis-app-ability-uiAbility.md)) | function | Yes | Yes | Called when the window stage is restored.<br>If this attribute is not set, the corresponding lifecycle callback cannot be received.<br>| 35| onWindowStageDestroy?:(data: [UIAbility](js-apis-app-ability-uiAbility.md)) | function | Yes | Yes | Called when the window stage is destroyed.<br>If this attribute is not set, the corresponding lifecycle callback cannot be received.<br>| 36 37**Example** 38 39```ts 40import AbilityDelegatorRegistry from '@ohos.app.ability.abilityDelegatorRegistry'; 41import UIAbility from '@ohos.app.ability.UIAbility'; 42import { BusinessError } from '@ohos.base'; 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