openharmony 鸿蒙 js-apis-app-ability-abilityConstant

2025-06-12 浏览 (1)

@ohos.app.ability.AbilityConstant (AbilityConstant)

The AbilityConstant module defines the UIAbility-related enums, including the initial launch reasons, reasons for the last exit, ability continuation results, and window modes.

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 { AbilityConstant } from '@kit.AbilityKit';

LaunchParam

Defines the parameters for starting an ability. The parameter values are automatically passed in by the system when the ability is started. You do not need to change the values.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameTypeRead-onlyOptionalDescription
launchReasonLaunchReasonNoNoAbility launch reason, which is an enumerated type.
Atomic service API: This API can be used in atomic services since API version 11.
launchReasonMessage18+stringNoYesDetailed message that describes the ability launch reason.
Atomic service API: This API can be used in atomic services since API version 18.
lastExitReasonLastExitReasonNoNoReason for the last exit, which is an enumerated type.
Atomic service API: This API can be used in atomic services since API version 11.
lastExitMessage12+stringNoNoReason for the last exit.
Atomic service API: This API can be used in atomic services since API version 12.
lastExitDetailInfo18+LastExitDetailInfoNoYesDetailed information about the last exit.
Atomic service API: This API can be used in atomic services since API version 18.

LaunchReason

Enumerates the initial ability launch reasons. You can use it together with the value of launchParam.launchReason in onCreate(want, launchParam) of the UIAbility to complete different operations.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
UNKNOWN0Unknown reason.
Atomic service API: This API can be used in atomic services since API version 11.
START_ABILITY1The ability is started by calling startAbility.
Atomic service API: This API can be used in atomic services since API version 11.
CALL2The ability is started by calling startAbilityByCall.
Atomic service API: This API can be used in atomic services since API version 11.
CONTINUATION3The ability is started by means of cross-device migration.
Atomic service API: This API can be used in atomic services since API version 11.
APP_RECOVERY4The ability is automatically started when the application is restored from a fault.
Atomic service API: This API can be used in atomic services since API version 11.
SHARE10+5The ability is started by means of atomic service sharing.
Atomic service API: This API can be used in atomic services since API version 11.
AUTO_STARTUP11+8The ability is automatically started upon system boot.
INSIGHT_INTENT11+9The ability is started by the InsightIntent framework.
Atomic service API: This API can be used in atomic services since API version 11.
PREPARE_CONTINUATION12+10The ability is started in advance during cross-device migration.
Atomic service API: This API can be used in atomic services since API version 12.

Example

import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';

class MyAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    if (launchParam.launchReason === AbilityConstant.LaunchReason.START_ABILITY) {
      console.log('The ability has been started by the way of startAbility.');
    }
  }
}

LastExitReason

Enumerates the reasons for the last exit. You can use it together with the value of launchParam.lastExitReason in onCreate(want, launchParam) of the UIAbility to complete different operations.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
UNKNOWN0Unknown reason.
Atomic service API: This API can be used in atomic services since API version 11.
ABILITY_NOT_RESPONDING(deprecated)1The ability does not respond.
NOTE
This enum is supported since API version 9 and deprecated since API version 10. You are advised to use APP_FREEZE.
NORMAL2The ability exits normally because the user closes the application.
Atomic service API: This API can be used in atomic services since API version 11.
NOTE: If the application process is terminated using methods not provided by Ability Kit, such as calling process.exit() or using the kill command, the reason for the last exit is also reported as NORMAL.
CPP_CRASH10+3The ability exits due to abnormal signals on the local host.
Atomic service API: This API can be used in atomic services since API version 11.
JS_ERROR10+4The ability exits due to a JS_ERROR fault triggered when an application has a JS syntax error that is not captured by developers.
Atomic service API: This API can be used in atomic services since API version 11.
APP_FREEZE10+5The ability exits because watchdog detects that the application is frozen.
Atomic service API: This API can be used in atomic services since API version 11.
PERFORMANCE_CONTROL10+6The ability exits due to system performance problems, for example, insufficient device memory.
Atomic service API: This API can be used in atomic services since API version 11.
NOTE: This API will be deprecated. You are advised to use RESOURCE_CONTROL instead.
RESOURCE_CONTROL10+7The ability exits due to improper use of system resources. The specific error cause can be obtained through LaunchParam.lastExitMessage. The possible causes are as follows:
- CPU Highload: The CPU load is high.
- CPU_EXT Highload: A fast CPU load detection is carried out.
- IO Manage Control: An I/O management and control operation is carried out.
- App Memory Deterioration: The application memory usage exceeds the threshold.
- Temperature Control: The temperature is too high or too low.
- Memory Pressure: The system is low on memory, triggering ability exiting in ascending order of priority.
Atomic service API: This API can be used in atomic services since API version 11.
UPGRADE10+8The ability exits due to an update.
Atomic service API: This API can be used in atomic services since API version 11.
USER_REQUEST18+9The ability exits because of an action in the multitasking center, for example, when users swipe up or hit the one-click clean button in the multitasking view.
Atomic service API: This API can be used in atomic services since API version 18.
SIGNAL18+10The ability exits because it receives a kill signal from the system.
Atomic service API: This API can be used in atomic services since API version 18.

