1# Context
2
3Context模块继承自[BaseContext](js-apis-inner-application-baseContext.md),提供了ability或application的上下文的能力,包括访问特定应用程序的资源等。
4
5> **说明:**
6>
7>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>  - 本模块接口仅可在Stage模型下使用。
9
10## 导入模块
11
12```ts
13import { common } from '@kit.AbilityKit';
14```
15
16## 属性
17
18**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
19
20| 名称                  | 类型     | 只读   | 可选   | 说明                                                               |
21|---------------------| ------ | ---- | ---- |------------------------------------------------------------------|
22| resourceManager     | resmgr.[ResourceManager](../apis-localization-kit/js-apis-resource-manager.md#resourcemanager) | 否    | 否    | 资源管理对象。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
23| applicationInfo     | [ApplicationInfo](js-apis-bundleManager-applicationInfo.md) | 否    | 否    | 当前应用程序的信息。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
24| cacheDir            | string | 否    | 否    | 缓存目录。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
25| tempDir             | string | 否    | 否    | 临时目录。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
26| resourceDir<sup>11+<sup>         | string | 否    | 否    | 资源目录。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
27| filesDir            | string | 否    | 否    | 文件目录。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
28| databaseDir         | string | 否    | 否    | 数据库目录。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
29| preferencesDir      | string | 否    | 否    | preferences目录。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
30| bundleCodeDir       | string | 否    | 否    | 安装包目录。不能拼接路径访问资源文件,请使用[资源管理接口](../apis-localization-kit/js-apis-resource-manager.md)访问资源。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
31| distributedFilesDir | string | 否    | 否    | 分布式文件目录。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
32| cloudFileDir<sup>12+</sup>        | string | 否    | 否    | 云文件目录。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。    |
33| eventHub            | [EventHub](js-apis-inner-application-eventHub.md) | 否    | 否    | 事件中心,提供订阅、取消订阅、触发事件对象。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
34| area                | contextConstant.[AreaMode](js-apis-app-ability-contextConstant.md) | 否    | 否    | 文件分区信息。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
35
36## Context.createModuleContext
37
38createModuleContext(moduleName: string): Context
39
40根据模块名创建上下文。
41
42**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
43
44**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
45
46**参数:**
47
48| 参数名       | 类型                     | 必填   | 说明            |
49| -------- | ---------------------- | ---- | ------------- |
50| moduleName | string | 是    | 模块名。 |
51
52**返回值:**
53
54| 类型 | 说明 |
55| -------- | -------- |
56| Context | 模块的上下文。 |
57
58**错误码**:
59
60以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。
61
62| 错误码ID | 错误信息 |
63| ------- | -------------------------------- |
64| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
65
66**示例:**
67
68```ts
69import { common, UIAbility } from '@kit.AbilityKit';
70import { BusinessError } from '@kit.BasicServicesKit';
71
72export default class EntryAbility extends UIAbility {
73  onCreate() {
74    console.log('MyAbility onCreate');
75    let moduleContext: common.Context;
76    try {
77      moduleContext = this.context.createModuleContext('entry');
78    } catch (error) {
79      console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
80    }
81  }
82}
83```
84
85> 说明:仅支持获取本应用中其他Module的Context和应用内HSP的Context,不支持获取其他应用的Context。
86
87## Context.getApplicationContext
88
89getApplicationContext(): ApplicationContext
90
91获取本应用的应用上下文。
92
93**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
94
95**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
96
97**返回值:**
98
99| 类型 | 说明 |
100| -------- | -------- |
101| [ApplicationContext](js-apis-inner-application-applicationContext.md) | 应用上下文Context。 |
102
103**错误码**:
104
105以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。
106
107| 错误码ID | 错误信息 |
108| ------- | -------------------------------- |
109| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
110
111**示例:**
112
113```ts
114import { common, UIAbility } from '@kit.AbilityKit';
115import { BusinessError } from '@kit.BasicServicesKit';
116
117export default class EntryAbility extends UIAbility {
118  onCreate() {
119    console.log('MyAbility onCreate');
120    let applicationContext: common.Context;
121    try {
122      applicationContext = this.context.getApplicationContext();
123    } catch (error) {
124      console.error(`getApplicationContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
125    }
126  }
127}
128```
129
130## Context.getGroupDir<sup>10+</sup>
131
132getGroupDir(dataGroupID: string): Promise\<string>
133
134通过使用应用中的Group ID获取对应的共享目录,使用Promise异步回调。
135
136**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
137
138**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
139
140**参数:**
141
142| 参数名       | 类型                     | 必填   | 说明            |
143| -------- | ---------------------- | ---- | ------------- |
144| dataGroupID | string | 是    | 原子化服务应用项目创建时,系统会指定分配唯一Group ID。 |
145
146**返回值:**
147
148| 类型 | 说明 |
149| -------- | -------- |
150| Promise\<string> | 以Promise方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。|
151
152**错误码:**
153
154以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
155
156| 错误码ID | 错误信息 |
157| ------- | -------------------------------- |
158| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
159| 16000011 | The context does not exist. |
160
161**示例:**
162
163```ts
164import { common, UIAbility } from '@kit.AbilityKit';
165import { BusinessError } from '@kit.BasicServicesKit';
166
167export default class EntryAbility extends UIAbility {
168  onCreate() {
169    console.log('MyAbility onCreate');
170    let groupId = "1";
171    let getGroupDirContext: common.Context = this.context;
172    try {
173      getGroupDirContext.getGroupDir(groupId).then(data => {
174        console.log("getGroupDir result:" + data);
175      })
176    } catch (error) {
177      console.error(`getGroupDirContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
178    }
179  }
180}
181```
182
183## Context.getGroupDir<sup>10+</sup>
184
185getGroupDir(dataGroupID: string, callback: AsyncCallback\<string>): void
186
187通过使用应用中的Group ID获取对应的共享目录,使用callback异步回调。
188
189**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
190
191**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
192
193**参数:**
194
195| 参数名       | 类型                     | 必填   | 说明            |
196| -------- | ---------------------- | ---- | ------------- |
197| dataGroupID | string | 是    | 原子化服务应用项目创建时,系统会指定分配唯一Group ID。 |
198| callback | AsyncCallback\<string> | 是    | 以callback方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。|
199
200**错误码:**
201
202以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。
203
204| 错误码ID | 错误信息 |
205| ------- | -------------------------------- |
206| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
207| 16000011 | The context does not exist. |
208
209**示例:**
210
211```ts
212import { common, UIAbility } from '@kit.AbilityKit';
213import { BusinessError } from '@kit.BasicServicesKit';
214
215export default class EntryAbility extends UIAbility {
216  onCreate() {
217    console.log('MyAbility onCreate');
218    let getGroupDirContext: common.Context = this.context;
219
220    getGroupDirContext.getGroupDir("1", (err: BusinessError, data) => {
221      if (err) {
222        console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`);
223      } else {
224        console.log(`getGroupDir result is: ${JSON.stringify(data)}`);
225      }
226    });
227  }
228}
229```
230