鸿蒙原生应用/元服务开发-Stage模型能力接口(二)
@ohos.app.ability.AbilityConstant (AbilityConstant)
一、说明
AbilityConstant提供Ability相关的枚举,包括设置初次启动原因、上次退出原因、迁移结果、窗口类型等。本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。本模块接口仅可在Stage模型下使用。
二、导入模块
import AbilityConstant from ‘@ohos.app.ability.AbilityConstant’;
三、属性
1.AbilityConstant.LaunchParam
启动参数。
系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
2.AbilityConstant.LaunchReason
Ability初次启动原因,该类型为枚举,可配合Ability的onCreate(want, launchParam)方法根据launchParam.launchReason的不同类型执行相应操作。
系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
示例:
import UIAbility from ‘@ohos.app.ability.UIAbility’;
class MyAbility extends UIAbility {
onCreate(want, launchParam) {
if (launchParam.launchReason === AbilityConstant.LaunchReason.START_ABILITY) {
console.log(‘The ability has been started by the way of startAbility.’);
}
}
}
3.AbilityConstant.LastExitReason
Ability上次退出原因,该类型为枚举,可配合Ability的onCreate(want, launchParam)方法根据launchParam.lastExitReason的不同类型执行相应操作。
系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
示例:
import UIAbility from ‘@ohos.app.ability.UIAbility’;
class MyAbility extends UIAbility {
onCreate(want, launchParam) {
if (launchParam.lastExitReason === AbilityConstant.LastExitReason.ABILITY_NOT_RESPONDING) {
console.log(‘The ability has exit last because the ability was not responding.’);
}
}
4.AbilityConstant.OnContinueResult
Ability迁移结果,该类型为枚举,可配合Ability的onContinue(wantParam)方法进完成相应的返回。
系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
示例:
import UIAbility from ‘@ohos.app.ability.UIAbility’;
class MyAbility extends UIAbility {
onContinue(wantParam) {
return AbilityConstant.OnContinueResult.AGREE;
}
}
5.AbilityConstant.MemoryLevel
内存级别,该类型为枚举,可配合Ability的onMemoryLevel(level)方法根据level执行不同内存级别的相应操作。
系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
示例:
import UIAbility from ‘@ohos.app.ability.UIAbility’;
class MyAbility extends UIAbility {
onContinue(wantParam) {
return AbilityConstant.OnContinueResult.AGREE;
}
}
6.AbilityConstant.MemoryLevel
内存级别,该类型为枚举,可配合Ability的onMemoryLevel(level)方法根据level执行不同内存级别的相应操作。
系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
示例:
import UIAbility from ‘@ohos.app.ability.UIAbility’;
class MyAbility extends UIAbility {
onSaveState(reason, wantParam) {
return AbilityConstant.OnSaveResult.ALL_AGREE;
}
}
7.AbilityConstant.StateType
保存应用数据场景原因,该类型为枚举,可配合Ability的onSaveState(reason, wantParam)方法根据reason的不同类型执行相应操作。
系统能力:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
示例:
import UIAbility from ‘@ohos.app.ability.UIAbility’;
class MyAbility extends UIAbility {
onSaveState(reason, wantParam) {
if (reason === AbilityConstant.StateType.CONTINUATION) {
console.log(‘Save the ability data when the ability continuation.’);
}
return AbilityConstant.OnSaveResult.ALL_AGREE;
}
}
本文根据HarmonyOS官方开发文档整理