js-apis-inner-application-applicationContext.md 8.3 KB
Newer Older
W
wusongqing 已提交
1 2
# ApplicationContext

3 4
The **ApplicationContext** module provides application-level context. You can use the APIs of this module to register and deregister the ability lifecycle listener in an application.

W
wusongqing 已提交
5 6 7 8
> **NOTE**
> 
> 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. 
> The APIs of this module can be used only in the stage model.
W
wusongqing 已提交
9

W
wusongqing 已提交
10
## Usage
W
wusongqing 已提交
11 12 13

Before calling any APIs in **ApplicationContext**, obtain an **ApplicationContext** instance through the **context** instance.

14
```ts
W
wusongqing 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
let applicationContext = this.context.getApplicationContext();
```

## ApplicationContext.registerAbilityLifecycleCallback

registerAbilityLifecycleCallback(callback: AbilityLifecycleCallback): **number**;

Registers a listener to monitor the ability lifecycle of the application.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name                  | Type    | Mandatory| Description                          |
| ------------------------ | -------- | ---- | ------------------------------ |
30
| callback | [AbilityLifecycleCallback](js-apis-app-ability-abilityLifecycleCallback.md) | Yes  | Callback used to return the ID of the registered listener.|
W
wusongqing 已提交
31 32 33 34 35 36 37

**Return value**

| Type  | Description                          |
| ------ | ------------------------------ |
| number | ID of the registered listener. The ID is incremented by 1 each time the listener is registered. When the ID exceeds 2^63-1, **-1** is returned.|

38 39
**Example**

40
```ts
41
import UIAbility from '@ohos.app.ability.UIAbility';
42 43 44

var lifecycleId;

45
export default class EntryAbility extends UIAbility {
46
    onCreate() {
47 48 49 50 51 52 53 54 55 56 57 58
        console.log("MyAbility onCreate")
        let AbilityLifecycleCallback = {
            onAbilityCreate(ability) {
                console.log("AbilityLifecycleCallback onAbilityCreate ability:" + JSON.stringify(ability));
            },
            onWindowStageCreate(ability, windowStage) {
                console.log("AbilityLifecycleCallback onWindowStageCreate ability:" + JSON.stringify(ability));
                console.log("AbilityLifecycleCallback onWindowStageCreate windowStage:" + JSON.stringify(windowStage));
            },
            onWindowStageActive(ability, windowStage) {
                console.log("AbilityLifecycleCallback onWindowStageActive ability:" + JSON.stringify(ability));
                console.log("AbilityLifecycleCallback onWindowStageActive windowStage:" + JSON.stringify(windowStage));
59
            },
60 61 62
            onWindowStageInactive(ability, windowStage) {
                console.log("AbilityLifecycleCallback onWindowStageInactive ability:" + JSON.stringify(ability));
                console.log("AbilityLifecycleCallback onWindowStageInactive windowStage:" + JSON.stringify(windowStage));
63
            },
64 65 66
            onWindowStageDestroy(ability, windowStage) {
                console.log("AbilityLifecycleCallback onWindowStageDestroy ability:" + JSON.stringify(ability));
                console.log("AbilityLifecycleCallback onWindowStageDestroy windowStage:" + JSON.stringify(windowStage));
67
            },
68 69
            onAbilityDestroy(ability) {
                console.log("AbilityLifecycleCallback onAbilityDestroy ability:" + JSON.stringify(ability));
70
            },
71 72
            onAbilityForeground(ability) {
                console.log("AbilityLifecycleCallback onAbilityForeground ability:" + JSON.stringify(ability));
73
            },
74 75
            onAbilityBackground(ability) {
                console.log("AbilityLifecycleCallback onAbilityBackground ability:" + JSON.stringify(ability));
76
            },
77
            onAbilityContinue(ability) {
78 79 80 81 82 83 84 85 86 87
                console.log("AbilityLifecycleCallback onAbilityContinue ability:" + JSON.stringify(ability));
            }
        }
        // 1. Obtain applicationContext through the context attribute.
        let applicationContext = this.context.getApplicationContext();
        // 2. Use applicationContext to register a listener for the ability lifecycle in the application.
        lifecycleId = applicationContext.registerAbilityLifecycleCallback(AbilityLifecycleCallback);
        console.log("registerAbilityLifecycleCallback number: " + JSON.stringify(lifecycleId));
    }
}
88
```
W
wusongqing 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102

