js-apis-application-applicationContext.md 8.1 KB
Newer Older
1 2
# ApplicationContext

Y
yuyaozhi 已提交
3 4
ApplicationContext模块提供开发者应用级别的的上下文的能力,包括提供注册及取消注册应用内组件生命周期的监听接口。

Y
yuyaozhi 已提交
5 6
> **说明:**
> 
7 8
> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。  
> 本模块接口仅可在Stage模型下使用。
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

## 使用说明

在使用ApplicationContext的功能前,需要通过context的实例获取。

```js
let applicationContext = this.context.getApplicationContext();
```


## ApplicationContext.registerAbilityLifecycleCallback

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

注册监听应用内生命周期

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名                   | 类型     | 必填 | 说明                           |
| ------------------------ | -------- | ---- | ------------------------------ |
31
| callback | [AbilityLifecycleCallback](js-apis-application-abilityLifecycleCallback.md) | 是   | 回调方法,返回注册监听事件的ID。 |
32 33 34 35 36

**返回值:**

| 类型   | 说明                           |
| ------ | ------------------------------ |
D
dy_study 已提交
37
| number | 返回的此次注册监听生命周期的ID(每次注册该ID会自增+1,当超过监听上限数量2^63-1时,返回-1)。|
38

Y
yuyaozhi 已提交
39 40 41
**示例:**

  ```js
D
dy_study 已提交
42
import Ability from "@ohos.application.Ability";
Y
yuyaozhi 已提交
43

D
dy_study 已提交
44
var lifecycleId;
Y
yuyaozhi 已提交
45

D
dy_study 已提交
46
export default class MyAbility extends Ability {
Y
yuyaozhi 已提交
47
    onCreate() {
D
dy_study 已提交
48
        console.log("MyAbility onCreate")
Y
yuyaozhi 已提交
49 50 51 52
        let AbilityLifecycleCallback  =  {
            onAbilityCreate(ability){
                console.log("AbilityLifecycleCallback onAbilityCreate ability:" + JSON.stringify(ability));        
            },
D
dy_study 已提交
53 54 55
            onWindowStageCreate(ability, windowStage){
                console.log("AbilityLifecycleCallback onWindowStageCreate ability:" + JSON.stringify(ability)); 
                console.log("AbilityLifecycleCallback onWindowStageCreate windowStage:" + JSON.stringify(windowStage));           
Y
yuyaozhi 已提交
56
            },
D
dy_study 已提交
57 58 59 60 61 62 63 64 65 66 67
            onWindowStageActive(ability, windowStage){
                console.log("AbilityLifecycleCallback onWindowStageActive ability:" + JSON.stringify(ability)); 
                console.log("AbilityLifecycleCallback onWindowStageActive windowStage:" + JSON.stringify(windowStage));           
            },
            onWindowStageInactive(ability, windowStage){
                console.log("AbilityLifecycleCallback onWindowStageInactive ability:" + JSON.stringify(ability));
                console.log("AbilityLifecycleCallback onWindowStageInactive windowStage:" + JSON.stringify(windowStage));  
            },
            onWindowStageDestroy(ability, windowStage){
                console.log("AbilityLifecycleCallback onWindowStageDestroy ability:" + JSON.stringify(ability));
                console.log("AbilityLifecycleCallback onWindowStageDestroy windowStage:" + JSON.stringify(windowStage));  
Y
yuyaozhi 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
            },
            onAbilityDestroy(ability){
                console.log("AbilityLifecycleCallback onAbilityDestroy ability:" + JSON.stringify(ability));             
            },
            onAbilityForeground(ability){
                console.log("AbilityLifecycleCallback onAbilityForeground ability:" + JSON.stringify(ability));             
            },
            onAbilityBackground(ability){
                console.log("AbilityLifecycleCallback onAbilityBackground ability:" + JSON.stringify(ability));              
            },
            onAbilityContinue(ability){
                console.log("AbilityLifecycleCallback onAbilityContinue ability:" + JSON.stringify(ability));
            }
        }
        // 1.通过context属性获取applicationContext
        let applicationContext = this.context.getApplicationContext();
        // 2.通过applicationContext注册监听应用内生命周期
D
dy_study 已提交
85 86
        lifecycleId = applicationContext.registerAbilityLifecycleCallback(AbilityLifecycleCallback);
        console.log("registerAbilityLifecycleCallback number: " + JSON.stringify(lifecycleId));
D
dy_study 已提交
87
    },
Y
yuyaozhi 已提交
88 89
    onDestroy() {
        let applicationContext = this.context.getApplicationContext();
D
dy_study 已提交
90
        applicationContext.unregisterAbilityLifecycleCallback(lifecycleId, (error, data) => {
91
            console.log("unregisterAbilityLifecycleCallback success, err: " + JSON.stringify(error));
Y
yuyaozhi 已提交
92 93 94 95
        });
    }
}
  ```
