1# @ohos.app.ability.EnvironmentCallback (EnvironmentCallback) 2 3The **EnvironmentCallback** module provides APIs for the application context to listen for system environment changes. 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> The APIs of this module can be used only in the stage model. 10 11 12## Modules to Import 13 14```ts 15import { EnvironmentCallback } from '@kit.AbilityKit'; 16``` 17 18 19## EnvironmentCallback.onConfigurationUpdated 20 21onConfigurationUpdated(config: Configuration): void 22 23Called when the system environment changes. 24 25**Atomic service API**: This API can be used in atomic services since API version 11. 26 27**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 28 29**Parameters** 30 31 | Name| Type| Mandatory| Description| 32 | -------- | -------- | -------- | -------- | 33 | config | [Configuration](js-apis-app-ability-configuration.md) | Yes| **Configuration** object after the change.| 34 35**Example** 36 37See [Usage of EnvironmentCallback](#usage-of-environmentcallback). 38 39## EnvironmentCallback.onMemoryLevel 40 41onMemoryLevel(level: AbilityConstant.MemoryLevel): void 42 43Called when the system memory level changes. 44 45**Atomic service API**: This API can be used in atomic services since API version 11. 46 47**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 48 49**Parameters** 50 51 | Name| Type| Mandatory| Description| 52 | -------- | -------- | -------- | -------- | 53 | 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.| 54 55**Example** 56 57See [Usage of EnvironmentCallback](#usage-of-environmentcallback). 58 59## Usage of EnvironmentCallback 60 61**Example** 62 63```ts 64import { UIAbility, EnvironmentCallback } from '@kit.AbilityKit'; 65import { BusinessError } from '@kit.BasicServicesKit'; 66 67let callbackId: number; 68 69export default class MyAbility extends UIAbility { 70 onCreate() { 71 console.log('MyAbility onCreate'); 72 let environmentCallback: EnvironmentCallback = { 73 onConfigurationUpdated(config){ 74 console.log(`onConfigurationUpdated config: ${JSON.stringify(config)}`); 75 }, 76 77 onMemoryLevel(level){ 78 console.log(`onMemoryLevel level: ${JSON.stringify(level)}`); 79 } 80 }; 81 // 1. Obtain an applicationContext object. 82 let applicationContext = this.context.getApplicationContext(); 83 try { 84 // 2. Register a listener for the environment changes through the applicationContext object. 85 callbackId = applicationContext.on('environment', environmentCallback); 86 } catch (paramError) { 87 console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 88 } 89 console.log(`registerEnvironmentCallback number: ${JSON.stringify(callbackId)}`); 90 } 91 92 onDestroy() { 93 let applicationContext = this.context.getApplicationContext(); 94 try { 95 applicationContext.off('environment', callbackId, (error, data) => { 96 if (error && error.code !== 0) { 97 console.error(`unregisterEnvironmentCallback fail, error: ${JSON.stringify(error)}`); 98 } else { 99 console.log(`unregisterEnvironmentCallback success, data: ${JSON.stringify(data)}`); 100 } 101 }); 102 } catch (paramError) { 103 console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`); 104 } 105 } 106} 107``` 108