• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Context
2
3Context模块提供了ability或application的上下文的能力,包括访问特定应用程序的资源等。
4
5> **说明:**
6>
7>  - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>  - 本模块接口仅可在Stage模型下使用。
9
10## 导入模块
11
12```ts
13import common from '@ohos.app.ability.common';
14```
15
16## 属性
17
18**系统能力**:以下各项对应的系统能力均为SystemCapability.Ability.AbilityRuntime.Core
19
20| 名称          | 类型     | 可读   | 可写   | 说明      |
21| ----------- | ------ | ---- | ---- | ------- |
22| resourceManager     | resmgr.[ResourceManager](js-apis-resource-manager.md) | 是    | 否    | 资源管理对象。   |
23| applicationInfo | [ApplicationInfo](js-apis-bundle-ApplicationInfo.md) | 是    | 否    | 当前应用程序的信息。 |
24| cacheDir | string | 是    | 否    | 缓存目录。 |
25| tempDir | string | 是    | 否    | 临时目录。 |
26| filesDir | string | 是    | 否    | 文件目录。 |
27| databaseDir | string | 是    | 否    | 数据库目录。 |
28| preferencesDir | string | 是    | 否    | preferences目录。 |
29| bundleCodeDir | string | 是    | 否    | 安装包目录。不能拼接路径访问资源文件,请使用[资源管理接口](js-apis-resource-manager.md)访问资源。 |
30| distributedFilesDir | string | 是    | 否    | 分布式文件目录。 |
31| eventHub | [EventHub](js-apis-inner-application-eventHub.md) | 是    | 否    | 事件中心,提供订阅、取消订阅、触发事件对象。 |
32| area | contextConstant.[AreaMode](js-apis-app-ability-contextConstant.md) | 是    | 否    | 文件分区信息。 |
33
34## Context.createBundleContext
35
36createBundleContext(bundleName: string): Context;
37
38根据Bundle名称创建安装包的上下文。
39
40**需要权限**: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
41
42**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
43
44**系统API**:该接口为系统接口,三方应用不支持调用。
45
46**参数:**
47
48| 参数名       | 类型                     | 必填   | 说明            |
49| -------- | ---------------------- | ---- | ------------- |
50| bundleName | string | 是    | Bundle名称。 |
51
52**返回值:**
53
54| 类型 | 说明 |
55| -------- | -------- |
56| Context | 安装包的上下文。 |
57
58**示例:**
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
75根据模块名创建上下文。
76
77**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
78
79**参数:**
80
81| 参数名       | 类型                     | 必填   | 说明            |
82| -------- | ---------------------- | ---- | ------------- |
83| moduleName | string | 是    | 模块名。 |
84
85**返回值:**
86
87| 类型 | 说明 |
88| -------- | -------- |
89| Context | 模块的上下文。 |
90
91**示例:**
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
108根据Bundle名称和模块名称创建上下文。
109
110**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
111
112**系统API**:该接口为系统接口,三方应用不支持调用。
113
114**参数:**
115
116| 参数名       | 类型                     | 必填   | 说明            |
117| -------- | ---------------------- | ---- | ------------- |
118| bundleName | string | 是    | Bundle名称。 |
119| moduleName | string | 是    | 模块名。 |
120
121**返回值:**
122
123| 类型 | 说明 |
124| -------- | -------- |
125| Context | 模块的上下文。 |
126
127**示例:**
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
144获取本应用的应用上下文。
145
146**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
147
148**返回值:**
149
150| 类型 | 说明 |
151| -------- | -------- |
152| [ApplicationContext](js-apis-inner-application-applicationContext.md) | 应用上下文Context。 |
153
154**示例:**
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
171通过使用元服务应用中的Group ID获取对应的共享目录,使用Promise异步回调。
172
173**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
174
175**参数:**
176
177| 参数名       | 类型                     | 必填   | 说明            |
178| -------- | ---------------------- | ---- | ------------- |
179| dataGroupID | string | 是    | 元服务应用项目创建时,系统会指定分配唯一Group ID。 |
180
181**返回值:**
182
183| 类型 | 说明 |
184| -------- | -------- |
185| Promise\<string> | 以Promise方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。|
186
187**错误码**:
188
189以下错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
190
191| 错误码ID | 错误信息 |
192| ------- | -------- |
193| 16000011 | The context does not exist. |
194
195**示例:**
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
215通过使用元服务应用中的Group ID获取对应的共享目录,使用callback异步回调。
216
217**系统能力**:SystemCapability.Ability.AbilityRuntime.Core
218
219**参数:**
220
221| 参数名       | 类型                     | 必填   | 说明            |
222| -------- | ---------------------- | ---- | ------------- |
223| dataGroupID | string | 是    | 元服务应用项目创建时,系统会指定分配唯一Group ID。 |
224| callback | AsyncCallback\<string> | 是    | 以callback方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。|
225
226**错误码**:
227
228以下错误码详细介绍请参考[errcode-ability](../errorcodes/errorcode-ability.md)。
229
230| 错误码ID | 错误信息 |
231| ------- | -------- |
232| 16000011 | The context does not exist. |
233
234**示例:**
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