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>
81>通过该接口取得的ApplicationContext,只支持获取对应的[应用信息](js-apis-bundleManager-applicationInfo.md)和全部的[沙箱路径](js-apis-inner-application-context.md#属性)。
82
83**原子化服务API**:从API version 14开始,该接口支持在元服务中使用。
84
85**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
86
87**返回值:**
88
89| 类型                                                         | 说明                |
90| ------------------------------------------------------------ | ------------------- |
91| [ApplicationContext](js-apis-inner-application-applicationContext.md) | 应用上下文Context。 |
92
93**错误码:**
94
95以下错误码详细介绍请参考[元能力子系统错误码](errorcode-ability.md)。
96
97| 错误码ID | 错误信息        |
98| -------- | --------------- |
99| 16000050 | Internal error. |
100
101**示例:**
102
103```ts
104import { UIAbility, application } from '@kit.AbilityKit';
105import { BusinessError } from '@kit.BasicServicesKit';
106
107export default class EntryAbility extends UIAbility {
108  onCreate(): void {
109    try {
110      let applicationContext = application.getApplicationContext();
111    } catch (error) {
112      let code: number = (error as BusinessError).code;
113      let message: string = (error as BusinessError).message;
114      console.error(`getApplicationContext failed, error.code: ${code}, error.message: ${message}`);
115    }
116  }
117}
118```