• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Subscribing to System Environment Variable Changes
2
3System environment variables are system settings (for example, the system language or screen direction) of a device that may change during the running of an application.
4
5By subscribing to the changes of system environment variables, the application can detect the changes in a timely manner and process the changes accordingly, providing better user experience. For example, when the system language changes, the application can display the UI in the new language; when the user rotates the device to landscape or portrait mode, the application can re-arrange the UI to adapt to the new screen orientation and size.
6
7The system environment variable changes are usually triggered by options in **Settings** or icons in **Control Panel**. For details about the system environment variables that support subscription, see [Configuration](../reference/apis/js-apis-app-ability-configuration.md).
8
9In OpenHarmony, you can subscribe to system environment variable changes in the following ways:
10
11- [Using ApplicationContext for Subscription](#using-applicationcontext-for-subscription)
12- [Using AbilityStage for Subscription](#using-abilitystage-for-subscription)
13- [Using UIAbility for Subscription](#using-uiability-for-subscription)
14- [Using ExtensionAbility for Subscription](#using-extensionability-for-subscription)
15
16## Using ApplicationContext for Subscription
17
18[ApplicationContext](../reference/apis/js-apis-inner-application-applicationContext.md) provides an API for registering a callback function to subscribe to the system environment variable changes. It also provides an API for deregistration so you can release related resources when they are no longer needed.
19
201. Call **ApplicationContext.on(type: 'environment', callback: EnvironmentCallback)** to subscribe to changes in system environment variables. The code snippet below is used to subscribe to system language changes on a page.
21
22   ```ts
23   import common from '@ohos.app.ability.common';
24
25   @Entry
26   @Component
27   struct Index {
28     private context = getContext(this) as common.UIAbilityContext;
29     private callbackId: number; // ID of the subscription for system environment variable changes.
30
31     subscribeConfigurationUpdate() {
32       let systemLanguage: string = this.context.config.language; // Obtain the system language in use.
33
34       // 1. Obtain an ApplicationContext object.
35       let applicationContext = this.context.getApplicationContext();
36
37       // 2. Subscribe to system environment variable changes through ApplicationContext.
38       let environmentCallback = {
39         onConfigurationUpdated(newConfig) {
40           console.info(`onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)}`);
41
42           if (this.systemLanguage !== newConfig.language) {
43             console.info(`systemLanguage from ${systemLanguage} changed to ${newConfig.language}`);
44             systemLanguage = newConfig.language; // Save the new system language as the system language in use, which will be used for comparison.
45           }
46         },
47         onMemoryLevel(level) {
48           console.info(`onMemoryLevel level: ${level}`);
49         }
50       }
51
52       this.callbackId = applicationContext.on('environment', environmentCallback);
53     }
54
55     // Page display.
56     build() {
57       ...
58     }
59   }
60   ```
61
622. Call **ApplicationContext.off(type: 'environment', callbackId: number)** to release the resources.
63
64   ```ts
65   import common from '@ohos.app.ability.common';
66
67   @Entry
68   @Component
69   struct Index {
70     private context = getContext(this) as common.UIAbilityContext;
71     private callbackId: number; // ID of the subscription for system environment variable changes.
72
73     unsubscribeConfigurationUpdate() {
74       let applicationContext = this.context.getApplicationContext();
75       applicationContext.off('environment', this.callbackId);
76     }
77
78     // Page display.
79     build() {
80       ...
81     }
82   }
83   ```
84
85## Using AbilityStage for Subscription
86
87The AbilityStage component provides the [AbilityStage.onConfigurationUpdate()](../reference/apis/js-apis-app-ability-abilityStage.md#abilitystageonconfigurationupdate) callback for subscribing to system environment variable changes. This callback is invoked when a system environment variable changes. In this callback, the latest system environment configuration is obtained through the [Configuration](../reference/apis/js-apis-app-ability-configuration.md) object.
88
89> **NOTE**
90>
91> - AbilityStage is not automatically generated in the default project of DevEco Studio. For details about how to create an AbilityStage file, see [AbilityStage Component Container](abilitystage.md).
92> - The callback used to subscribe to system environment variable changes has the same lifecycle as the [AbilityStage](../reference/apis/js-apis-app-ability-abilityStage.md) instance and will be destroyed when the instance is destroyed.
93
94The code snippet below uses the [AbilityStage.onConfigurationUpdate()](../reference/apis/js-apis-app-ability-abilityStage.md#abilitystageonconfigurationupdate) callback to subscribe to the system language changes.
95
96```ts
97import AbilityStage from '@ohos.app.ability.AbilityStage';
98
99let systemLanguage: string; // System language in use.
100
101export default class MyAbilityStage extends AbilityStage {
102  onCreate() {
103    systemLanguage = this.context.config.language; // Obtain the system language in use when the AbilityStage instance is loaded for the first time.
104    console.info(`systemLanguage is ${systemLanguage} `);
105  }
106
107  onConfigurationUpdate(newConfig) {
108    console.info(`onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)}`);
109
110    if (systemLanguage !== newConfig.language) {
111      console.info(`systemLanguage from ${systemLanguage} changed to ${newConfig.language}`);
112      systemLanguage = newConfig.language; // Save the new system language as the system language in use, which will be used for comparison.
113    }
114  }
115}
116```
117
118## Using UIAbility for Subscription
119
120The UIAbility component provides the **UIAbility.onConfigurationUpdate()** callback for subscribing to system environment variable changes. This callback is invoked when a system environment variable changes. In this callback, the latest system environment configuration is obtained through the [Configuration](../reference/apis/js-apis-app-ability-configuration.md) object, without restarting the UIAbility.
121
122> **NOTE**
123>
124> The callback used to subscribe to system environment variable changes has the same lifecycle as the UIAbility instance and will be destroyed when the instance is destroyed.
125
126The code snippet below uses the **onConfigurationUpdate()** callback to subscribe to the system language changes.
127
128```ts
129import UIAbility from '@ohos.app.ability.UIAbility';
130
131let systemLanguage: string; // System language in use.
132
133export default class EntryAbility extends UIAbility {
134  onCreate(want, launchParam) {
135    systemLanguage = this.context.config.language; // Obtain the system language in use when the UIAbility instance is loaded for the first time.
136    console.info(`systemLanguage is ${systemLanguage} `);
137  }
138
139  onConfigurationUpdate(newConfig) {
140    console.info(`onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)}`);
141
142    if (systemLanguage !== newConfig.language) {
143      console.info(`systemLanguage from ${systemLanguage} changed to ${newConfig.language}`);
144      systemLanguage = newConfig.language; // Save the new system language as the system language in use, which will be used for comparison.
145    }
146  }
147
148  ...
149}
150```
151
152## Using ExtensionAbility for Subscription
153
154The ExtensionAbility component provides the **onConfigurationUpdate()** callback for subscribing system environment variable changes. This callback is invoked when a system environment variable changes. In this callback, the latest system environment configuration is obtained through the [Configuration](../reference/apis/js-apis-app-ability-configuration.md) object.
155
156> **NOTE**
157>
158> The callback used to subscribe to system environment variable changes has the same lifecycle as the ExtensionAbility instance and will be destroyed when the instance is destroyed.
159
160The code snippet below uses FormExtensionAbility as an example to describe how to use the **onConfigurationUpdate()** callback to subscribe to system environment variable changes.
161
162```ts
163import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
164
165export default class EntryFormAbility extends FormExtensionAbility {
166  onConfigurationUpdate(newConfig) {
167    console.info(`newConfig is ${JSON.stringify(newConfig)}`);
168  }
169
170  ...
171}
172```
173