• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.app.ability.Configuration (Configuration)
2
3The **Configuration** module defines environment change information. **Configuration** is an interface definition and is used only for field declaration.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import Configuration from '@ohos.app.ability.Configuration';
13```
14
15## Attributes
16
17**System capability**: SystemCapability.Ability.AbilityBase
18
19| Name| Type| Readable| Writable| Description|
20| -------- | -------- | -------- | -------- | -------- |
21| language | string | Yes| Yes| Language of the application, for example, **zh**.|
22| colorMode | [ColorMode](js-apis-app-ability-configurationConstant.md#configurationconstantcolormode) | Yes| Yes| Color mode. The default value is **COLOR_MODE_LIGHT**. The options are as follows:<br>- **COLOR_MODE_NOT_SET**: The color mode is not set.<br>- **COLOR_MODE_LIGHT**: light mode.<br>- **COLOR_MODE_DARK**: dark mode.|
23| direction | [Direction](js-apis-app-ability-configurationConstant.md#configurationconstantdirection) | Yes| No| Screen orientation. The options are as follows:<br>- **DIRECTION_NOT_SET**: The screen orientation is not set.<br>- **DIRECTION_HORIZONTAL**: horizontal direction.<br>- **DIRECTION_VERTICAL**: vertical direction.|
24| screenDensity  | [ScreenDensity](js-apis-app-ability-configurationConstant.md#configurationconstantscreendensity) | Yes| No| Pixel density of the screen. The options are as follows:<br>- **SCREEN_DENSITY_NOT_SET**: The pixel density is not set.<br>- **SCREEN_DENSITY_SDPI**: 120.<br>- **SCREEN_DENSITY_MDPI**: 160.<br>- **SCREEN_DENSITY_LDPI**: 240.<br>- **SCREEN_DENSITY_XLDPI**: 320.<br>- **SCREEN_DENSITY_XXLDPI**: 480.<br>- **SCREEN_DENSITY_XXXLDPI**: 640.|
25| displayId  | number | Yes| No| ID of the display where the application is located.|
26| hasPointerDevice  | boolean | Yes| No| Whether a pointer device, such as a keyboard, mouse, or touchpad, is connected.|
27
28For details about the fields, see the **ohos.app.ability.Configuration.d.ts** file.
29
30**Example**
31
32  ```ts
33import UIAbility from '@ohos.app.ability.UIAbility';
34import AbilityConstant from '@ohos.app.ability.AbilityConstant';
35import EnvironmentCallback from '@ohos.app.ability.EnvironmentCallback';
36import Want from '@ohos.app.ability.Want';
37
38export default class EntryAbility extends UIAbility {
39    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
40        let envCallback: EnvironmentCallback = {
41            onConfigurationUpdated(config) {
42                console.info(`envCallback onConfigurationUpdated success: ${JSON.stringify(config)}`);
43                let language = config.language;
44                let colorMode = config.colorMode;
45                let direction = config.direction;
46                let screenDensity = config.screenDensity;
47                let displayId = config.displayId;
48                let hasPointerDevice = config.hasPointerDevice;
49            }
50            onMemoryLevel(level){
51                console.log('onMemoryLevel level: ${level}');
52            }
53        };
54        try {
55            let applicationContext = this.context.getApplicationContext();
56            let callbackId = applicationContext.on('environment', envCallback);
57            console.log('callbackId: ${callbackId}');
58        } catch (paramError) {
59            console.error(`error: ${paramError.code}, ${paramError.message}`);
60        }
61    }
62}
63  ```
64
65