1e41f4b71Sopenharmony_ci# @ohos.app.ability.EnvironmentCallback (EnvironmentCallback)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciEnvironmentCallback模块提供应用上下文ApplicationContext对系统环境变化监听回调的能力。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **说明:**
6e41f4b71Sopenharmony_ci> 
7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 9 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> 本模块接口仅可在Stage模型下使用。
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci## 导入模块
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci```ts
15e41f4b71Sopenharmony_ciimport { EnvironmentCallback } from '@kit.AbilityKit';
16e41f4b71Sopenharmony_ci```
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci## EnvironmentCallback.onConfigurationUpdated
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_cionConfigurationUpdated(config: Configuration): void
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci注册系统环境变化的监听后,在系统环境变化时触发回调。
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**参数:**
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci  | 参数名 | 类型 | 必填 | 说明 | 
32e41f4b71Sopenharmony_ci  | -------- | -------- | -------- | -------- |
33e41f4b71Sopenharmony_ci  | config | [Configuration](js-apis-app-ability-configuration.md) | 是 | 变化后的Configuration对象。 |
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci**示例:**
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci参见[EnvironmentCallback使用](#environmentcallback使用)。
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci## EnvironmentCallback.onMemoryLevel
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_cionMemoryLevel(level: AbilityConstant.MemoryLevel): void
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci注册系统环境变化的监听后,在系统内存变化时触发回调。
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci**参数:**
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci  | 参数名 | 类型 | 必填 | 说明 | 
52e41f4b71Sopenharmony_ci  | -------- | -------- | -------- | -------- |
53e41f4b71Sopenharmony_ci  | level | [AbilityConstant.MemoryLevel](js-apis-app-ability-abilityConstant.md#memorylevel) | 是 | 回调返回内存微调级别,显示当前内存使用状态。|
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci**示例:**
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ci参见[EnvironmentCallback使用](#environmentcallback使用)。
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci## EnvironmentCallback使用
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci**示例:**
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci```ts
64e41f4b71Sopenharmony_ciimport { UIAbility, EnvironmentCallback } from '@kit.AbilityKit';
65e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_cilet callbackId: number;
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ciexport default class MyAbility extends UIAbility {
70e41f4b71Sopenharmony_ci  onCreate() {
71e41f4b71Sopenharmony_ci    console.log('MyAbility onCreate');
72e41f4b71Sopenharmony_ci    let environmentCallback: EnvironmentCallback  =  {
73e41f4b71Sopenharmony_ci      onConfigurationUpdated(config){
74e41f4b71Sopenharmony_ci        console.log(`onConfigurationUpdated config: ${JSON.stringify(config)}`);
75e41f4b71Sopenharmony_ci      },
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci      onMemoryLevel(level){
78e41f4b71Sopenharmony_ci        console.log(`onMemoryLevel level: ${JSON.stringify(level)}`);
79e41f4b71Sopenharmony_ci      }
80e41f4b71Sopenharmony_ci    };
81e41f4b71Sopenharmony_ci    // 1.获取applicationContext
82e41f4b71Sopenharmony_ci    let applicationContext = this.context.getApplicationContext();
83e41f4b71Sopenharmony_ci    try {
84e41f4b71Sopenharmony_ci      // 2.通过applicationContext注册监听应用内生命周期
85e41f4b71Sopenharmony_ci      callbackId = applicationContext.on('environment', environmentCallback);
86e41f4b71Sopenharmony_ci    } catch (paramError) {
87e41f4b71Sopenharmony_ci      console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
88e41f4b71Sopenharmony_ci    }
89e41f4b71Sopenharmony_ci    console.log(`registerEnvironmentCallback number: ${JSON.stringify(callbackId)}`);
90e41f4b71Sopenharmony_ci  }
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci  onDestroy() {
93e41f4b71Sopenharmony_ci    let applicationContext = this.context.getApplicationContext();
94e41f4b71Sopenharmony_ci    try {
95e41f4b71Sopenharmony_ci      applicationContext.off('environment', callbackId, (error, data) => {
96e41f4b71Sopenharmony_ci        if (error && error.code !== 0) {
97e41f4b71Sopenharmony_ci          console.error(`unregisterEnvironmentCallback fail, error: ${JSON.stringify(error)}`);
98e41f4b71Sopenharmony_ci        } else {
99e41f4b71Sopenharmony_ci          console.log(`unregisterEnvironmentCallback success, data: ${JSON.stringify(data)}`);
100e41f4b71Sopenharmony_ci        }
101e41f4b71Sopenharmony_ci      });
102e41f4b71Sopenharmony_ci    } catch (paramError) {
103e41f4b71Sopenharmony_ci      console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
104e41f4b71Sopenharmony_ci    }
105e41f4b71Sopenharmony_ci  }
106e41f4b71Sopenharmony_ci}
107e41f4b71Sopenharmony_ci```