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**元服务API:** 从API version 12开始,该接口支持在元服务中使用。 21 22**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 23 24**系统接口**:此接口为系统接口。 25 26**参数**: 27 28| 参数名 | 类型 | 必填 | 说明 | 29| --------- | ---------------------------------------- | ---- | -------------- | 30| context | [Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md) | 是 | 表示应用上下文。 | 31| bundleName | string | 是 | 表示应用包名。取值为空字符串时,默认为当前应用。| 32| moduleName | string | 是 | 表示应用模块名。 | 33 34**返回值:** 35 36| 类型 | 说明 | 37| ------------------ | ------------------- | 38| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise对象。返回创建的Context。 | 39 40**错误码:** 41 42以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 43 44| 错误码ID | 错误信息 | 45| -------- | --------------- | 46| 201 | Permission denied. | 47| 202 | Permission denied, non-system app called system api.| 48| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 49 50**示例:** 51 52```ts 53import { UIAbility, application} from '@kit.AbilityKit'; 54import { BusinessError } from '@kit.BasicServicesKit'; 55 56export default class EntryAbility extends UIAbility { 57 onCreate() { 58 let moduleContext: common.Context; 59 try { 60 application.createModuleContext(this.context, 'bundlename', 'entry').then((data: Context)=>{ 61 moduleContext = data; 62 console.info('createModuleContext success!'); 63 }).catch((error : BusinessError)=>{ 64 console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 65 }) 66 } catch (error) { 67 console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 68 } 69 } 70} 71``` 72 73## application.createBundleContext<sup>12+</sup> 74 75createBundleContext(context: Context, bundleName: string): Promise\<Context> 76 77根据入参Context创建相应应用的Context。 78 79**元服务API:** 从API version 12开始,该接口支持在元服务中使用。 80 81**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 82 83**系统接口**:此接口为系统接口。 84 85**参数**: 86 87| 参数名 | 类型 | 必填 | 说明 | 88| --------- | ---------------------------------------- | ---- | -------------- | 89| context | [Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md) | 是 | 表示应用上下文。 | 90| bundleName | string | 是 | 表示应用包名。 | 91 92**返回值:** 93 94| 类型 | 说明 | 95| ------------------ | ------------------- | 96| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise对象。返回创建的Context。 | 97 98**错误码:** 99 100以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 101 102| 错误码ID | 错误信息 | 103| -------- | --------------- | 104| 201 | Permission denied. | 105| 202 | Permission denied, non-system app called system api.| 106| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 107 108 109**示例:** 110 111```ts 112import { UIAbility, application} from '@kit.AbilityKit'; 113import { BusinessError } from '@kit.BasicServicesKit'; 114 115export default class EntryAbility extends UIAbility { 116 onCreate() { 117 let moduleContext: common.Context; 118 try { 119 application.createBundleContext(this.context, 'bundlename').then((data: Context)=>{ 120 moduleContext = data; 121 console.info('createBundleContext success!'); 122 }).catch((error : BusinessError)=>{ 123 console.error(`createBundleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 124 }) 125 } catch (error) { 126 console.error(`createBundleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 127 } 128 } 129} 130```