harmony 鸿蒙@ohos.app.ability.Configuration (Configuration)

  • 2022-12-22
  • 浏览 (668)

@ohos.app.ability.Configuration (Configuration)

The Configuration module defines environment change information. Configuration is an interface definition and is used only for field declaration.

NOTE

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.

Modules to Import

import Configuration from '@ohos.app.ability.Configuration';

Attributes

System capability: SystemCapability.Ability.AbilityBase

Name Type Readable Writable Description
language string Yes Yes Language of the application, for example, zh.
colorMode ColorMode Yes Yes Color mode. The default value is COLOR_MODE_LIGHT. The options are as follows:
- COLOR_MODE_NOT_SET: The color mode is not set.
- COLOR_MODE_LIGHT: light mode.
- COLOR_MODE_DARK: dark mode.
direction Direction Yes No Screen orientation. The options are as follows:
- DIRECTION_NOT_SET: The screen orientation is not set.
- DIRECTION_HORIZONTAL: horizontal direction.
- DIRECTION_VERTICAL: vertical direction.
screenDensity ScreenDensity Yes No Pixel density of the screen. The options are as follows:
- SCREEN_DENSITY_NOT_SET: The pixel density is not set.
- SCREEN_DENSITY_SDPI: 120.
- SCREEN_DENSITY_MDPI: 160.
- SCREEN_DENSITY_LDPI: 240.
- SCREEN_DENSITY_XLDPI: 320.
- SCREEN_DENSITY_XXLDPI: 480.
- SCREEN_DENSITY_XXXLDPI: 640.
displayId number Yes No ID of the display where the application is located.
hasPointerDevice boolean Yes No Whether a pointer device, such as a keyboard, mouse, or touchpad, is connected.

For details about the fields, see the ohos.app.ability.Configuration.d.ts file.

Example

import UIAbility from '@ohos.app.ability.UIAbility';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import EnvironmentCallback from '@ohos.app.ability.EnvironmentCallback';
import Want from '@ohos.app.ability.Want';

export default class EntryAbility extends UIAbility {
    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
        let envCallback: EnvironmentCallback = {
            onConfigurationUpdated(config) {
                console.info(`envCallback onConfigurationUpdated success: ${JSON.stringify(config)}`);
                let language = config.language;
                let colorMode = config.colorMode;
                let direction = config.direction;
                let screenDensity = config.screenDensity;
                let displayId = config.displayId;
                let hasPointerDevice = config.hasPointerDevice;
            }
            onMemoryLevel(level){
                console.log('onMemoryLevel level: ${level}');
            }
        };
        try {
            let applicationContext = this.context.getApplicationContext();
            let callbackId = applicationContext.on('environment', envCallback);
            console.log('callbackId: ${callbackId}');
        } catch (paramError) {
            console.error(`error: ${paramError.code}, ${paramError.message}`);
        }
    }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

0  赞