1e41f4b71Sopenharmony_ci# Context
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThe Context module, inherited frome [BaseContext](js-apis-inner-application-baseContext.md), provides context for abilities or applications, including access to application-specific resources.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci>  - 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.
8e41f4b71Sopenharmony_ci>  - The APIs of this module can be used only in the stage model.
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## Modules to Import
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci```ts
13e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit';
14e41f4b71Sopenharmony_ci```
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## Properties
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci| Name                 | Type    | Read-only  | Optional  | Description                                                              |
21e41f4b71Sopenharmony_ci|---------------------| ------ | ---- | ---- |------------------------------------------------------------------|
22e41f4b71Sopenharmony_ci| resourceManager     | resmgr.[ResourceManager](../apis-localization-kit/js-apis-resource-manager.md#resourcemanager) | No   | No   | Object for resource management.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
23e41f4b71Sopenharmony_ci| applicationInfo     | [ApplicationInfo](js-apis-bundleManager-applicationInfo.md) | No   | No   | Application information.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
24e41f4b71Sopenharmony_ci| cacheDir            | string | No   | No   | Cache directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
25e41f4b71Sopenharmony_ci| tempDir             | string | No   | No   | Temporary directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
26e41f4b71Sopenharmony_ci| resourceDir<sup>11+<sup>         | string | No   | No   | Resource directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
27e41f4b71Sopenharmony_ci| filesDir            | string | No   | No   | File directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
28e41f4b71Sopenharmony_ci| databaseDir         | string | No   | No   | Database directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
29e41f4b71Sopenharmony_ci| preferencesDir      | string | No   | No   | Preferences directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
30e41f4b71Sopenharmony_ci| bundleCodeDir       | string | No   | No   | Bundle code directory. Do not access resource files using concatenated paths. Use [@ohos.resourceManager](../apis-localization-kit/js-apis-resource-manager.md) instead.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
31e41f4b71Sopenharmony_ci| distributedFilesDir | string | No   | No   | Distributed file directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
32e41f4b71Sopenharmony_ci| cloudFileDir<sup>12+</sup>        | string | No   | No   | Cloud file directory.<br>**Atomic service API**: This API can be used in atomic services since API version 12.   |
33e41f4b71Sopenharmony_ci| eventHub            | [EventHub](js-apis-inner-application-eventHub.md) | No   | No   | Event hub that implements event subscription, unsubscription, and triggering.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
34e41f4b71Sopenharmony_ci| area                | contextConstant.[AreaMode](js-apis-app-ability-contextConstant.md) | No   | No   | Encryption level of the directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.|
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci## Context.createModuleContext
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_cicreateModuleContext(moduleName: string): Context
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ciCreates the context based on the module name.
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci**Parameters**
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci| Name      | Type                    | Mandatory  | Description           |
49e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ------------- |
50e41f4b71Sopenharmony_ci| moduleName | string | Yes   | Module name.|
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci**Return value**
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci| Type| Description|
55e41f4b71Sopenharmony_ci| -------- | -------- |
56e41f4b71Sopenharmony_ci| Context | Context created.|
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci**Error codes**
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci| ID| Error Message|
63e41f4b71Sopenharmony_ci| ------- | -------------------------------- |
64e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci**Example**
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci```ts
69e41f4b71Sopenharmony_ciimport { common, UIAbility } from '@kit.AbilityKit';
70e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
73e41f4b71Sopenharmony_ci  onCreate() {
74e41f4b71Sopenharmony_ci    console.log('MyAbility onCreate');
75e41f4b71Sopenharmony_ci    let moduleContext: common.Context;
76e41f4b71Sopenharmony_ci    try {
77e41f4b71Sopenharmony_ci      moduleContext = this.context.createModuleContext('entry');
78e41f4b71Sopenharmony_ci    } catch (error) {
79e41f4b71Sopenharmony_ci      console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
80e41f4b71Sopenharmony_ci    }
81e41f4b71Sopenharmony_ci  }
82e41f4b71Sopenharmony_ci}
83e41f4b71Sopenharmony_ci```
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci> **NOTE**
86e41f4b71Sopenharmony_ci>
87e41f4b71Sopenharmony_ci> Only the context of other modules in the current application and the context of the intra-application HSP can be obtained. The context of other applications cannot be obtained.
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci## Context.getApplicationContext
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_cigetApplicationContext(): ApplicationContext
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ciObtains the context of this application.
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci**Return value**
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci| Type| Description|
102e41f4b71Sopenharmony_ci| -------- | -------- |
103e41f4b71Sopenharmony_ci| [ApplicationContext](js-apis-inner-application-applicationContext.md) | Application context obtained.|
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci**Error codes**
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci| ID| Error Message|
110e41f4b71Sopenharmony_ci| ------- | -------------------------------- |
111e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci**Example**
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci```ts
116e41f4b71Sopenharmony_ciimport { common, UIAbility } from '@kit.AbilityKit';
117e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
120e41f4b71Sopenharmony_ci  onCreate() {
121e41f4b71Sopenharmony_ci    console.log('MyAbility onCreate');
122e41f4b71Sopenharmony_ci    let applicationContext: common.Context;
123e41f4b71Sopenharmony_ci    try {
124e41f4b71Sopenharmony_ci      applicationContext = this.context.getApplicationContext();
125e41f4b71Sopenharmony_ci    } catch (error) {
126e41f4b71Sopenharmony_ci      console.error(`getApplicationContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
127e41f4b71Sopenharmony_ci    }
128e41f4b71Sopenharmony_ci  }
129e41f4b71Sopenharmony_ci}
130e41f4b71Sopenharmony_ci```
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci## Context.getGroupDir<sup>10+</sup>
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_cigetGroupDir(dataGroupID: string): Promise\<string>
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ciObtains the shared directory based on a group ID. This API uses a promise to return the result.
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci**Parameters**
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci| Name      | Type                    | Mandatory  | Description           |
145e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ------------- |
146e41f4b71Sopenharmony_ci| dataGroupID | string | Yes   | Group ID, which is assigned by the system when an atomic service project is created.|
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci**Return value**
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci| Type| Description|
151e41f4b71Sopenharmony_ci| -------- | -------- |
152e41f4b71Sopenharmony_ci| Promise\<string> | Promise used to return the result. If no shared directory exists, null is returned. Only the encryption level EL2 is supported.|
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci**Error codes**
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci| ID| Error Message|
159e41f4b71Sopenharmony_ci| ------- | -------------------------------- |
160e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
161e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. |
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci**Example**
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci```ts
166e41f4b71Sopenharmony_ciimport { common, UIAbility } from '@kit.AbilityKit';
167e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
170e41f4b71Sopenharmony_ci  onCreate() {
171e41f4b71Sopenharmony_ci    console.log('MyAbility onCreate');
172e41f4b71Sopenharmony_ci    let groupId = "1";
173e41f4b71Sopenharmony_ci    let getGroupDirContext: common.Context = this.context;
174e41f4b71Sopenharmony_ci    try {
175e41f4b71Sopenharmony_ci      getGroupDirContext.getGroupDir(groupId).then(data => {
176e41f4b71Sopenharmony_ci        console.log("getGroupDir result:" + data);
177e41f4b71Sopenharmony_ci      })
178e41f4b71Sopenharmony_ci    } catch (error) {
179e41f4b71Sopenharmony_ci      console.error(`getGroupDirContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
180e41f4b71Sopenharmony_ci    }
181e41f4b71Sopenharmony_ci  }
182e41f4b71Sopenharmony_ci}
183e41f4b71Sopenharmony_ci```
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci## Context.getGroupDir<sup>10+</sup>
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_cigetGroupDir(dataGroupID: string, callback: AsyncCallback\<string>): void
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ciObtains the shared directory based on a group ID. This API uses an asynchronous callback to return the result.
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11.
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci**Parameters**
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci| Name      | Type                    | Mandatory  | Description           |
198e41f4b71Sopenharmony_ci| -------- | ---------------------- | ---- | ------------- |
199e41f4b71Sopenharmony_ci| dataGroupID | string | Yes   | Group ID, which is assigned by the system when an atomic service project is created.|
200e41f4b71Sopenharmony_ci| 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.|
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci**Error codes**
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md).
205e41f4b71Sopenharmony_ci
206e41f4b71Sopenharmony_ci| ID| Error Message|
207e41f4b71Sopenharmony_ci| ------- | -------------------------------- |
208e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
209e41f4b71Sopenharmony_ci| 16000011 | The context does not exist. |
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ci**Example**
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci```ts
214e41f4b71Sopenharmony_ciimport { common, UIAbility } from '@kit.AbilityKit';
215e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility {
218e41f4b71Sopenharmony_ci  onCreate() {
219e41f4b71Sopenharmony_ci    console.log('MyAbility onCreate');
220e41f4b71Sopenharmony_ci    let getGroupDirContext: common.Context = this.context;
221e41f4b71Sopenharmony_ci
222e41f4b71Sopenharmony_ci    getGroupDirContext.getGroupDir("1", (err: BusinessError, data) => {
223e41f4b71Sopenharmony_ci      if (err) {
224e41f4b71Sopenharmony_ci        console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`);
225e41f4b71Sopenharmony_ci      } else {
226e41f4b71Sopenharmony_ci        console.log(`getGroupDir result is: ${JSON.stringify(data)}`);
227e41f4b71Sopenharmony_ci      }
228e41f4b71Sopenharmony_ci    });
229e41f4b71Sopenharmony_ci  }
230e41f4b71Sopenharmony_ci}
231e41f4b71Sopenharmony_ci```
232