1# @ohos.app.ability.application (Application) 2 3开发者可以通过该模块创建[Context](../../application-models/application-context-stage.md)。 4 5> **说明:** 6> 7> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 本模块接口仅可在Stage模型下使用。 9 10## 导入模块 11 12```ts 13import { application } from '@kit.AbilityKit'; 14``` 15 16## application.createModuleContext<sup>12+</sup> 17 18createModuleContext(context: Context, moduleName: string): Promise\<Context> 19 20根据入参Context创建相应模块的Context。 21 22**元服务API:** 从API version 12开始,该接口支持在元服务中使用。 23 24**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 25 26**参数**: 27 28| 参数名 | 类型 | 必填 | 说明 | 29| --------- | ---------------------------------------- | ---- | -------------- | 30| context | [Context](js-apis-inner-application-context.md) | 是 | 表示应用上下文。 | 31| moduleName | string | 是 | 表示应用模块名。 | 32 33**返回值:** 34 35| 类型 | 说明 | 36| ------------------ | ------------------- | 37| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise对象。返回创建的Context。 | 38 39**错误码:** 40 41以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 42 43| 错误码ID | 错误信息 | 44| -------- | --------------- | 45| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 46 47**示例:** 48 49```ts 50import { UIAbility, application, common } from '@kit.AbilityKit'; 51import { BusinessError } from '@kit.BasicServicesKit'; 52 53export default class EntryAbility extends UIAbility { 54 onCreate() { 55 let moduleContext: common.Context; 56 try { 57 application.createModuleContext(this.context, 'entry').then((data: Context) => { 58 moduleContext = data; 59 console.info('createBundleContext success!'); 60 }).catch((error: BusinessError) => { 61 let code: number = (error as BusinessError).code; 62 let message: string = (error as BusinessError).message; 63 console.error(`createModuleContext failed, error.code: ${code}, error.message: ${message}`); 64 }) 65 } catch (error) { 66 let code: number = (error as BusinessError).code; 67 let message: string = (error as BusinessError).message; 68 console.error(`createModuleContext failed, error.code: ${code}, error.message: ${message}`); 69 } 70 } 71} 72``` 73 74## application.getApplicationContext<sup>14+</sup> 75 76getApplicationContext(): ApplicationContext 77 78获取应用程序上下文。 79 80**元服务API:** 从API version 14开始,该接口支持在元服务中使用。 81 82**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 83 84**返回值:** 85 86| 类型 | 说明 | 87| ------------------------------------------------------------ | ------------------- | 88| [ApplicationContext](js-apis-inner-application-applicationContext.md) | 应用上下文Context。 | 89 90**错误码:** 91 92以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。 93 94| 错误码ID | 错误信息 | 95| -------- | --------------- | 96| 16000050 | Internal error. | 97 98**示例:** 99 100```ts 101import { UIAbility, application } from '@kit.AbilityKit'; 102import { BusinessError } from '@kit.BasicServicesKit'; 103 104export default class EntryAbility extends UIAbility { 105 onCreate(): void { 106 try { 107 let applicationContext = application.getApplicationContext(); 108 } catch (error) { 109 let code: number = (error as BusinessError).code; 110 let message: string = (error as BusinessError).message; 111 console.error(`getApplicationContext failed, error.code: ${code}, error.message: ${message}`); 112 } 113 } 114} 115```