96

D
dy_study 已提交
97

98 99 100 101 102 103 104 105 106 107 108 109
## ApplicationContext.unregisterAbilityLifecycleCallback

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

取消监听应用内生命周期

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名        | 类型     | 必填 | 说明                       |
| ------------- | -------- | ---- | -------------------------- |
D
dy_study 已提交
110
| callbackId    | number   | 是   | 注册监听应用内生命周期的ID。 |
111
| callback | AsyncCallback\<void> | 是   | 回调方法。                   |
112 113 114 115

**示例:**

  ```js
Y
yuyaozhi 已提交
116
  let applicationContext = this.context.getApplicationContext();
D
dy_study 已提交
117
  let lifecycleId = 1;
Y
yuyaozhi 已提交
118
  console.log("stage applicationContext: " + JSON.stringify(applicationContext));
D
dy_study 已提交
119
  applicationContext.unregisterAbilityLifecycleCallback(lifecycleId, (error, data) => {
Y
yuyaozhi 已提交
120 121
      console.log("unregisterAbilityLifecycleCallback success, err: " + JSON.stringify(error));
  });
122
  ```
D
dy_study 已提交
123 124 125 126 127

## ApplicationContext.registerEnvironmentCallback

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

D
dy_study 已提交
128
注册对系统环境变化的监听。使用callback异步回调。
D
dy_study 已提交
129 130 131 132 133 134 135

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

| 参数名                   | 类型     | 必填 | 说明                           |
| ------------------------ | -------- | ---- | ------------------------------ |
136
| callback | [EnvironmentCallback](js-apis-application-EnvironmentCallback.md) | 是   | 回调方法,返回注册监听事件的ID。 |
D
dy_study 已提交
137 138 139 140 141

**返回值:**

| 类型   | 说明                           |
| ------ | ------------------------------ |
D
dy_study 已提交
142
| number | 返回的此次注册监听系统环境变化的ID(每次注册该ID会自增+1,当超过监听上限数量2^63-1时,返回-1)。|
D
dy_study 已提交
143 144 145 146

**示例:**

  ```js
D
dy_study 已提交
147
import Ability from "@ohos.application.Ability";
D
dy_study 已提交
148 149 150

var callbackId;

D
dy_study 已提交
151
export default class MyAbility extends Ability {
D
dy_study 已提交
152
    onCreate() {
D
dy_study 已提交
153
        console.log("MyAbility onCreate")
D
dy_study 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
        globalThis.applicationContext = this.context.getApplicationContext();
        let EnvironmentCallback = {
            onConfigurationUpdated(config){
                console.log("onConfigurationUpdated config:" + JSON.stringify(config));
            },
        }
        // 1.获取applicationContext
        let applicationContext = globalThis.applicationContext;
        // 2.通过applicationContext注册监听应用内生命周期
        callbackId = applicationContext.registerEnvironmentCallback(EnvironmentCallback);
        console.log("registerEnvironmentCallback number: " + JSON.stringify(callbackId));
    }
    onDestroy() {
        let applicationContext = globalThis.applicationContext;
        applicationContext.unregisterEnvironmentCallback(callbackId, (error, data) => {
            console.log("unregisterEnvironmentCallback success, err: " + JSON.stringify(error));
        });
    }
}
  ```

## ApplicationContext.unregisterEnvironmentCallback

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

D
dy_study 已提交
179
取消对系统环境变化的监听。使用callback异步回调。
D
dy_study 已提交
180 181 182 183 184

**系统能力**:SystemCapability.Ability.AbilityRuntime.Core

**参数:**

D
dy_study 已提交
185
| 参数名         | 类型     | 必填 | 说明                       |
D
dy_study 已提交
186
| ------------- | -------- | ---- | -------------------------- |
D
dy_study 已提交
187
| callbackId    | number   | 是   | 注册监听系统环境变化的ID。   |
188
| callback | AsyncCallback\<void> | 是   | 回调方法。                  |
D
dy_study 已提交
189 190 191 192 193 194 195 196 197 198

**示例:**

  ```js
  let applicationContext = this.context.getApplicationContext();
  let callbackId = 1;
  applicationContext.unregisterEnvironmentCallback(callbackId, (error, data) => {
      console.log("unregisterEnvironmentCallback success, err: " + JSON.stringify(error));
  });
  ```