• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 订阅系统环境变量的变化
2
3系统环境变量是指:在应用程序运行期间,终端设备的系统设置(例如系统的语言环境、屏幕方向等)发生变化。
4
5开发者通过订阅系统环境变化,可以使应用程序及时感知这种变化,并作出相应处理,从而提供更好的用户体验。例如,用户更改系统语言设置时,应用程序可以自动根据新的语言设置更新用户界面的语言;当用户将设备旋转到横屏或者竖屏时,应用程序可以重新布局用户界面,以适应屏幕方向和尺寸。
6
7系统配置的变化通常由“设置”中的选项或“控制中心”中的图标触发。订阅系统环境变量变化,可以使应用程序更加智能地响应系统环境变化,从而提供更好的用户体验。查看当前支持订阅变化的系统环境变量,请参见[Configuration](../reference/apis/js-apis-app-ability-configuration.md)。
8
9基于OpenHarmony应用模型,可以通过以下几种方式来实现订阅系统环境变量的变化。
10
11- [使用ApplicationContext订阅回调](#使用applicationcontext订阅回调)
12- [在AbilityStage组件容器中订阅回调](#在abilitystage组件容器中订阅回调)
13- [在UIAbility组件中订阅回调](#在uiability组件中订阅回调)
14- [在ExtensionAbility组件中订阅回调](#在extensionability组件中订阅回调)
15
16## 使用ApplicationContext订阅回调
17
18[ApplicationContext](../reference/apis/js-apis-inner-application-applicationContext.md)提供了注册回调函数以订阅系统环境变量的变化,并且可以通过调用相应的方法来撤销该回调。这有助于在资源不再需要时释放相关资源,从而提高系统的可靠性和性能。
19
201. 使用`ApplicationContext.on(type: 'environment', callback: EnvironmentCallback)`方法,应用程序可以通过在非应用组件模块中订阅系统环境变量的变化来动态响应这些变化。例如,使用该方法在页面中监测系统语言的变化。
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
32
33     subscribeConfigurationUpdate() {
34       let systemLanguage: string | undefined = this.context.config.language; // 获取系统当前语言
35
36       // 1.获取ApplicationContext
37       let applicationContext = this.context.getApplicationContext();
38
39       // 2.通过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; // 将变化之后的系统语言保存,作为下一次变化前的系统语言
47           }
48         },
49         onMemoryLevel(level) {
50           console.info(`onMemoryLevel level: ${level}`);
51         }
52       }
53
54       this.callbackId = applicationContext.on('environment', environmentCallback);
55     }
56
57     // 页面展示
58     build() {
59       ...
60     }
61   }
62   ```
63
642. 在资源使用完成之后,可以通过调用`ApplicationContext.off(type: 'environment', callbackId: number)`方法释放相关资源。
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
74
75     unsubscribeConfigurationUpdate() {
76       let applicationContext = this.context.getApplicationContext();
77       applicationContext.off('environment', this.callbackId);
78     }
79
80     // 页面展示
81     build() {
82       ...
83     }
84   }
85   ```
86
87## 在AbilityStage组件容器中订阅回调
88
89使用[AbilityStage.onConfigurationUpdate()](../reference/apis/js-apis-app-ability-abilityStage.md#abilitystageonconfigurationupdate)回调方法订阅系统环境变量的变化。当系统环境变量发生变化时,会调用该回调方法。在该方法中,通过[Configuration](../reference/apis/js-apis-app-ability-configuration.md)对象获取最新的系统环境配置信息。可以进行相应的界面适配等操作,从而提高系统的灵活性和可维护性。
90
91> **说明:**
92>
93> - DevEco Studio默认工程中未自动生成AbilityStage,AbilityStage文件的创建请参见[AbilityStage组件容器](abilitystage.md)。
94> - 当使用回调方法订阅系统环境变量的变化时,该回调方法会随着[AbilityStage](../reference/apis/js-apis-app-ability-abilityStage.md)的生命周期而存在,在Module销毁时一并销毁。
95
96例如,在[AbilityStage.onConfigurationUpdate()](../reference/apis/js-apis-app-ability-abilityStage.md#abilitystageonconfigurationupdate)回调方法中实现监测系统语言的变化。
97
98```ts
99import AbilityStage from '@ohos.app.ability.AbilityStage';
100import { Configuration } from '@ohos.app.ability.Configuration';
101
102let systemLanguage: string | undefined; // 系统当前语言
103
104export default class MyAbilityStage extends AbilityStage {
105  onCreate() {
106    systemLanguage = this.context.config.language; // Module首次加载时,获取系统当前语言
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; // 将变化之后的系统语言保存,作为下一次变化前的系统语言
116    }
117  }
118}
119```
120
121## 在UIAbility组件中订阅回调
122
123UIAbility组件提供了`UIAbility.onConfigurationUpdate()`回调方法用于订阅系统环境变量的变化。当系统环境变量发生变化时,会调用该回调方法。在该方法中,通过[Configuration](../reference/apis/js-apis-app-ability-configuration.md)对象获取最新的系统环境配置信息,而无需重启UIAbility。
124
125> **说明:**
126>
127> 当使用回调方法订阅系统环境变量的变化时,该回调方法会随着UIAbility的生命周期而存在,在UIAbility销毁时一并销毁。
128
129例如,在`onConfigurationUpdate()`回调方法中实现监测系统语言的变化。
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; // 系统当前语言
138
139export default class EntryAbility extends UIAbility {
140  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
141    systemLanguage = this.context.config.language; // UIAbility实例首次加载时,获取系统当前语言
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; // 将变化之后的系统语言保存,作为下一次变化前的系统语言
151    }
152  }
153
154  // ...
155}
156```
157
158## 在ExtensionAbility组件中订阅回调
159
160ExtensionAbility组件提供了`onConfigurationUpdate()`回调方法用于订阅系统环境变量的变化。当系统环境变量发生变化时,会调用该回调方法。在该方法中,通过[Configuration](../reference/apis/js-apis-app-ability-configuration.md)对象获取最新的系统环境配置信息。
161
162> **说明:**
163>
164> 当使用回调方法订阅系统环境变量的变化时,该回调方法会随着ExtensionAbility的生命周期而存在,在ExtensionAbility销毁时一并销毁。
165
166以FormExtensionAbility为例说明。例如,在`onConfigurationUpdate()`回调方法中实现系统环境变量的变化。
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