1# AbilityMonitor 2 3The **AbilityMonitor** module provides monitors for abilities that meet specified conditions. The latest matched abilities are stored in an **AbilityMonitor** 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 40function onAbilityCreateCallback(data) { 41 console.info('onAbilityCreateCallback'); 42} 43 44let monitor = { 45 abilityName: 'abilityname', 46 moduleName: "moduleName", 47 onAbilityCreate: onAbilityCreateCallback 48}; 49 50let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator(); 51abilityDelegator.addAbilityMonitor(monitor, (error : any) => { 52 if (error && error.code !== 0) { 53 console.error('addAbilityMonitor fail, error: ${JSON.stringify(error)}'); 54 } 55}); 56``` 57 58 59