## ApplicationContext.unregisterAbilityLifecycleCallback

unregisterAbilityLifecycleCallback(callbackId: **number**,  callback: AsyncCallback<**void**>): **void**;

Deregisters the listener that monitors the ability lifecycle of the application.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name       | Type    | Mandatory| Description                      |
| ------------- | -------- | ---- | -------------------------- |
| callbackId    | number   | Yes  | ID of the listener to deregister.|
W
wusongqing 已提交
103
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.                  |
W
wusongqing 已提交
104 105 106

**Example**

107
```ts
108
import UIAbility from '@ohos.app.ability.UIAbility';
109 110 111

var lifecycleId;

112
export default class EntryAbility extends UIAbility {
113 114 115 116 117 118 119 120 121
    onDestroy() {
        let applicationContext = this.context.getApplicationContext();
        console.log("stage applicationContext: " + JSON.stringify(applicationContext));
        applicationContext.unregisterAbilityLifecycleCallback(lifecycleId, (error, data) => {
            console.log("unregisterAbilityLifecycleCallback success, err: " + JSON.stringify(error));
        });
    }
}
```
122 123 124 125 126 127 128 129 130 131 132 133 134

## ApplicationContext.registerEnvironmentCallback

registerEnvironmentCallback(callback: EnvironmentCallback): **number**;

Registers a listener for system environment changes. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name                  | Type    | Mandatory| Description                          |
| ------------------------ | -------- | ---- | ------------------------------ |
135
| callback | [EnvironmentCallback](js-apis-app-ability-environmentCallback.md) | Yes  | Callback used to return the ID of the registered listener.|
136 137 138 139 140 141 142 143 144

**Return value**

| Type  | Description                          |
| ------ | ------------------------------ |
| number | ID of the registered listener. The ID is incremented by 1 each time the listener is registered. When the ID exceeds 2^63-1, **-1** is returned.|

**Example**

145
```ts
146
import UIAbility from '@ohos.app.ability.UIAbility';
147 148 149

var callbackId;

150
export default class EntryAbility extends UIAbility {
151
    onCreate() {
152
        console.log("MyAbility onCreate")
153 154 155 156 157
        globalThis.applicationContext = this.context.getApplicationContext();
        let EnvironmentCallback = {
            onConfigurationUpdated(config){
                console.log("onConfigurationUpdated config:" + JSON.stringify(config));
            },
158 159 160
            onMemoryLevel(level){
                console.log("onMemoryLevel level:" + level);
            }
161 162 163 164 165 166 167 168
        }
        // 1. Obtain an applicationContext object.
        let applicationContext = globalThis.applicationContext;
        // 2. Use applicationContext to register a listener for the ability lifecycle in the application.
        callbackId = applicationContext.registerEnvironmentCallback(EnvironmentCallback);
        console.log("registerEnvironmentCallback number: " + JSON.stringify(callbackId));
    }
}
169
```
170 171 172 173 174 175 176 177 178 179 180 181 182 183

## ApplicationContext.unregisterEnvironmentCallback

unregisterEnvironmentCallback(callbackId: **number**,  callback: AsyncCallback<**void**>): **void**;

Deregisters the listener for system environment changes. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name        | Type    | Mandatory| Description                      |
| ------------- | -------- | ---- | -------------------------- |
| callbackId    | number   | Yes  | ID of the listener to deregister.  |
W
wusongqing 已提交
184
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.                 |
185 186 187

**Example**

188
```ts
189
import UIAbility from '@ohos.app.ability.UIAbility';
190 191 192

var callbackId;

193
export default class EntryAbility extends UIAbility {
194 195 196 197 198 199 200 201
    onDestroy() {
        let applicationContext = this.context.getApplicationContext();
        applicationContext.unregisterEnvironmentCallback(callbackId, (error, data) => {
            console.log("unregisterEnvironmentCallback success, err: " + JSON.stringify(error));
        });
    }
}
```