@ohos.app.ability.application (应用工具类)
开发者可以通过该模块管理和获取应用的上下文Context,以及控制应用进程的状态。
说明:
本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 本模块接口仅可在Stage模型下使用。
导入模块
import { application } from '@kit.AbilityKit';
application.createModuleContext12+
createModuleContext(context: Context, moduleName: string): Promise<Context>
创建指定模块的上下文。创建出的模块上下文中resourceManager.Configuration资源继承自入参上下文,便于开发者获取跨HAP/HSP包应用资源。使用Promise异步回调。
原子化服务API:从API version 12开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| context | Context | 是 | 表示应用上下文。 |
| moduleName | string | 是 | 表示应用模块名。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<Context> | Promise对象。返回创建的Context。 |
错误码:
以下错误码详细介绍请参考通用错误码说明文档。
| 错误码ID | 错误信息 |
|---|---|
| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
示例:
import { UIAbility, application, common } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate() {
let moduleContext: common.Context;
try {
application.createModuleContext(this.context, 'entry').then((data: Context) => {
moduleContext = data;
console.info('createModuleContext success!');
}).catch((error: BusinessError) => {
let code: number = (error as BusinessError).code;
let message: string = (error as BusinessError).message;
console.error(`createModuleContext failed, error.code: ${code}, error.message: ${message}`);
})
} catch (error) {
let code: number = (error as BusinessError).code;
let message: string = (error as BusinessError).message;
console.error(`createModuleContext failed, error.code: ${code}, error.message: ${message}`);
}
}
}
application.getApplicationContext14+
getApplicationContext(): ApplicationContext
获取应用上下文。开发者使用该接口时,无需依赖Context基类。重复调用会生成新的ApplicationContext对象。
原子化服务API:从API version 14开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
返回值:
| 类型 | 说明 |
|---|---|
| ApplicationContext | 应用上下文。 |
错误码:
以下错误码详细介绍请参考元能力子系统错误码。
| 错误码ID | 错误信息 |
|---|---|
| 16000050 | Internal error. |
示例:
import { UIAbility, application } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate(): void {
try {
let applicationContext = application.getApplicationContext();
} catch (error) {
let code: number = (error as BusinessError).code;
let message: string = (error as BusinessError).message;
console.error(`getApplicationContext failed, error.code: ${code}, error.message: ${message}`);
}
}
}
application.createPluginModuleContext19+
createPluginModuleContext(context: Context, pluginBundleName: string, pluginModuleName: string): Promise<Context>
根据入参Context、指定的插件包名和插件模块名,创建本应用下插件的Context,用于获取插件的基本信息。使用Promise异步回调。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| context | Context | 是 | 表示应用上下文。 |
| pluginBundleName | string | 是 | 表示应用的插件包名。 |
| pluginModuleName | string | 是 | 表示应用的插件模块名。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<Context> | Promise对象。返回创建的Context。 |
示例:
import { AbilityConstant, UIAbility, application, common, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
let moduleContext: common.Context;
try {
application.createPluginModuleContext(this.context, 'pluginBundleName', 'pluginModuleName')
.then((data: Context) => {
moduleContext = data;
console.info('createPluginModuleContext success!');
})
.catch((error: BusinessError) => {
let code: number = (error as BusinessError).code;
let message: string = (error as BusinessError).message;
console.error(`createPluginModuleContext failed, error.code: ${code}, error.message: ${message}`);
});
} catch (error) {
let code: number = (error as BusinessError).code;
let message: string = (error as BusinessError).message;
console.error(`createPluginModuleContext failed, error.code: ${code}, error.message: ${message}`);
}
}
}