Example

import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';

class MyAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    if (launchParam.lastExitReason === AbilityConstant.LastExitReason.APP_FREEZE) {
      console.log('The ability has exit last because the ability was not responding.');
    }
    if (launchParam.lastExitReason === AbilityConstant.LastExitReason.RESOURCE_CONTROL) {
      console.log('The ability has exit last because the rss control, the lastExitReason is '+ launchParam.lastExitReason + ', the lastExitMessage is ' + launchParam.lastExitMessage);
    }
  }
}

LastExitDetailInfo18+

Describes the detailed information about the last exit.

Atomic service API: This API can be used in atomic services since API version 18.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameTypeRead-onlyOptionalDescription
pidnumberNoNoID of the process where the ability is running when it exits last time.
processNamestringNoNoName of the process.
uidnumberNoNoUID of the application.
exitSubReasonnumberNoNoSpecific reason for the last exit of the ability.
exitMsgstringNoNoReason why the process was killed.
rssnumberNoNoRSS value of the process.
pssnumberNoNoPSS value of the process.
timestampnumberNoNoExact time when the ability last exits.

Example

import { UIAbility, Want, AbilityConstant } from '@kit.AbilityKit';

class MyAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    if (launchParam.lastExitDetailInfo) {
      console.log('pid: ' + launchParam.lastExitDetailInfo.pid +
        '\n processName: ' + launchParam.lastExitDetailInfo.processName +
        '\n uid: ' + launchParam.lastExitDetailInfo.uid +
        '\n exitSubReason: ' + launchParam.lastExitDetailInfo.exitSubReason +
        '\n exitMsg: ' + launchParam.lastExitDetailInfo.exitMsg +
        '\n rss: ' + launchParam.lastExitDetailInfo.rss +
        '\n pss: ' + launchParam.lastExitDetailInfo.pss +
        '\n timestamp: ' + launchParam.lastExitDetailInfo.timestamp
      );
    }
  }
}

OnContinueResult

Enumerates the ability continuation results. You can use it together with onContinue(wantParam) of the UIAbility to complete different operations.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
AGREE0The ability continuation is accepted.
REJECT1The ability continuation is rejected. If the application is abnormal in onContinue, which results in abnormal display during data restoration, this error code is returned.
MISMATCH2The version does not match. The application on the initiator can obtain the version of the target application from onContinue. If the ability continuation cannot be performed due to version mismatch, this error code is returned.

Example

import { UIAbility, AbilityConstant } from '@kit.AbilityKit';

class MyAbility extends UIAbility {
  onContinue(wantParam: Record<string, Object>) {
    return AbilityConstant.OnContinueResult.AGREE;
  }
}

MemoryLevel

Enumerates the memory levels. You can use it in onMemoryLevel(level) of the UIAbility to complete different operations.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
MEMORY_LEVEL_MODERATE0Moderate memory usage.
MEMORY_LEVEL_LOW1Low memory usage.
MEMORY_LEVEL_CRITICAL2High memory usage.

NOTE

The trigger conditions may differ across various devices. For example, on a standard device with 12 GB of memory:

  • A callback with value 0 is triggered when available memory drops between 1700 MB and 1800 MB.
  • A callback with value 1 is triggered when available memory drops between 1600 MB and 1700 MB.
  • A callback with value 2 is triggered when available memory falls below 1600 MB.

Example

import { UIAbility, AbilityConstant } from '@kit.AbilityKit';

class MyAbility extends UIAbility {
  onMemoryLevel(level: AbilityConstant.MemoryLevel) {
    if (level === AbilityConstant.MemoryLevel.MEMORY_LEVEL_CRITICAL) {
      console.log('The memory of device is critical, please release some memory.');
    }
  }
}

