1# Context (Stage模型的上下文基类) 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: Ability--> 5<!--Owner: @zexin_c--> 6<!--Designer: @li-weifeng2--> 7<!--Tester: @lixueqing513--> 8<!--Adviser: @huipeizi--> 9 10Context是Stage模型的上下文基类,主要用于访问特定应用程序的资源,以及执行应用级操作的回调。 11 12> **说明:** 13> 14> - 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 15> - 本模块接口仅可在Stage模型下使用。 16 17## 不同类型Context的继承和持有关系 18- 不同类型Context的继承关系如下: 19 20  21 22- 不同类型Context的持有关系如下: 23 24  25 26> **说明** 27> 28> [UIContext](../../reference/apis-arkui/arkts-apis-uicontext-uicontext.md)是指UI实例上下文,用于关联窗口与UI页面。与本文档中的应用上下文Context无直接关联,不存在继承或持有关系。 29 30## 导入模块 31 32```ts 33import { common } from '@kit.AbilityKit'; 34``` 35 36## Context 37 38Context提供了ability或application的上下文的能力,包括访问特定应用程序的资源等。 39 40### 属性 41 42**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 43 44| 名称 | 类型 | 只读 | 可选 | 说明 | 45|---------------------| ------ | ---- | ---- |------------------------------------------------------------------| 46| resourceManager | resmgr.[ResourceManager](../apis-localization-kit/js-apis-resource-manager.md#resourcemanager) | 否 | 否 | 资源管理对象。<br>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 47| applicationInfo | [ApplicationInfo](js-apis-bundleManager-applicationInfo.md) | 否 | 否 | 当前应用程序的信息。 <br>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 48| cacheDir | string | 否 | 否 | 缓存目录,详情参考[应用沙箱目录](../../file-management/app-sandbox-directory.md)。<br>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 49| tempDir | string | 否 | 否 | 临时目录,详情参考[应用沙箱目录](../../file-management/app-sandbox-directory.md)。<br/>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 50| resourceDir<sup>11+<sup> | string | 否 | 否 | 资源目录。<br>**说明**:需要开发者手动在`\<module-name>\resource`路径下创建`resfile`目录。创建的`resfile`目录仅支持以只读方式访问。<br>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 51| filesDir | string | 否 | 否 | 文件目录,详情参考[应用沙箱目录](../../file-management/app-sandbox-directory.md)。<br/>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 52| databaseDir | string | 否 | 否 | 数据库目录,详情参考[应用沙箱目录](../../file-management/app-sandbox-directory.md)。<br/>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 53| preferencesDir | string | 否 | 否 | preferences目录,详情参考[应用沙箱目录](../../file-management/app-sandbox-directory.md)。<br/>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 54| bundleCodeDir | string | 否 | 否 | 安装包目录。不能拼接路径访问资源文件,请使用[资源管理接口](../apis-localization-kit/js-apis-resource-manager.md)访问资源,详情参考[应用沙箱目录](../../file-management/app-sandbox-directory.md)。<br/>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 55| distributedFilesDir | string | 否 | 否 | 分布式文件目录,详情参考[应用沙箱目录](../../file-management/app-sandbox-directory.md)。<br/>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 56| cloudFileDir<sup>12+</sup> | string | 否 | 否 | 云文件目录。<br>**原子化服务API**:从API version 12开始,该接口支持在原子化服务中使用。 | 57| eventHub | [EventHub](js-apis-inner-application-eventHub.md) | 否 | 否 | 事件中心,提供订阅、取消订阅、触发事件对象。<br>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 58| area | contextConstant.[AreaMode](js-apis-app-ability-contextConstant.md#areamode) | 否 | 否 | 文件分区信息,按加密等级[AreaMode](js-apis-app-ability-contextConstant.md#areamode) 进行分区。<br>**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 | 59| processName<sup>18+</sup> | string | 否 | 否 | 当前应用的进程名。<br/>**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 | 60 61### createModuleContext<sup>(deprecated)</sup> 62 63createModuleContext(moduleName: string): Context 64 65根据模块名创建上下文。 66 67> **说明:** 68> 69> - 仅支持获取本应用中其他Module的Context和应用内HSP的Context,不支持获取其他应用的Context。 70> 71> - 从 API Version 12 开始废弃,建议使用[application.createModuleContext](./js-apis-app-ability-application.md#applicationcreatemodulecontext12)替代,否则可能导致资源获取异常。 72 73**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 74 75**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 76 77**参数:** 78 79| 参数名 | 类型 | 必填 | 说明 | 80| -------- | ---------------------- | ---- | ------------- | 81| moduleName | string | 是 | 模块名。 | 82 83**返回值:** 84 85| 类型 | 说明 | 86| -------- | -------- | 87| Context | 模块的上下文。 | 88 89**错误码**: 90 91以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 92 93| 错误码ID | 错误信息 | 94| ------- | -------------------------------- | 95| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 96 97**示例:** 98 99```ts 100import { common, UIAbility } from '@kit.AbilityKit'; 101import { BusinessError } from '@kit.BasicServicesKit'; 102 103export default class EntryAbility extends UIAbility { 104 onCreate() { 105 console.log('MyAbility onCreate'); 106 let moduleContext: common.Context; 107 try { 108 moduleContext = this.context.createModuleContext('entry'); 109 } catch (error) { 110 console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 111 } 112 } 113} 114``` 115 116### getApplicationContext 117 118getApplicationContext(): ApplicationContext 119 120获取当前应用上下文。 121 122**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 123 124**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 125 126**返回值:** 127 128| 类型 | 说明 | 129| -------- | -------- | 130| [ApplicationContext](js-apis-inner-application-applicationContext.md) | 应用上下文。 | 131 132**错误码**: 133 134以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 135 136| 错误码ID | 错误信息 | 137| ------- | -------------------------------- | 138| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 139 140**示例:** 141 142```ts 143import { common, UIAbility } from '@kit.AbilityKit'; 144import { BusinessError } from '@kit.BasicServicesKit'; 145 146export default class EntryAbility extends UIAbility { 147 onCreate() { 148 console.log('MyAbility onCreate'); 149 let applicationContext: common.Context; 150 try { 151 applicationContext = this.context.getApplicationContext(); 152 } catch (error) { 153 console.error(`getApplicationContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 154 } 155 } 156} 157``` 158 159### getGroupDir<sup>10+</sup> 160 161getGroupDir(dataGroupID: string): Promise\<string> 162 163通过应用中的Group ID获取对应的共享目录,使用Promise异步回调。 164 165**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 166 167**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 168 169**参数:** 170 171| 参数名 | 类型 | 必填 | 说明 | 172| -------- | ---------------------- | ---- | ------------- | 173| [dataGroupID](../apis-arkdata/js-apis-data-preferences.md#options10) | string | 是 | 原子化服务类型的应用创建时,系统会指定分配唯一Group ID。 | 174 175**返回值:** 176 177| 类型 | 说明 | 178| -------- | -------- | 179| Promise\<string> | 以Promise方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。| 180 181**错误码:** 182 183以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 184 185| 错误码ID | 错误信息 | 186| ------- | -------------------------------- | 187| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 188| 16000011 | The context does not exist. | 189 190**示例:** 191 192```ts 193import { common, UIAbility } from '@kit.AbilityKit'; 194import { BusinessError } from '@kit.BasicServicesKit'; 195 196export default class EntryAbility extends UIAbility { 197 onCreate() { 198 console.log('MyAbility onCreate'); 199 let groupId = "1"; 200 let getGroupDirContext: common.Context = this.context; 201 try { 202 getGroupDirContext.getGroupDir(groupId).then(data => { 203 console.log("getGroupDir result:" + data); 204 }) 205 } catch (error) { 206 console.error(`getGroupDirContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 207 } 208 } 209} 210``` 211 212### getGroupDir<sup>10+</sup> 213 214getGroupDir(dataGroupID: string, callback: AsyncCallback\<string>): void 215 216通过应用中的Group ID获取对应的共享目录,使用callback异步回调。 217 218**原子化服务API**:从API version 11开始,该接口支持在原子化服务中使用。 219 220**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 221 222**参数:** 223 224| 参数名 | 类型 | 必填 | 说明 | 225| -------- | ---------------------- | ---- | ------------- | 226| [dataGroupID](../apis-arkdata/js-apis-data-preferences.md#options10) | string | 是 | 原子化服务类型的应用创建时,系统会指定分配唯一Group ID。 | 227| callback | AsyncCallback\<string> | 是 | 以callback方式返回对应的共享目录。如果不存在则返回为空,仅支持应用el2加密级别。| 228 229**错误码:** 230 231以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)和[元能力子系统错误码](errorcode-ability.md)。 232 233| 错误码ID | 错误信息 | 234| ------- | -------------------------------- | 235| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 236| 16000011 | The context does not exist. | 237 238**示例:** 239 240```ts 241import { common, UIAbility } from '@kit.AbilityKit'; 242import { BusinessError } from '@kit.BasicServicesKit'; 243 244export default class EntryAbility extends UIAbility { 245 onCreate() { 246 console.log('MyAbility onCreate'); 247 let getGroupDirContext: common.Context = this.context; 248 249 getGroupDirContext.getGroupDir("1", (err: BusinessError, data) => { 250 if (err) { 251 console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`); 252 } else { 253 console.log(`getGroupDir result is: ${JSON.stringify(data)}`); 254 } 255 }); 256 } 257} 258``` 259 260### createAreaModeContext<sup>18+</sup> 261 262createAreaModeContext(areaMode: contextConstant.AreaMode): Context 263 264创建特定数据加密级别的应用上下文。开发者可以调用该接口创建不同加密级别的上下文,从而获取对应的沙箱路径。 265 266**原子化服务API**:从API version 18开始,该接口支持在原子化服务中使用。 267 268**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 269 270**参数:** 271 272| 参数名 | 类型 | 必填 | 说明 | 273| -------- | ------------------------------------------------------------ | ---- | ------------------------ | 274| areaMode | [contextConstant.AreaMode](js-apis-app-ability-contextConstant.md#areamode) | 是 | 指定的数据加密等级。 | 275 276**返回值:** 277 278| 类型 | 说明 | 279| ------- | ---------------------- | 280| Context | 指定数据加密等级的上下文。 | 281 282**示例:** 283 284```ts 285import { common, UIAbility, contextConstant } from '@kit.AbilityKit'; 286import { hilog } from '@kit.PerformanceAnalysisKit'; 287 288export default class EntryAbility extends UIAbility { 289 onCreate() { 290 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 291 let areaMode: contextConstant.AreaMode = contextConstant.AreaMode.EL2; 292 let areaModeContext: common.Context; 293 try { 294 areaModeContext = this.context.createAreaModeContext(areaMode); 295 } catch (error) { 296 hilog.error(0x0000, 'testTag', 'createAreaModeContext error is:%{public}s', JSON.stringify(error)); 297 } 298 } 299} 300``` 301 302### createDisplayContext<sup>15+</sup> 303 304createDisplayContext(displayId: number): Context 305 306根据指定的物理屏幕ID创建带有屏幕信息(包括屏幕密度[ScreenDensity](../apis-localization-kit/js-apis-resource-manager.md#screendensity)和屏幕方向[Direction](../apis-localization-kit/js-apis-resource-manager.md#direction))的应用上下文。 307 308**原子化服务API**:从API version 15开始,该接口支持在原子化服务中使用。 309 310**系统能力**:SystemCapability.Ability.AbilityRuntime.Core 311 312**参数:** 313 314| 参数名 | 类型 | 必填 | 说明 | 315| -------- | ------------------------------------------------------------ | ---- | ------------------------ | 316| [displayId](../apis-arkui/arkts-apis-window-i.md#windowproperties) | number | 是 | 物理屏幕ID。 | 317 318**返回值:** 319 320| 类型 | 说明 | 321| ------- | ---------------------- | 322| [Context](#context) | 带有指定物理屏幕信息的上下文。 | 323 324**错误码:** 325 326以下错误码详细介绍请参考[通用错误码](../errorcode-universal.md)。 327 328| 错误码ID | 错误信息 | 329| -------- | ------------------------------------------------------------ | 330| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 331 332**示例:** 333 334```ts 335import { common, UIAbility } from '@kit.AbilityKit'; 336import { hilog } from '@kit.PerformanceAnalysisKit'; 337 338export default class EntryAbility extends UIAbility { 339 onCreate() { 340 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 341 let displayContext: common.Context; 342 try { 343 displayContext = this.context.createDisplayContext(0); 344 } catch (error) { 345 hilog.error(0x0000, 'testTag', 'createDisplayContext error is:%{public}s', JSON.stringify(error)); 346 } 347 } 348} 349``` 350