1e41f4b71Sopenharmony_ci# @ohos.app.ability.EnvironmentCallback (EnvironmentCallback) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **EnvironmentCallback** module provides APIs for the application context to listen for system environment changes. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 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. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> The APIs of this module can be used only in the stage model. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## Modules to Import 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_ciCalled when the system environment changes. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**Parameters** 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci | Name| Type| Mandatory| Description| 32e41f4b71Sopenharmony_ci | -------- | -------- | -------- | -------- | 33e41f4b71Sopenharmony_ci | config | [Configuration](js-apis-app-ability-configuration.md) | Yes| **Configuration** object after the change.| 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**Example** 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ciSee [Usage of EnvironmentCallback](#usage-of-environmentcallback). 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci## EnvironmentCallback.onMemoryLevel 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_cionMemoryLevel(level: AbilityConstant.MemoryLevel): void 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ciCalled when the system memory level changes. 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci**Parameters** 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci | Name| Type| Mandatory| Description| 52e41f4b71Sopenharmony_ci | -------- | -------- | -------- | -------- | 53e41f4b71Sopenharmony_ci | level | [AbilityConstant.MemoryLevel](js-apis-app-ability-abilityConstant.md#memorylevel) | Yes| Memory level that indicates the memory usage status. When the specified memory level is reached, a callback will be invoked and the system will start adjustment.| 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci**Example** 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ciSee [Usage of EnvironmentCallback](#usage-of-environmentcallback). 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci## Usage of EnvironmentCallback 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci**Example** 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. Obtain an applicationContext object. 82e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 83e41f4b71Sopenharmony_ci try { 84e41f4b71Sopenharmony_ci // 2. Register a listener for the environment changes through the applicationContext object. 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``` 108