• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  @ohos.app.ability.application (Application) (System API)
2You can use this module to create a [Context](../../application-models/application-context-stage.md).
3
4> **NOTE**
5>
6> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.
7> The APIs of this module can be used only in the stage model.
8
9## Modules to Import
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
18Creates the context for a module.
19
20> **NOTE**
21>
22> Since API version 18, the context can obtain the [process name](js-apis-inner-application-context.md#properties) of the current application. The **processName** property in the context created by **createModuleContext** is the same as the **processName** property in the input parameter **Context**. The values of other properties are obtained based on the input parameters **Context**, **bundleName**, and **moduleName**.
23
24**Atomic service API**: This API can be used in atomic services since API version 12.
25
26**System capability**: SystemCapability.Ability.AbilityRuntime.Core
27
28**System API**: This is a system API.
29
30**Parameters**
31
32| Name       | Type                                      | Mandatory  | Description            |
33| --------- | ---------------------------------------- | ---- | -------------- |
34| context | [Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md) | Yes| Application context.|
35| bundleName | string   | Yes   | Bundle name of the application. If an empty string is passed in, the current application is used by default.|
36| moduleName | string | Yes| Module name.|
37
38**Return value**
39
40| Type              | Description               |
41| ------------------ | ------------------- |
42| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise used to return the context created.|
43
44**Error codes**
45
46For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
47
48| ID| Error Message       |
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**Example**
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
81Creates the context for an application.
82
83> **NOTE**
84>
85> Since API version 18, the context can obtain the [process name](js-apis-inner-application-context.md#properties) of the current application. The **processName** property in the context created by **createBundleContext** is the same as the **processName** property in the input parameter **Context**. The values of other properties are obtained based on the input parameters **Context**, **bundleName**, and **moduleName**.
86
87**Atomic service API**: This API can be used in atomic services since API version 12.
88
89**System capability**: SystemCapability.Ability.AbilityRuntime.Core
90
91**System API**: This is a system API.
92
93**Parameters**
94
95| Name       | Type                                      | Mandatory  | Description            |
96| --------- | ---------------------------------------- | ---- | -------------- |
97| context | [Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md) | Yes| Application context.|
98| bundleName | string   | Yes   | Bundle name of the application.|
99
100**Return value**
101
102| Type              | Description               |
103| ------------------ | ------------------- |
104| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise used to return the context created.|
105
106**Error codes**
107
108For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
109
110| ID| Error Message       |
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**Example**
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```
139