• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.EnvironmentCallback (EnvironmentCallback)
2
3The **EnvironmentCallback** module provides the **onConfigurationUpdated** API 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> The APIs of this module can be used only in the stage model.
9
10
11## Modules to Import
12
13```ts
14import EnvironmentCallback from '@ohos.app.ability.EnvironmentCallback';
15```
16
17
18## EnvironmentCallback.onConfigurationUpdated
19
20onConfigurationUpdated(config: Configuration): void;
21
22Called when the system environment changes.
23
24**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
25
26**Parameters**
27
28  | Name| Type| Mandatory| Description|
29  | -------- | -------- | -------- | -------- |
30  | config | [Configuration](js-apis-app-ability-configuration.md) | Yes| **Configuration** object after the change.|
31
32## EnvironmentCallback.onMemoryLevel
33
34onMemoryLevel(level: AbilityConstant.MemoryLevel): void;
35
36Called when the system memory level changes.
37
38**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
39
40**Parameters**
41
42  | Name| Type| Mandatory| Description|
43  | -------- | -------- | -------- | -------- |
44  | level | [AbilityConstant.MemoryLevel](js-apis-app-ability-abilityConstant.md#abilityconstantmemorylevel) | 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.|
45
46**Example**
47
48
49  ```ts
50import UIAbility from '@ohos.app.ability.UIAbility';
51
52let callbackId;
53
54export default class MyAbility extends UIAbility {
55    onCreate() {
56        console.log('MyAbility onCreate');
57        globalThis.applicationContext = this.context.getApplicationContext();
58        let EnvironmentCallback  =  {
59            onConfigurationUpdated(config){
60                console.log('onConfigurationUpdated config:' + JSON.stringify(config));
61            },
62            onMemoryLevel(level){
63                console.log('onMemoryLevel level: ${JSON.stringify(level)}');
64            }
65        };
66        // 1. Obtain an applicationContext object.
67        let applicationContext = globalThis.applicationContext;
68        // 2. Register a listener for the environment changes through the applicationContext object.
69        callbackId = applicationContext.registerEnvironmentCallback(EnvironmentCallback);
70        console.log('registerEnvironmentCallback number: ' + JSON.stringify(callbackId));
71    }
72    onDestroy() {
73        let applicationContext = globalThis.applicationContext;
74        applicationContext.unregisterEnvironmentCallback(callbackId, (error, data) => {
75            console.log('unregisterEnvironmentCallback success, err: ' + JSON.stringify(error));
76        });
77    }
78}
79  ```
80