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>**说明**:需要开发者手动在`\<module-name>\resource`路径下创建`resfile`目录。创建的`resfile`目录仅支持以只读方式访问。<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| processName<sup>18+</sup> | string | 否 | 否 | 当前应用的进程名。<br/>**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 | 36 37## Context.createModuleContext<sup>(deprecated)</sup> 38 39createModuleContext(moduleName: string): Context 40 41根据模块名创建上下文。 42 43> **说明:** 44> 45> 从 API Version 12 开始废弃,建议使用[application.createModuleContext](./js-apis-app-ability-application.md#applicationcreatemodulecontext12)替代。 46 47**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 48 49**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 50 51**参数:** 52 53| 参数名 | 类型 | 必填 | 说明 | 54| -------- | ---------------------- | ---- | ------------- | 55| moduleName | string | 是 | 模块名。 | 56 57**返回值:** 58 59| 类型 | 说明 | 60| -------- | -------- | 61| Context | 模块的上下文。 | 62 63**错误码**: 64 65以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 66 67| 错误码ID | 错误信息 | 68| ------- | -------------------------------- | 69| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 70 71**示例:** 72 73```ts 74import { common, UIAbility } from '@kit.AbilityKit'; 75import { BusinessError } from '@kit.BasicServicesKit'; 76 77export default class EntryAbility extends UIAbility { 78 onCreate() { 79 console.log('MyAbility onCreate'); 80 let moduleContext: common.Context; 81 try { 82 moduleContext = this.context.createModuleContext('entry'); 83 } catch (error) { 84 console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 85 } 86 } 87} 88``` 89 90> 说明:仅支持获取本应用中其他Module的Context和应用内HSP的Context,不支持获取其他应用的Context。 91 92## Context.getApplicationContext 93 94getApplicationContext(): ApplicationContext 95 96获取本应用的应用上下文。 97 98**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 99 100**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 101 102**返回值:** 103 104| 类型 | 说明 | 105| -------- | -------- | 106| [ApplicationContext](js-apis-inner-application-applicationContext.md) | 应用上下文Context。 | 107 108**错误码**: 109 110以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 111 112| 错误码ID | 错误信息 | 113| ------- | -------------------------------- | 114| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 115 116**示例:** 117 118```ts 119import { common, UIAbility } from '@kit.AbilityKit'; 120import { BusinessError } from '@kit.BasicServicesKit'; 121 122export default class EntryAbility extends UIAbility { 123 onCreate() { 124 console.log('MyAbility onCreate'); 125 let applicationContext: common.Context; 126 try { 127 applicationContext = this.context.getApplicationContext(); 128 } catch (error) { 129 console.error(`getApplicationContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 130 } 131 } 132} 133``` 134 135## Context.getGroupDir<sup>10+</sup> 136 137getGroupDir(dataGroupID: string): Promise\<string> 138 139通过使用应用中的Group ID获取对应的共享目录,使用Promise异步回调。 140 141**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 142 143**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 144 145**参数:** 146 147| 参数名 | 类型 | 必填 | 说明 | 148| -------- | ---------------------- | ---- | ------------- | 149| dataGroupID | string | 是 | 原子化服务应用项目创建时,系统会指定分配唯一Group ID。 | 150 151**返回值:** 152 153| 类型 | 说明 | 154| -------- | -------- | 155| Promise\<string> | 以Promise方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。| 156 157**错误码:** 158 159以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 160 161| 错误码ID | 错误信息 | 162| ------- | -------------------------------- | 163| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 164| 16000011 | The context does not exist. | 165 166**示例:** 167 168```ts 169import { common, UIAbility } from '@kit.AbilityKit'; 170import { BusinessError } from '@kit.BasicServicesKit'; 171 172export default class EntryAbility extends UIAbility { 173 onCreate() { 174 console.log('MyAbility onCreate'); 175 let groupId = "1"; 176 let getGroupDirContext: common.Context = this.context; 177 try { 178 getGroupDirContext.getGroupDir(groupId).then(data => { 179 console.log("getGroupDir result:" + data); 180 }) 181 } catch (error) { 182 console.error(`getGroupDirContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 183 } 184 } 185} 186``` 187 188## Context.getGroupDir<sup>10+</sup> 189 190getGroupDir(dataGroupID: string, callback: AsyncCallback\<string>): void 191 192通过使用应用中的Group ID获取对应的共享目录,使用callback异步回调。 193 194**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 195 196**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 197 198**参数:** 199 200| 参数名 | 类型 | 必填 | 说明 | 201| -------- | ---------------------- | ---- | ------------- | 202| dataGroupID | string | 是 | 原子化服务应用项目创建时,系统会指定分配唯一Group ID。 | 203| callback | AsyncCallback\<string> | 是 | 以callback方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。| 204 205**错误码:** 206 207以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 208 209| 错误码ID | 错误信息 | 210| ------- | -------------------------------- | 211| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 212| 16000011 | The context does not exist. | 213 214**示例:** 215 216```ts 217import { common, UIAbility } from '@kit.AbilityKit'; 218import { BusinessError } from '@kit.BasicServicesKit'; 219 220export default class EntryAbility extends UIAbility { 221 onCreate() { 222 console.log('MyAbility onCreate'); 223 let getGroupDirContext: common.Context = this.context; 224 225 getGroupDirContext.getGroupDir("1", (err: BusinessError, data) => { 226 if (err) { 227 console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`); 228 } else { 229 console.log(`getGroupDir result is: ${JSON.stringify(data)}`); 230 } 231 }); 232 } 233} 234``` 235 236## Context.createAreaModeContext<sup>18+</sup> 237 238createAreaModeContext(areaMode: contextConstant.AreaMode): Context 239 240当应用需要将不同类型的信息存放在不同目录下时,可以根据指定的数据加密级别创建应用上下文,以获取相应的路径。 241 242**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 243 244**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 245 246**参数:** 247 248| 参数名 | 类型 | 必填 | 说明 | 249| -------- | ------------------------------------------------------------ | ---- | ------------------------ | 250| areaMode | [contextConstant.AreaMode](js-apis-app-ability-contextConstant.md#areamode) | 是 | 指定的数据加密等级。 | 251 252**返回值:** 253 254| 类型 | 说明 | 255| ------- | ---------------------- | 256| Context | 指定数据加密等级的上下文。 | 257 258**错误码**: 259 260以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 261 262| 错误码ID | 错误信息 | 263| -------- | ------------------------------------------------------------ | 264| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 265 266**示例:** 267 268```ts 269import { common, UIAbility, contextConstant } from '@kit.AbilityKit'; 270import { hilog } from '@kit.PerformanceAnalysisKit'; 271 272export default class EntryAbility extends UIAbility { 273 onCreate() { 274 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 275 let areaMode: contextConstant.AreaMode = contextConstant.AreaMode.EL2; 276 let areaModeContext: common.Context; 277 try { 278 areaModeContext = this.context.createAreaModeContext(areaMode); 279 } catch (error) { 280 hilog.error(0x0000, 'testTag', 'createAreaModeContext error is:%{public}s', JSON.stringify(error)); 281 } 282 } 283} 284``` 285 286## Context.createDisplayContext<sup>15+</sup> 287 288createDisplayContext(displayId: number): Context 289 290根据指定的物理屏幕ID创建应用上下文,以便于获取和使用其他带有屏幕信息(包括屏幕密度[ScreenDensity](../apis-localization-kit/js-apis-resource-manager.md#screendensity)和屏幕方向[Direction](../apis-localization-kit/js-apis-resource-manager.md#direction))的应用上下文。 291 292**原子化服务API**:从API version 15开始,该接口支持在原子化服务中使用。 293 294**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 295 296**参数:** 297 298| 参数名 | 类型 | 必填 | 说明 | 299| -------- | ------------------------------------------------------------ | ---- | ------------------------ | 300| displayId | number | 是 | 物理屏幕ID。 | 301 302**返回值:** 303 304| 类型 | 说明 | 305| ------- | ---------------------- | 306| [Context](#context) | 带有指定物理屏幕信息的上下文。 | 307 308**错误码:** 309 310以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 311 312| 错误码ID | 错误信息 | 313| -------- | ------------------------------------------------------------ | 314| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 315 316**示例:** 317 318```ts 319import { common, UIAbility } from '@kit.AbilityKit'; 320import { hilog } from '@kit.PerformanceAnalysisKit'; 321 322export default class EntryAbility extends UIAbility { 323 onCreate() { 324 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 325 let displayContext: common.Context; 326 try { 327 displayContext = this.context.createDisplayContext(0); 328 } catch (error) { 329 hilog.error(0x0000, 'testTag', 'createDisplayContext error is:%{public}s', JSON.stringify(error)); 330 } 331 } 332} 333``` 334