1# @ohos.application.Configuration (Configuration)
2
3定义环境变化信息。Configuration是接口定义,仅做字段声明。
4
5> **说明:**
6>
7> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> 本模块从API version 9废弃,替换模块为[@ohos.app.ability.Configuration (Configuration)](js-apis-app-ability-configuration.md)
9
10## 导入模块
11
12```ts
13import Configuration from '@ohos.application.Configuration';
14```
15
16## 属性
17
18**系统能力**:SystemCapability.Ability.AbilityBase
19
20  | 名称 | 类型 | 可读 | 可写 | 说明 |
21| -------- | -------- | -------- | -------- | -------- |
22| language<sup>8+</sup> | string | 是 | 是 | 表示应用程序的当前语言。例如:zh。 |
23| colorMode<sup>8+</sup> | [ConfigurationConstant.ColorMode](js-apis-application-configurationConstant.md#colormode) | 是 | 是 | 表示深浅色模式,取值范围:浅色模式(COLOR_MODE_LIGHT),深色模式(COLOR_MODE_DARK)。默认为浅色。 |
24
25具体字段描述参考ohos.application.Configuration.d.ts文件
26
27**示例:**
28  ```ts
29import UIAbility from '@ohos.app.ability.UIAbility';
30import AbilityConstant from '@ohos.app.ability.AbilityConstant';
31import EnvironmentCallback from '@ohos.app.ability.EnvironmentCallback';
32import Want from '@ohos.app.ability.Want';
33import Window from '@ohos.window';
34import { BusinessError } from '@ohos.base';
35
36export default class EntryAbility extends UIAbility {
37    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
38    }
39
40    onDestroy() {
41    }
42
43    onWindowStageCreate(windowStage: Window.WindowStage) {
44        let envCallback: EnvironmentCallback = {
45            onConfigurationUpdated(config) {
46                console.info(`envCallback onConfigurationUpdated success: ${JSON.stringify(config)}`);
47                let language = config.language;
48                let colorMode = config.colorMode;
49            },
50            onMemoryLevel(level){
51                console.log(`onMemoryLevel level: ${JSON.stringify(level)}`);
52            }
53        };
54
55        let applicationContext = this.context.getApplicationContext();
56        try {
57            applicationContext.on('environment',envCallback);
58        } catch (paramError) {
59            console.error(`error: ${(paramError as BusinessError).code}, ${(paramError as BusinessError).message}`);
60        }
61
62        windowStage.loadContent('pages/index', (err, data) => {
63            if (err.code) {
64                console.error(`failed to load the content, error: ${JSON.stringify(err)}`);
65                return;
66            }
67            console.info(`Succeeded in loading the content, data: ${JSON.stringify(data)}`);
68        });
69    }
70}
71  ```
72
73