harmony 鸿蒙@ohos.app.ability.contextConstant (ContextConstant)

  • 2025-06-12
  • 浏览 (4)

@ohos.app.ability.contextConstant (ContextConstant)

The ContextConstant module defines context-related enums. Currently, it defines only the enum of encryption levels.

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.

The APIs of this module can be used only in the stage model.

Modules to Import

import { contextConstant } from '@kit.AbilityKit';

AreaMode

Enumerates the data encryption levels.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Name Value Description
EL1 0 Device-level encryption. Directories with this encryption level are accessible after the device is powered on.
Atomic service API: This API can be used in atomic services since API version 11.
EL2 1 User-level encryption. Directories with this encryption level are accessible only after the device is powered on and the password is entered (for the first time).
Atomic service API: This API can be used in atomic services since API version 11.
EL311+ 2 User-level encryption. The file permissions vary according to their scenarios.
- An open file is always readable and writable regardless of whether the screen is locked.
- When the screen is locked, a closed file cannot be opened, read, or written. When the screen is unlocked, such a file can be opened, read, and written.
- When the screen is locked, a file can be created and then opened and written but not read. When the screen is unlocked, a file can be created and then opened, read, and written.
Atomic service API: This API can be used in atomic services since API version 11.
EL411+ 3 User-level encryption. The file permissions vary according to their scenarios.
- When the screen is locked, an open file is not readable or writable. When the screen is unlocked, such a file is readable and writable.
- When the screen is locked, a closed file cannot be opened, read, or written. When the screen is unlocked, such a file can be opened, read, and written.
- When the screen is locked, a file cannot be created. When the screen is unlocked, a file can be created and then opened, read, and written.
Atomic service API: This API can be used in atomic services since API version 11.
EL512+ 4 Application-level encryption. The file permissions vary according to their scenarios.
- An open file is always readable and writable regardless of whether the screen is locked.
When the screen is locked, a closed file can be opened, read, and written only if the reserved key is obtained by calling Access. When the screen is unlocked, such a file can be opened, read, and written.
A file can be created and then opened, read, and written regardless of whether the screen is locked.
Atomic service API: This API can be used in atomic services since API version 12.

ProcessMode12+

Enumerates the process modes. It takes effect only on 2-in-1 devices and tablets.

As a property of StartOptions, ProcessMode takes effect only in UIAbilityContext.startAbility and is used to specify the process mode of the target ability.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Name Value Description
NEW_PROCESS_ATTACH_TO_PARENT 1 A new process is created, the ability is started on the process, and the process exits along with the parent process.
Constraints:
In this mode, the target ability and caller must be in the same application.
NEW_PROCESS_ATTACH_TO_STATUS_BAR_ITEM 2 A new process is created, the ability is started on the process, and the process is bound to the status bar icon.
Constraints:
In this mode, the target ability and caller must be in the same application, and the application must have an icon in the status bar.
ATTACH_TO_STATUS_BAR_ITEM 3 The ability is started, and the process of the ability is bound to the status bar icon.
Constraints:
In this mode, the target ability and caller must be in the same application, and the application must have an icon in the status bar.

Example

  import { UIAbility, Want, StartOptions, contextConstant } from '@kit.AbilityKit';
  import { BusinessError } from '@kit.BasicServicesKit';

  export default class EntryAbility extends UIAbility {
    onForeground() {
      let want: Want = {
        deviceId: '',
        bundleName: 'com.example.myapplication',
        abilityName: 'MainAbility2'
      };
      let options: StartOptions = {
        processMode: contextConstant.ProcessMode.NEW_PROCESS_ATTACH_TO_STATUS_BAR_ITEM,
        startupVisibility: contextConstant.StartupVisibility.STARTUP_HIDE
      };

      try {
        this.context.startAbility(want, options, (err: BusinessError) => {
          if (err.code) {
            // Process service logic errors.
            console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`);
            return;
          }
          // Carry out normal service processing.
          console.info('startAbility succeed');
        });
      } catch (err) {
        // Process input parameter errors.
        let code = (err as BusinessError).code;
        let message = (err as BusinessError).message;
        console.error(`startAbility failed, code is ${code}, message is ${message}`);
      }
    }
  }

StartupVisibility12+

Enumerates the visibility statuses of an ability after it is started. It takes effect only on 2-in-1 devices and tablets.

As a property of StartOptions, StartupVisibility takes effect only in UIAbilityContext.startAbility and specifies the visibility of the target ability after it is started.

System capability: SystemCapability.Ability.AbilityRuntime.Core

Name Value Description
STARTUP_HIDE 0 The target ability is hidden after it is started in the new process. The onForeground lifecycle of the ability is not invoked.
STARTUP_SHOW 1 The target ability is displayed normally after it is started in the new process.

Example

See ContextConstant.ProcessMode.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit

harmony 鸿蒙AbilityAccessControl

harmony 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_access_control.h

harmony 鸿蒙ability_base_common.h

0  赞