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<sup>(deprecated)</sup> 37 38createModuleContext(moduleName: string): Context 39 40根据模块名创建上下文。 41 42> **说明:** 43> 44> 从 API Version 12 开始废弃,建议使用[application.createModuleContext](./js-apis-app-ability-application.md#applicationcreatemodulecontext12)替代。 45 46**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 47 48**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 49 50**参数:** 51 52| 参数名 | 类型 | 必填 | 说明 | 53| -------- | ---------------------- | ---- | ------------- | 54| moduleName | string | 是 | 模块名。 | 55 56**返回值:** 57 58| 类型 | 说明 | 59| -------- | -------- | 60| Context | 模块的上下文。 | 61 62**错误码**: 63 64以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 65 66| 错误码ID | 错误信息 | 67| ------- | -------------------------------- | 68| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 69 70**示例:** 71 72```ts 73import { common, UIAbility } from '@kit.AbilityKit'; 74import { BusinessError } from '@kit.BasicServicesKit'; 75 76export default class EntryAbility extends UIAbility { 77 onCreate() { 78 console.log('MyAbility onCreate'); 79 let moduleContext: common.Context; 80 try { 81 moduleContext = this.context.createModuleContext('entry'); 82 } catch (error) { 83 console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 84 } 85 } 86} 87``` 88 89> 说明:仅支持获取本应用中其他Module的Context和应用内HSP的Context,不支持获取其他应用的Context。 90 91## Context.getApplicationContext 92 93getApplicationContext(): ApplicationContext 94 95获取本应用的应用上下文。 96 97**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 98 99**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 100 101**返回值:** 102 103| 类型 | 说明 | 104| -------- | -------- | 105| [ApplicationContext](js-apis-inner-application-applicationContext.md) | 应用上下文Context。 | 106 107**错误码**: 108 109以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 110 111| 错误码ID | 错误信息 | 112| ------- | -------------------------------- | 113| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 114 115**示例:** 116 117```ts 118import { common, UIAbility } from '@kit.AbilityKit'; 119import { BusinessError } from '@kit.BasicServicesKit'; 120 121export default class EntryAbility extends UIAbility { 122 onCreate() { 123 console.log('MyAbility onCreate'); 124 let applicationContext: common.Context; 125 try { 126 applicationContext = this.context.getApplicationContext(); 127 } catch (error) { 128 console.error(`getApplicationContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 129 } 130 } 131} 132``` 133 134## Context.getGroupDir<sup>10+</sup> 135 136getGroupDir(dataGroupID: string): Promise\<string> 137 138通过使用应用中的Group ID获取对应的共享目录,使用Promise异步回调。 139 140**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 141 142**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 143 144**参数:** 145 146| 参数名 | 类型 | 必填 | 说明 | 147| -------- | ---------------------- | ---- | ------------- | 148| dataGroupID | string | 是 | 原子化服务应用项目创建时,系统会指定分配唯一Group ID。 | 149 150**返回值:** 151 152| 类型 | 说明 | 153| -------- | -------- | 154| Promise\<string> | 以Promise方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。| 155 156**错误码:** 157 158以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 159 160| 错误码ID | 错误信息 | 161| ------- | -------------------------------- | 162| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 163| 16000011 | The context does not exist. | 164 165**示例:** 166 167```ts 168import { common, UIAbility } from '@kit.AbilityKit'; 169import { BusinessError } from '@kit.BasicServicesKit'; 170 171export default class EntryAbility extends UIAbility { 172 onCreate() { 173 console.log('MyAbility onCreate'); 174 let groupId = "1"; 175 let getGroupDirContext: common.Context = this.context; 176 try { 177 getGroupDirContext.getGroupDir(groupId).then(data => { 178 console.log("getGroupDir result:" + data); 179 }) 180 } catch (error) { 181 console.error(`getGroupDirContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 182 } 183 } 184} 185``` 186 187## Context.getGroupDir<sup>10+</sup> 188 189getGroupDir(dataGroupID: string, callback: AsyncCallback\<string>): void 190 191通过使用应用中的Group ID获取对应的共享目录,使用callback异步回调。 192 193**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 194 195**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 196 197**参数:** 198 199| 参数名 | 类型 | 必填 | 说明 | 200| -------- | ---------------------- | ---- | ------------- | 201| dataGroupID | string | 是 | 原子化服务应用项目创建时,系统会指定分配唯一Group ID。 | 202| callback | AsyncCallback\<string> | 是 | 以callback方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。| 203 204**错误码:** 205 206以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 207 208| 错误码ID | 错误信息 | 209| ------- | -------------------------------- | 210| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 211| 16000011 | The context does not exist. | 212 213**示例:** 214 215```ts 216import { common, UIAbility } from '@kit.AbilityKit'; 217import { BusinessError } from '@kit.BasicServicesKit'; 218 219export default class EntryAbility extends UIAbility { 220 onCreate() { 221 console.log('MyAbility onCreate'); 222 let getGroupDirContext: common.Context = this.context; 223 224 getGroupDirContext.getGroupDir("1", (err: BusinessError, data) => { 225 if (err) { 226 console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`); 227 } else { 228 console.log(`getGroupDir result is: ${JSON.stringify(data)}`); 229 } 230 }); 231 } 232} 233``` 234