harmony 鸿蒙@ohos.app.ability.application (应用基础能力)
@ohos.app.ability.application (应用基础能力)
开发者可以通过该模块创建Context。
说明:
本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 本模块接口仅可在Stage模型下使用。
导入模块
import { application } from '@kit.AbilityKit';
application.createModuleContext12+
createModuleContext(context: Context, moduleName: string): Promise<Context>
根据入参Context创建相应模块的Context。
原子化服务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 { 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.createModuleContext(this.context, 'entry').then((data: Context) => {
moduleContext = data;
console.info('createBundleContext 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
获取应用程序上下文。 > 说明: > >通过该接口取得的ApplicationContext,只支持获取对应的应用信息和全部的沙箱路径。
原子化服务API:从API version 14开始,该接口支持在元服务中使用。
系统能力:SystemCapability.Ability.AbilityRuntime.Core
返回值:
类型 | 说明 |
---|---|
ApplicationContext | 应用上下文Context。 |
错误码:
以下错误码详细介绍请参考元能力子系统错误码。
错误码ID | 错误信息 |
---|---|
16000050 | Internal error. |
示例:
import { AbilityConstant, UIAbility, application, Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): 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}`);
}
}
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙OH_NativeBundle_ApplicationInfo
harmony 鸿蒙OH_NativeBundle_ElementName
harmony 鸿蒙ability_base_common.h
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