1e41f4b71Sopenharmony_ci# Subscribing to System Environment Variable Changes 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciSystem 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. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ciBy 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. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciThe 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-ability-kit/js-apis-app-ability-configuration.md). 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciYou can subscribe to system environment variable changes in the following ways: 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- [Using ApplicationContext for Subscription](#using-applicationcontext-for-subscription) 12e41f4b71Sopenharmony_ci- [Using AbilityStage for Subscription](#using-abilitystage-for-subscription) 13e41f4b71Sopenharmony_ci- [Using UIAbility for Subscription](#using-uiability-for-subscription) 14e41f4b71Sopenharmony_ci- [Using ExtensionAbility for Subscription](#using-extensionability-for-subscription) 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Using ApplicationContext for Subscription 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci[ApplicationContext](../reference/apis-ability-kit/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. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci1. Non-application components can call [on](../reference/apis-ability-kit/js-apis-inner-application-applicationContext.md#applicationcontextonenvironment) to subscribe to changes in system environment variables. The code snippet below is used to subscribe to system language changes on a page. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci ```ts 23e41f4b71Sopenharmony_ci import { common, EnvironmentCallback, Configuration } from '@kit.AbilityKit'; 24e41f4b71Sopenharmony_ci import { hilog } from '@kit.PerformanceAnalysisKit'; 25e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci const TAG: string = '[CollaborateAbility]'; 28e41f4b71Sopenharmony_ci const DOMAIN_NUMBER: number = 0xFF00; 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci @Entry 31e41f4b71Sopenharmony_ci @Component 32e41f4b71Sopenharmony_ci struct Index { 33e41f4b71Sopenharmony_ci private context = getContext(this) as common.UIAbilityContext; 34e41f4b71Sopenharmony_ci private callbackId: number = 0; // ID of the subscription for system environment variable changes. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci subscribeConfigurationUpdate(): void { 37e41f4b71Sopenharmony_ci let systemLanguage: string | undefined = this.context.config.language; // Obtain the system language in use. 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci // 1. Obtain an ApplicationContext object. 40e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci // 2. Subscribe to system environment variable changes through ApplicationContext. 43e41f4b71Sopenharmony_ci let environmentCallback: EnvironmentCallback = { 44e41f4b71Sopenharmony_ci onConfigurationUpdated(newConfig: Configuration) { 45e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)}`); 46e41f4b71Sopenharmony_ci if (this.systemLanguage !== newConfig.language) { 47e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `systemLanguage from ${systemLanguage} changed to ${newConfig.language}`); 48e41f4b71Sopenharmony_ci systemLanguage = newConfig.language; // Save the new system language as the system language in use, which will be used for comparison. 49e41f4b71Sopenharmony_ci } 50e41f4b71Sopenharmony_ci }, 51e41f4b71Sopenharmony_ci onMemoryLevel(level) { 52e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `onMemoryLevel level: ${level}`); 53e41f4b71Sopenharmony_ci } 54e41f4b71Sopenharmony_ci } 55e41f4b71Sopenharmony_ci try { 56e41f4b71Sopenharmony_ci this.callbackId = applicationContext.on('environment', environmentCallback); 57e41f4b71Sopenharmony_ci } catch (err) { 58e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 59e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 60e41f4b71Sopenharmony_ci hilog.error(DOMAIN_NUMBER, TAG, `Failed to register applicationContext. Code is ${code}, message is ${message}`); 61e41f4b71Sopenharmony_ci } 62e41f4b71Sopenharmony_ci } 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci // Page display. 65e41f4b71Sopenharmony_ci build() { 66e41f4b71Sopenharmony_ci //... 67e41f4b71Sopenharmony_ci } 68e41f4b71Sopenharmony_ci } 69e41f4b71Sopenharmony_ci ``` 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci2. They can call [off](../reference/apis-ability-kit/js-apis-inner-application-applicationContext.md#applicationcontextoffenvironment-1) to release the resources. 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci ```ts 74e41f4b71Sopenharmony_ci import { common } from '@kit.AbilityKit'; 75e41f4b71Sopenharmony_ci import { hilog } from '@kit.PerformanceAnalysisKit'; 76e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci const TAG: string = '[CollaborateAbility]'; 79e41f4b71Sopenharmony_ci const DOMAIN_NUMBER: number = 0xFF00; 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci @Entry 82e41f4b71Sopenharmony_ci @Component 83e41f4b71Sopenharmony_ci struct Index { 84e41f4b71Sopenharmony_ci private context = getContext(this) as common.UIAbilityContext; 85e41f4b71Sopenharmony_ci private callbackId: number = 0; // ID of the subscription for system environment variable changes. 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci unsubscribeConfigurationUpdate() { 88e41f4b71Sopenharmony_ci let applicationContext = this.context.getApplicationContext(); 89e41f4b71Sopenharmony_ci try { 90e41f4b71Sopenharmony_ci applicationContext.off('environment', this.callbackId); 91e41f4b71Sopenharmony_ci } catch (err) { 92e41f4b71Sopenharmony_ci let code = (err as BusinessError).code; 93e41f4b71Sopenharmony_ci let message = (err as BusinessError).message; 94e41f4b71Sopenharmony_ci hilog.error(DOMAIN_NUMBER, TAG, `Failed to unregister applicationContext. Code is ${code}, message is ${message}`); 95e41f4b71Sopenharmony_ci } 96e41f4b71Sopenharmony_ci } 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci // Page display. 99e41f4b71Sopenharmony_ci build() { 100e41f4b71Sopenharmony_ci //... 101e41f4b71Sopenharmony_ci } 102e41f4b71Sopenharmony_ci } 103e41f4b71Sopenharmony_ci ``` 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci## Using AbilityStage for Subscription 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ciThe AbilityStage component provides the [AbilityStage.onConfigurationUpdate()](../reference/apis-ability-kit/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-ability-kit/js-apis-app-ability-configuration.md) object. 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci> **NOTE** 110e41f4b71Sopenharmony_ci> 111e41f4b71Sopenharmony_ci> - [AbilityStage](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md) 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). 112e41f4b71Sopenharmony_ci> - The callback used to subscribe to system environment variable changes has the same lifecycle as the AbilityStage instance and will be destroyed when the module is destroyed. 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ciThe code snippet below uses the [AbilityStage.onConfigurationUpdate()](../reference/apis-ability-kit/js-apis-app-ability-abilityStage.md#abilitystageonconfigurationupdate) callback to subscribe to the system language changes. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci```ts 117e41f4b71Sopenharmony_ciimport { AbilityStage, Configuration } from '@kit.AbilityKit'; 118e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ciconst TAG: string = '[MyAbilityStage]'; 121e41f4b71Sopenharmony_ciconst DOMAIN_NUMBER: number = 0xFF00; 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_cilet systemLanguage: string | undefined; // System language in use. 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ciexport default class MyAbilityStage extends AbilityStage { 126e41f4b71Sopenharmony_ci onCreate(): void { 127e41f4b71Sopenharmony_ci systemLanguage = this.context.config.language; // Obtain the system language in use when the module is loaded for the first time. 128e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `systemLanguage is ${systemLanguage}`); 129e41f4b71Sopenharmony_ci //... 130e41f4b71Sopenharmony_ci } 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci onConfigurationUpdate(newConfig: Configuration): void { 133e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `onConfigurationUpdate, language: ${newConfig.language}`); 134e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)}`); 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci if (systemLanguage !== newConfig.language) { 137e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `systemLanguage from ${systemLanguage} changed to ${newConfig.language}`); 138e41f4b71Sopenharmony_ci systemLanguage = newConfig.language; // Save the new system language as the system language in use, which will be used for comparison. 139e41f4b71Sopenharmony_ci } 140e41f4b71Sopenharmony_ci } 141e41f4b71Sopenharmony_ci} 142e41f4b71Sopenharmony_ci``` 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci## Using UIAbility for Subscription 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ciThe [UIAbility](../reference/apis-ability-kit/js-apis-app-ability-uiAbility.md) component provides the [UIAbility.onConfigurationUpdate()](../reference/apis-ability-kit/js-apis-app-ability-ability.md#abilityonconfigurationupdate) 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-ability-kit/js-apis-app-ability-configuration.md) object, without restarting the UIAbility. 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci> **NOTE** 149e41f4b71Sopenharmony_ci> 150e41f4b71Sopenharmony_ci> 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. 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ciThe code snippet below uses the [onConfigurationUpdate()](../reference/apis-ability-kit/js-apis-app-ability-ability.md#abilityonconfigurationupdate) callback to subscribe to the system language changes. 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci```ts 155e41f4b71Sopenharmony_ciimport { AbilityConstant, Configuration, UIAbility, Want } from '@kit.AbilityKit'; 156e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ciconst TAG: string = '[EntryAbility]'; 159e41f4b71Sopenharmony_ciconst DOMAIN_NUMBER: number = 0xFF00; 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_cilet systemLanguage: string | undefined; // System language in use. 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 164e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 165e41f4b71Sopenharmony_ci systemLanguage = this.context.config.language; // Obtain the system language in use when the UIAbility instance is loaded for the first time. 166e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `systemLanguage is ${systemLanguage}`); 167e41f4b71Sopenharmony_ci } 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci onConfigurationUpdate(newConfig: Configuration): void { 170e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `onConfigurationUpdated systemLanguage is ${systemLanguage}, newConfig: ${JSON.stringify(newConfig)}`); 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci if (systemLanguage !== newConfig.language) { 173e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, `systemLanguage from ${systemLanguage} changed to ${newConfig.language}`); 174e41f4b71Sopenharmony_ci systemLanguage = newConfig.language; // Save the new system language as the system language in use, which will be used for comparison. 175e41f4b71Sopenharmony_ci } 176e41f4b71Sopenharmony_ci } 177e41f4b71Sopenharmony_ci // ... 178e41f4b71Sopenharmony_ci} 179e41f4b71Sopenharmony_ci``` 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci## Using ExtensionAbility for Subscription 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ciThe [ExtensionAbility](../reference/apis-ability-kit/js-apis-app-ability-extensionAbility.md) component provides the [onConfigurationUpdate()](../reference/apis-ability-kit/js-apis-app-ability-ability.md#abilityonconfigurationupdate) 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-ability-kit/js-apis-app-ability-configuration.md) object. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci> **NOTE** 186e41f4b71Sopenharmony_ci> 187e41f4b71Sopenharmony_ci> 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. 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ciThe code snippet below uses [FormExtensionAbility](../reference/apis-form-kit/js-apis-app-form-formExtensionAbility.md) as an example to describe how to use the [onConfigurationUpdate()](../reference/apis-ability-kit/js-apis-app-ability-ability.md#abilityonconfigurationupdate) callback to subscribe to system environment variable changes. 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci```ts 192e41f4b71Sopenharmony_ciimport { FormExtensionAbility } from '@kit.FormKit'; 193e41f4b71Sopenharmony_ciimport { Configuration } from '@kit.AbilityKit'; 194e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ciconst TAG: string = '[EntryAbility]'; 197e41f4b71Sopenharmony_ciconst DOMAIN_NUMBER: number = 0xFF00; 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ciexport default class EntryFormAbility extends FormExtensionAbility { 200e41f4b71Sopenharmony_ci onConfigurationUpdate(config: Configuration) { 201e41f4b71Sopenharmony_ci hilog.info(DOMAIN_NUMBER, TAG, '[EntryFormAbility] onConfigurationUpdate:' + JSON.stringify(config)); 202e41f4b71Sopenharmony_ci } 203e41f4b71Sopenharmony_ci // ... 204e41f4b71Sopenharmony_ci} 205e41f4b71Sopenharmony_ci``` 206