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