1# @ohos.app.ability.application (Application)(系统接口) 2开发者可以通过该模块创建[Context](../../application-models/application-context-stage.md)。 3 4> **说明:** 5> 6> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 7> 本模块接口仅可在Stage模型下使用。 8 9## 导入模块 10 11```ts 12import { application } from '@kit.AbilityKit'; 13``` 14## application.createModuleContext<sup>12+</sup> 15 16createModuleContext(context: Context, bundleName: string, moduleName: string): Promise\<Context> 17 18根据入参Context创建相应模块的Context。 19 20> **说明:** 21> 22> - 从API version 18开始,Context支持获取当前应用的进程名[processName](js-apis-inner-application-context.md#context)。createModuleContext创建的Context中的processName属性与入参Context中的processName属性一致,其他属性根据入参Context、bundleName和moduleName获得相应的属性值。 23> 24> - 该接口需要多次查询包信息、加载指定Module的全部资源,耗时较长。建议首次使用该接口时缓存创建的Context,以避免重复调用该接口创建Context。 25 26**元服务API:** 从API version 12开始,该接口支持在元服务中使用。 27 28**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 29 30**系统接口**:此接口为系统接口。 31 32**参数**: 33 34| 参数名 | 类型 | 必填 | 说明 | 35| --------- | ---------------------------------------- | ---- | -------------- | 36| context | [Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md) | 是 | 表示应用上下文。 | 37| bundleName | string | 是 | 表示应用包名。取值为空字符串时,默认为当前应用。| 38| moduleName | string | 是 | 表示应用模块名。 | 39 40**返回值:** 41 42| 类型 | 说明 | 43| ------------------ | ------------------- | 44| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise对象。返回创建的Context。 | 45 46**错误码:** 47 48以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 49 50| 错误码ID | 错误信息 | 51| -------- | --------------- | 52| 201 | Permission denied. | 53| 202 | Permission denied, non-system app called system api.| 54| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 55 56**示例:** 57 58```ts 59import { UIAbility, application, common } from '@kit.AbilityKit'; 60import { BusinessError } from '@kit.BasicServicesKit'; 61 62export default class EntryAbility extends UIAbility { 63 onCreate() { 64 let moduleContext: common.Context; 65 try { 66 application.createModuleContext(this.context, 'bundlename', 'entry').then((data: Context)=>{ 67 moduleContext = data; 68 console.info('createModuleContext success!'); 69 }).catch((error : BusinessError)=>{ 70 console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 71 }) 72 } catch (error) { 73 console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 74 } 75 } 76} 77``` 78 79## application.createBundleContext<sup>12+</sup> 80 81createBundleContext(context: Context, bundleName: string): Promise\<Context> 82 83根据入参Context创建相应应用的Context。 84 85> **说明:** 86> 87> 从API version 18开始,Context支持获取当前应用的进程名[processName](js-apis-inner-application-context.md#context)。createBundleContext创建的Context中的processName属性与入参Context中的processName属性一致,其他属性根据入参Context、bundleName和moduleName获得相应的属性值。 88 89**元服务API:** 从API version 12开始,该接口支持在元服务中使用。 90 91**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 92 93**系统接口**:此接口为系统接口。 94 95**参数**: 96 97| 参数名 | 类型 | 必填 | 说明 | 98| --------- | ---------------------------------------- | ---- | -------------- | 99| context | [Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md) | 是 | 表示应用上下文。 | 100| bundleName | string | 是 | 表示应用包名。 | 101 102**返回值:** 103 104| 类型 | 说明 | 105| ------------------ | ------------------- | 106| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise对象。返回创建的Context。 | 107 108**错误码:** 109 110以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 111 112| 错误码ID | 错误信息 | 113| -------- | --------------- | 114| 201 | Permission denied. | 115| 202 | Permission denied, non-system app called system api.| 116| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 117 118 119**示例:** 120 121```ts 122import { UIAbility, application, common } from '@kit.AbilityKit'; 123import { BusinessError } from '@kit.BasicServicesKit'; 124 125export default class EntryAbility extends UIAbility { 126 onCreate() { 127 let moduleContext: common.Context; 128 try { 129 application.createBundleContext(this.context, 'bundlename').then((data: Context)=>{ 130 moduleContext = data; 131 console.info('createBundleContext success!'); 132 }).catch((error : BusinessError)=>{ 133 console.error(`createBundleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 134 }) 135 } catch (error) { 136 console.error(`createBundleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 137 } 138 } 139} 140 141``` 142## application.createPluginModuleContextForHostBundle<sup>20+</sup> 143 144createPluginModuleContextForHostBundle(context: Context, pluginBundleName: string, pluginModuleName: string, hostBundleName: string): Promise\<Context> 145 146根据入参Context、插件包名和插件模块名和应用包名,创建对应插件的Context,用于获取插件的基本信息。使用Promise异步回调。 147 148**需要权限**:ohos.permission.GET_BUNDLE_INFO_PRIVILEGED 149 150**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 151 152**系统接口:** 此接口为系统接口。 153 154**参数**: 155 156| 参数名 | 类型 | 必填 | 说明 | 157| --------- | ---------------------------------------- | ---- | -------------- | 158| context | [Context](js-apis-inner-application-context.md) | 是 | 表示应用上下文。 | 159| pluginBundleName | string | 是 | 表示应用的插件包名。 | 160| pluginModuleName | string | 是 | 表示应用的插件模块名。 | 161| hostBundleName | string | 是 | 表示安装插件的应用包名。 | 162 163**返回值:** 164 165| 类型 | 说明 | 166| ------------------ | ------------------- | 167| Promise\<[Context](js-apis-inner-application-context.md)> | Promise对象。返回创建的Context,返回的Context中的属性processName和config与入参Context中的属性processName和config的值相同。 | 168 169**错误码**: 170 171以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 172 173| 错误码ID | 错误信息 | 174| ------- | -------- | 175| 201 | Permission denied. | 176| 202 | Permission denied, non-system app called system api. | 177 178**示例:** 179 180```ts 181import { AbilityConstant, UIAbility, application, common, Want } from '@kit.AbilityKit'; 182import { BusinessError } from '@kit.BasicServicesKit'; 183 184export default class EntryAbility extends UIAbility { 185 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 186 let moduleContext: common.Context; 187 try { 188 application.createPluginModuleContextForHostBundle(this.context, 'com.example.pluginBundleName', 'pluginModuleName', 'com.example.hostBundleName') 189 .then((data: Context) => { 190 moduleContext = data; 191 console.info('createPluginModuleContextForHostBundle success!'); 192 }) 193 .catch((error: BusinessError) => { 194 let code: number = (error as BusinessError).code; 195 let message: string = (error as BusinessError).message; 196 console.error(`createPluginModuleContextForHostBundle failed, error.code: ${code}, error.message: ${message}`); 197 }); 198 } catch (error) { 199 let code: number = (error as BusinessError).code; 200 let message: string = (error as BusinessError).message; 201 console.error(`createPluginModuleContextForHostBundle failed, error.code: ${code}, error.message: ${message}`); 202 } 203 } 204} 205```