• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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## Usage
10
11**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.
12
13## AbilityMonitor
14
15Describes an ability monitor.
16
17**System capability**: SystemCapability.Ability.AbilityRuntime.Core
18
19| Name                                                        | Type    | Readable| Writable| Description                                                        |
20| ------------------------------------------------------------ | -------- | ---- | ---- | ------------------------------------------------------------ |
21| abilityName                                                  | string   | Yes  | Yes  | Name of the ability bound to the ability monitor.|
22| moduleName?                                                  | string   | Yes  | Yes  | Name of the module bound to the ability monitor.|
23| 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.|
24| 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.|
25| 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.|
26| 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>|
27| 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>|
28| 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>|
29| 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>|
30
31**Example**
32
33```ts
34import AbilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry';
35
36function onAbilityCreateCallback(data) {
37    console.info('onAbilityCreateCallback');
38}
39
40let monitor = {
41    abilityName: 'abilityname',
42    moduleName: "moduleName",
43    onAbilityCreate: onAbilityCreateCallback
44};
45
46let abilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
47abilityDelegator.addAbilityMonitor(monitor, (err : any) => {
48    console.info('addAbilityMonitor callback');
49});
50```
51
52
53