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