js-apis-app-ability-environmentCallback.md 3.0 KB
Newer Older
1
# @ohos.app.ability.EnvironmentCallback (EnvironmentCallback)
2 3 4 5 6 7 8 9 10 11 12

The **EnvironmentCallback** module provides the **onConfigurationUpdated** API for the application context to listen for system environment changes.

> **NOTE**
> 
> 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. 
> The APIs of this module can be used only in the stage model.


## Modules to Import

13
```ts
14
import EnvironmentCallback from '@ohos.app.ability.EnvironmentCallback';
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
```


## EnvironmentCallback.onConfigurationUpdated

onConfigurationUpdated(config: Configuration): void;

Called when the system environment changes.

**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore

**Parameters**

  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
30
  | config | [Configuration](js-apis-app-ability-configuration.md) | Yes| **Configuration** object after the change.|
31

32 33 34 35 36 37
## EnvironmentCallback.onMemoryLevel

onMemoryLevel(level: AbilityConstant.MemoryLevel): void;

Called when the system memory level changes.

G
Gloria 已提交
38
**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore
39 40 41 42 43 44 45

**Parameters**

  | Name| Type| Mandatory| Description| 
  | -------- | -------- | -------- | -------- |
  | 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.| 

46 47 48
**Example**
    

49
  ```ts
50
import UIAbility from '@ohos.app.ability.Ability';
51

52
let callbackId;
53

54
export default class MyAbility extends UIAbility {
55
    onCreate() {
56
        console.log('MyAbility onCreate');
57
        globalThis.applicationContext = this.context.getApplicationContext();
G
Gloria 已提交
58
        let environmentCallback  =  {
59
            onConfigurationUpdated(config){
60 61 62 63 64
                console.log('onConfigurationUpdated config: ${JSON.stringify(config)}');
            }

            onMemoryLevel(level){
                console.log('onMemoryLevel level: ${JSON.stringify(level)}');
65
            }
66
        };
67 68 69
        // 1. Obtain an applicationContext object.
        let applicationContext = globalThis.applicationContext;
        // 2. Register a listener for the environment changes through the applicationContext object.
G
Gloria 已提交
70
        callbackId = applicationContext.registerEnvironmentCallback(environmentCallback);
71
        console.log('registerEnvironmentCallback number: ${JSON.stringify(callbackId)}');
72 73 74 75
    }
    onDestroy() {
        let applicationContext = globalThis.applicationContext;
        applicationContext.unregisterEnvironmentCallback(callbackId, (error, data) => {
76 77 78 79 80
            if (error && error.code !== 0) {
                console.error('unregisterEnvironmentCallback fail, error: ${JSON.stringify(error)}');
            } else {
                console.log('unregisterEnvironmentCallback success, data: ${JSON.stringify(data)}');
            }
81 82 83 84
        });
    }
}
  ```