• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Context
2
3The **Context** module provides context for abilities or applications. It allows access to application-specific resources.
4
5> **NOTE**
6>
7>  - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>  - The APIs of this module can be used only in the stage model.
9
10## Modules to Import
11
12```ts
13import common from '@ohos.app.ability.common';
14```
15
16## Attributes
17
18**System capability**: SystemCapability.Ability.AbilityRuntime.Core
19
20| Name         | Type    | Readable  | Writable  | Description     |
21| ----------- | ------ | ---- | ---- | ------- |
22| resourceManager     | resmgr.[ResourceManager](js-apis-resource-manager.md) | Yes   | No   | Object for resource management.  |
23| applicationInfo | [ApplicationInfo](js-apis-bundle-ApplicationInfo.md) | Yes   | No   | Application information.|
24| cacheDir | string | Yes   | No   | Cache directory.|
25| tempDir | string | Yes   | No   | Temporary directory.|
26| filesDir | string | Yes   | No   | File directory.|
27| databaseDir | string | Yes   | No   | Database directory.|
28| preferencesDir | string | Yes   | No   | Preferences directory.|
29| bundleCodeDir | string | Yes   | No   | Bundle code directory. Do not access resource files by concatenating paths. Use the [resourceManager API](js-apis-resource-manager.md) instead.|
30| distributedFilesDir | string | Yes   | No   | Distributed file directory.|
31| eventHub | [EventHub](js-apis-inner-application-eventHub.md) | Yes   | No   | Event hub that implements event subscription, unsubscription, and triggering.|
32| area | contextConstant.[AreaMode](js-apis-app-ability-contextConstant.md) | Yes   | No   | Encryption level of the directory.|
33
34## Context.createBundleContext
35
36createBundleContext(bundleName: string): Context;
37
38Creates the context based on the bundle name.
39
40**Required permissions**: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
41
42**System capability**: SystemCapability.Ability.AbilityRuntime.Core
43
44**System API**: This is a system API and cannot be called by third-party applications.
45
46**Parameters**
47
48| Name      | Type                    | Mandatory  | Description           |
49| -------- | ---------------------- | ---- | ------------- |
50| bundleName | string | Yes   | Bundle name.|
51
52**Return value**
53
54| Type| Description|
55| -------- | -------- |
56| Context | Context created.|
57
58**Example**
59
60```ts
61import common from '@ohos.app.ability.common';
62
63let bundleContext: common.Context;
64try {
65    bundleContext = this.context.createBundleContext('com.example.test');
66} catch (error) {
67    console.error('createBundleContext failed, error.code: ${error.code}, error.message: ${error.message}');
68}
69```
70
71## Context.createModuleContext
72
73createModuleContext(moduleName: string): Context;
74
75Creates the context based on the module name.
76
77**System capability**: SystemCapability.Ability.AbilityRuntime.Core
78
79**Parameters**
80
81| Name      | Type                    | Mandatory  | Description           |
82| -------- | ---------------------- | ---- | ------------- |
83| moduleName | string | Yes   | Module name.|
84
85**Return value**
86
87| Type| Description|
88| -------- | -------- |
89| Context | Context created.|
90
91**Example**
92
93```ts
94import common from '@ohos.app.ability.common';
95
96let moduleContext: common.Context;
97try {
98    moduleContext = this.context.createModuleContext('entry');
99} catch (error) {
100    console.error('createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}');
101}
102```
103
104## Context.createModuleContext
105
106createModuleContext(bundleName: string, moduleName: string): Context;
107
108Creates the context based on the bundle name and module name.
109
110**System capability**: SystemCapability.Ability.AbilityRuntime.Core
111
112**System API**: This is a system API and cannot be called by third-party applications.
113
114**Parameters**
115
116| Name      | Type                    | Mandatory  | Description           |
117| -------- | ---------------------- | ---- | ------------- |
118| bundleName | string | Yes   | Bundle name.|
119| moduleName | string | Yes   | Module name.|
120
121**Return value**
122
123| Type| Description|
124| -------- | -------- |
125| Context | Context created.|
126
127**Example**
128
129```ts
130import common from '@ohos.app.ability.common';
131
132let moduleContext: common.Context;
133try {
134    moduleContext = this.context.createModuleContext('com.example.test', 'entry');
135} catch (error) {
136    console.error('createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}');
137}
138```
139
140## Context.getApplicationContext
141
142getApplicationContext(): ApplicationContext;
143
144Obtains the context of this application.
145
146**System capability**: SystemCapability.Ability.AbilityRuntime.Core
147
148**Return value**
149
150| Type| Description|
151| -------- | -------- |
152| [ApplicationContext](js-apis-inner-application-applicationContext.md) | Application context obtained.|
153
154**Example**
155
156```ts
157import common from '@ohos.app.ability.common';
158
159let applicationContext: common.Context;
160try {
161    applicationContext = this.context.getApplicationContext();
162} catch (error) {
163    console.error('getApplicationContext failed, error.code: ${error.code}, error.message: ${error.message}');
164}
165```
166
167## Context.getGroupDir<sup>10+</sup>
168
169getGroupDir(dataGroupID: string): Promise\<string>;
170
171Obtains the shared directory based on a group ID. This API uses a promise to return the result.
172
173**System capability**: SystemCapability.Ability.AbilityRuntime.Core
174
175**Parameters**
176
177| Name      | Type                    | Mandatory  | Description           |
178| -------- | ---------------------- | ---- | ------------- |
179| dataGroupID | string | Yes   | Group ID, which is assigned by the system when an atomic service application project is created.|
180
181**Return value**
182
183| Type| Description|
184| -------- | -------- |
185| Promise\<string> | Promise used to return the result. If no shared directory exists, null is returned. Only the encryption level EL2 is supported.|
186
187**Error codes**
188
189For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
190
191| ID| Error Message|
192| ------- | -------- |
193| 16000011 | The context does not exist. |
194
195**Example**
196
197```ts
198import common from '@ohos.app.ability.common';
199
200let groupId = "1";
201let getGroupDirContext: common.Context = this.context;
202try {
203  getGroupDirContext.getGroupDir(groupId).then(data => {
204    console.log("getGroupDir result:" + data);
205  })
206} catch (error) {
207  console.error('getGroupDirContext failed, error.code: ${error.code}, error.message: ${error.message}');
208}
209```
210
211## Context.getGroupDir<sup>10+</sup>
212
213getGroupDir(dataGroupID: string, callback: AsyncCallback\<string>): void;
214
215Obtains the shared directory based on a group ID. This API uses an asynchronous callback to return the result.
216
217**System capability**: SystemCapability.Ability.AbilityRuntime.Core
218
219**Parameters**
220
221| Name      | Type                    | Mandatory  | Description           |
222| -------- | ---------------------- | ---- | ------------- |
223| dataGroupID | string | Yes   | Group ID, which is assigned by the system when an atomic service application project is created.|
224| callback | AsyncCallback\<string> | Yes   | Callback used to return the result. If no shared directory exists, null is returned. Only the encryption level EL2 is supported.|
225
226**Error codes**
227
228For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).
229
230| ID| Error Message|
231| ------- | -------- |
232| 16000011 | The context does not exist. |
233
234**Example**
235
236```ts
237import common from '@ohos.app.ability.common';
238
239let getGroupDirContext: common.Context = this.context;
240
241getGroupDirContext.getGroupDir("1", (err, data) => {
242  if (err) {
243    console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`);
244  } else {
245    console.log(`getGroupDir result is: ${JSON.stringify(data)}`);
246  }
247});
248```
249
250