• 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> - Starting from API version 18, the context can obtain the [process name](js-apis-inner-application-context.md#context) 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> - This API requires multiple bundle information queries and loads all resources of the specified module, which can be quite slow. To optimize performance, you are advised to cache the created context during the first use to prevent repeated calls for context creation.
25
26**Atomic service API**: This API can be used in atomic services since API version 12.
27
28**System capability**: SystemCapability.Ability.AbilityRuntime.Core
29
30**System API**: This is a system API.
31
32**Parameters**
33
34| Name       | Type                                      | Mandatory  | Description            |
35| --------- | ---------------------------------------- | ---- | -------------- |
36| context | [Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md) | Yes| Application context.|
37| bundleName | string   | Yes   | Bundle name of the application. If an empty string is passed in, the current application is used by default.|
38| moduleName | string | Yes| Module name.|
39
40**Return value**
41
42| Type              | Description               |
43| ------------------ | ------------------- |
44| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise used to return the context created.|
45
46**Error codes**
47
48For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
49
50| ID| Error Message       |
51| -------- | --------------- |
52| 201 | Permission denied. |
53| 202 | Permission denied, non-system app called system api.|
54| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
55
56**Example**
57
58```ts
59import { UIAbility, application, common } from '@kit.AbilityKit';
60import { BusinessError } from '@kit.BasicServicesKit';
61
62export default class EntryAbility extends UIAbility {
63  onCreate() {
64    let moduleContext: common.Context;
65    try {
66      application.createModuleContext(this.context, 'bundlename', 'entry').then((data: Context)=>{
67        moduleContext = data;
68        console.info('createModuleContext success!');
69      }).catch((error : BusinessError)=>{
70        console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
71      })
72    } catch (error) {
73      console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
74    }
75  }
76}
77```
78
79## application.createBundleContext<sup>12+</sup>
80
81createBundleContext(context: Context, bundleName: string): Promise\<Context>
82
83Creates the context for an application.
84
85> **NOTE**
86>
87> Starting from API version 18, the context can obtain the [process name](js-apis-inner-application-context.md#context) 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**.
88
89**Atomic service API**: This API can be used in atomic services since API version 12.
90
91**System capability**: SystemCapability.Ability.AbilityRuntime.Core
92
93**System API**: This is a system API.
94
95**Parameters**
96
97| Name       | Type                                      | Mandatory  | Description            |
98| --------- | ---------------------------------------- | ---- | -------------- |
99| context | [Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md) | Yes| Application context.|
100| bundleName | string   | Yes   | Bundle name of the application.|
101
102**Return value**
103
104| Type              | Description               |
105| ------------------ | ------------------- |
106| Promise\<[Context](../../reference/apis-ability-kit/js-apis-inner-application-context.md)> | Promise used to return the context created.|
107
108**Error codes**
109
110For details about the error codes, see [Ability Error Codes](errorcode-ability.md).
111
112| ID| Error Message       |
113| -------- | --------------- |
114| 201 | Permission denied. |
115| 202 | Permission denied, non-system app called system api.|
116| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
117
118
119**Example**
120
121```ts
122import { UIAbility, application, common } from '@kit.AbilityKit';
123import { BusinessError } from '@kit.BasicServicesKit';
124
125export default class EntryAbility extends UIAbility {
126  onCreate() {
127    let moduleContext: common.Context;
128    try {
129      application.createBundleContext(this.context, 'bundlename').then((data: Context)=>{
130        moduleContext = data;
131        console.info('createBundleContext success!');
132      }).catch((error : BusinessError)=>{
133        console.error(`createBundleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
134      })
135    } catch (error) {
136      console.error(`createBundleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
137    }
138  }
139}
140
141```
142## application.createPluginModuleContextForHostBundle<sup>20+</sup>
143
144createPluginModuleContextForHostBundle(context: Context, pluginBundleName: string, pluginModuleName: string, hostBundleName: string): Promise\<Context>
145
146Creates the context for a plugin based on a given context, plugin bundle name, plugin module name, and application bundle name to obtain the basic information about the plugin. This API uses a promise to return the result.
147
148**Required permissions**: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
149
150**System capability**: SystemCapability.Ability.AbilityRuntime.Core
151
152**System API**: This is a system API.
153
154**Parameters**
155
156| Name       | Type                                      | Mandatory  | Description            |
157| --------- | ---------------------------------------- | ---- | -------------- |
158| context | [Context](js-apis-inner-application-context.md) | Yes| Application context.|
159| pluginBundleName | string | Yes| Bundle name of the plugin.|
160| pluginModuleName | string | Yes| Module name of the plugin.|
161| hostBundleName | string | Yes| Bundle name of the application for which the plugin is installed.|
162
163**Return value**
164
165| Type              | Description               |
166| ------------------ | ------------------- |
167| Promise\<[Context](js-apis-inner-application-context.md)> | Promise used to return the context created, in which the **processName** and **config** properties are the same as those of the input context.|
168
169**Error codes**
170
171For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
172
173| ID| Error Message|
174| ------- | -------- |
175| 201      | Permission denied. |
176| 202      | Permission denied, non-system app called system api. |
177
178**Example**
179
180```ts
181import { AbilityConstant, UIAbility, application, common, Want } from '@kit.AbilityKit';
182import { BusinessError } from '@kit.BasicServicesKit';
183
184export default class EntryAbility extends UIAbility {
185  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
186    let moduleContext: common.Context;
187    try {
188      application.createPluginModuleContextForHostBundle(this.context, 'com.example.pluginBundleName', 'pluginModuleName', 'com.example.hostBundleName')
189        .then((data: Context) => {
190          moduleContext = data;
191          console.info('createPluginModuleContextForHostBundle success!');
192        })
193        .catch((error: BusinessError) => {
194          let code: number = (error as BusinessError).code;
195          let message: string = (error as BusinessError).message;
196          console.error(`createPluginModuleContextForHostBundle failed, error.code: ${code}, error.message: ${message}`);
197        });
198    } catch (error) {
199      let code: number = (error as BusinessError).code;
200      let message: string = (error as BusinessError).message;
201      console.error(`createPluginModuleContextForHostBundle failed, error.code: ${code}, error.message: ${message}`);
202    }
203  }
204}
205```
206