• 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 orientation) 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   import { Configuration } from '@ohos.app.ability.Configuration';
25   import EnvironmentCallback from '@ohos.app.ability.EnvironmentCallback';
26
27   @Entry
28   @Component
29   struct Index {
30     private context = getContext(this) as common.UIAbilityContext;
31     private callbackId: number = 0; // ID of the subscription for system environment variable changes.
32
33     subscribeConfigurationUpdate() {
34       let systemLanguage: string | undefined = this.context.config.language; // Obtain the system language in use.
35
36       // 1. Obtain an ApplicationContext object.
37       let applicationContext = this.context.getApplicationContext();
38
39       // 2. Subscribe to system environment variable changes through ApplicationContext.
40       let environmentCallback: EnvironmentCallback = {
41         onConfigurationUpdated(newConfig: Configuration) {
42           console.info(`onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)}`);
43
44           if (this.systemLanguage !== newConfig.language) {
45             console.info(`systemLanguage from ${systemLanguage} changed to ${newConfig.language}`);
46             systemLanguage = newConfig.language; // Save the new system language as the system language in use, which will be used for comparison.
47           }
48         },
49         onMemoryLevel(level) {
50           console.info(`onMemoryLevel level: ${level}`);
51         }
52       }
53
54       this.callbackId = applicationContext.on('environment', environmentCallback);
55     }
56
57     // Page display.
58     build() {
59       ...
60     }
61   }
62   ```
63
642. Call **ApplicationContext.off(type: 'environment', callbackId: number)** to release the resources.
65
66   ```ts
67   import common from '@ohos.app.ability.common';
68
69   @Entry
70   @Component
71   struct Index {
72     private context = getContext(this) as common.UIAbilityContext;
73     private callbackId: number = 0; // ID of the subscription for system environment variable changes.
74
75     unsubscribeConfigurationUpdate() {
76       let applicationContext = this.context.getApplicationContext();
77       applicationContext.off('environment', this.callbackId);
78     }
79
80     // Page display.
81     build() {
82       ...
83     }
84   }
85   ```
86
87## Using AbilityStage for Subscription
88
89The 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.
90
91> **NOTE**
92>
93> - 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).
94> - 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.
95
96The code snippet below uses the [AbilityStage.onConfigurationUpdate()](../reference/apis/js-apis-app-ability-abilityStage.md#abilitystageonconfigurationupdate) callback to subscribe to the system language changes.
97
98```ts
99import AbilityStage from '@ohos.app.ability.AbilityStage';
100import { Configuration } from '@ohos.app.ability.Configuration';
101
102let systemLanguage: string | undefined; // System language in use.
103
104export default class MyAbilityStage extends AbilityStage {
105  onCreate() {
106    systemLanguage = this.context.config.language; // Obtain the system language in use when the AbilityStage instance is loaded for the first time.
107    console.info(`systemLanguage is ${systemLanguage} `);
108  }
109
110  onConfigurationUpdate(newConfig: Configuration) {
111    console.info(`onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)}`);
112
113    if (systemLanguage !== newConfig.language) {
114      console.info(`systemLanguage from ${systemLanguage} changed to ${newConfig.language}`);
115      systemLanguage = newConfig.language; // Save the new system language as the system language in use, which will be used for comparison.
116    }
117  }
118}
119```
120
121## Using UIAbility for Subscription
122
123The 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.
124
125> **NOTE**
126>
127> 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.
128
129The code snippet below uses the **onConfigurationUpdate()** callback to subscribe to the system language changes.
130
131```ts
132import UIAbility from '@ohos.app.ability.UIAbility';
133import AbilityConstant from '@ohos.app.ability.AbilityConstant';
134import Want from '@ohos.app.ability.Want';
135import { Configuration } from '@ohos.app.ability.Configuration';
136
137let systemLanguage: string | undefined; // System language in use.
138
139export default class EntryAbility extends UIAbility {
140  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
141    systemLanguage = this.context.config.language; // Obtain the system language in use when the UIAbility instance is loaded for the first time.
142    console.info(`systemLanguage is ${systemLanguage} `);
143  }
144
145  onConfigurationUpdate(newConfig: Configuration) {
146    console.info(`onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)}`);
147
148    if (systemLanguage !== newConfig.language) {
149      console.info(`systemLanguage from ${systemLanguage} changed to ${newConfig.language}`);
150      systemLanguage = newConfig.language; // Save the new system language as the system language in use, which will be used for comparison.
151    }
152  }
153
154  // ...
155}
156```
157
158## Using ExtensionAbility for Subscription
159
160The 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.
161
162> **NOTE**
163>
164> 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.
165
166The code snippet below uses FormExtensionAbility as an example to describe how to use the **onConfigurationUpdate()** callback to subscribe to system environment variable changes.
167
168```ts
169import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
170import { Configuration } from '@ohos.app.ability.Configuration';
171
172export default class EntryFormAbility extends FormExtensionAbility {
173  onConfigurationUpdate(newConfig: Configuration) {
174    console.info(`newConfig is ${JSON.stringify(newConfig)}`);
175  }
176
177  // ...
178}
179```
180