WindowMode12+

Enumerates the window mode when the ability is started. It can be used together with startAbility to specify the window mode for starting the ability.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
WINDOW_MODE_FULLSCREEN1Full screen mode. It takes effect only on 2-in-1 devices and tablets.
WINDOW_MODE_SPLIT_PRIMARY100Primary screen (left screen in the case of horizontal orientation) in split-screen mode. It is valid only in intra-app redirection scenarios. It takes effect only on foldable devices and tablets.
WINDOW_MODE_SPLIT_SECONDARY101Secondary screen (right screen in the case of horizontal orientation) in split-screen mode. It is valid only in intra-app redirection scenarios. It takes effect only on foldable devices and tablets.

Example

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

let want: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility'
};
let option: StartOptions = {
  windowMode: AbilityConstant.WindowMode.WINDOW_MODE_SPLIT_PRIMARY
};

// Ensure that the context is obtained.
class MyAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    this.context.startAbility(want, option).then(() => {
      console.log('Succeed to start ability.');
    }).catch((error: BusinessError) => {
      console.error(`Failed to start ability with error: ${JSON.stringify(error)}`);
    });
  }
}

OnSaveResult

Enumerates the result types for the operation of saving application data. You can use it in onSaveState(reason, wantParam) of the UIAbility to complete different operations.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
ALL_AGREE0Always agreed to save the status.
CONTINUATION_REJECT1Rejected to save the status in continuation.
CONTINUATION_MISMATCH2Continuation mismatch.
RECOVERY_AGREE3Agreed to restore the saved status.
RECOVERY_REJECT4Rejected to restore the saved status.
ALL_REJECT5Always rejected to save the status.

Example

import { UIAbility, AbilityConstant } from '@kit.AbilityKit';

class MyAbility extends UIAbility {
  onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
    return AbilityConstant.OnSaveResult.ALL_AGREE;
  }
}

StateType

Enumerates the scenarios for saving application data. You can use it in onSaveState(reason, wantParam) of the UIAbility to complete different operations.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
CONTINUATION0Saving the status in continuation.
APP_RECOVERY1Saving the status in application recovery.

Example

import { UIAbility, AbilityConstant } from '@kit.AbilityKit';

class MyAbility extends UIAbility {
  onSaveState(reason: AbilityConstant.StateType, wantParam: Record<string, Object>) {
    if (reason === AbilityConstant.StateType.CONTINUATION) {
      console.log('Save the ability data when the ability continuation.');
    }
    return AbilityConstant.OnSaveResult.ALL_AGREE;
  }
}

ContinueState10+

Enumerates the mission continuation states of the application. It is used in the setMissionContinueState API of UIAbilityContext.

Atomic service API: This API can be used in atomic services since API version 11.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
ACTIVE0Mission continuation is activated for the current application.
INACTIVE1Mission continuation is not activated for the current application.

Example

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

class MyAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
    this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result: BusinessError) => {
      console.info(`setMissionContinueState: ${JSON.stringify(result)}`);
    });
  }
}

CollaborateResult18+

Enumerates the collaboration request results. This enum is used in multi-device collaboration scenarios to specify whether the target application accepts the collaboration request from the caller application. It is used in onCollaborate(wantParam) of the UIAbility.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
ACCEPT0Accepts the collaboration request.
REJECT1Rejects the collaboration request.

Example

import { UIAbility, AbilityConstant } from '@kit.AbilityKit';

class MyAbility extends UIAbility {
  onCollaborate(wantParam: Record<string, Object>) {
    return AbilityConstant.CollaborateResult.ACCEPT;
  }
}

PrepareTermination15+

Enumerates the actions triggered when an application is closed by the user. It must be used together with onPrepareTermination or onPrepareTerminationAsync of AbilityStage.

Atomic service API: This API can be used in atomic services since API version 15.

System capability: SystemCapability.Ability.AbilityRuntime.Core

NameValueDescription
TERMINATE_IMMEDIATELY0Executes the termination action immediately. This is the default behavior.
CANCEL1Cancels the termination action.

Example

import { AbilityConstant, AbilityStage } from '@kit.AbilityKit';

class MyAbilityStage extends AbilityStage {
  onPrepareTermination(): AbilityConstant.PrepareTermination {
    console.info('MyAbilityStage.onPrepareTermination is called');
    return AbilityConstant.PrepareTermination.CANCEL;
  }
}

你可能感兴趣的鸿蒙文章

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

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/TZtH5y