1# Context (Context Base Class of the Stage Model) 2 3The Context module inherits from [BaseContext](js-apis-inner-application-baseContext.md). It provides context for abilities or applications, including access to application-specific resources. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> - The APIs of this module can be used only in the stage model. 9 10## Inheritance and Holding Relationships of Different Context Types 11- Inheritance relationships among different types of context 12 13  14 15- Holding relationships among different types of context 16 17  18 19> **NOTE** 20> 21> [UIContext](../../reference/apis-arkui/arkts-apis-uicontext-uicontext.md) refers to the context of a UI instance, which is used to associate windows with UI pages. It is not directly related to the application context discussed in this topic and does not involve inheritance or holding relationships. 22 23## Modules to Import 24 25```ts 26import { common } from '@kit.AbilityKit'; 27``` 28 29## Context 30 31Represents the context for the ability or application. It allows access to application-specific resources. 32 33### Properties 34 35**System capability**: SystemCapability.Ability.AbilityRuntime.Core 36 37| Name | Type | Read-only | Optional | Description | 38|---------------------| ------ | ---- | ---- |------------------------------------------------------------------| 39| resourceManager | resmgr.[ResourceManager](../apis-localization-kit/js-apis-resource-manager.md#resourcemanager) | No | No | Object for resource management.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 40| applicationInfo | [ApplicationInfo](js-apis-bundleManager-applicationInfo.md) | No | No | Application information.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 41| cacheDir | string | No | No | Cache directory. For details, see [Application Sandbox](../../file-management/app-sandbox-directory.md).<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 42| tempDir | string | No | No | Temporary directory. For details, see [Application Sandbox](../../file-management/app-sandbox-directory.md).<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 43| resourceDir<sup>11+<sup> | string | No | No | Resource directory.<br>Note: You are required to manually create the **resfile** directory in **\<module-name>\resource**. The **resfile** directory can be accessed only in read-only mode.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 44| filesDir | string | No | No | File directory. For details, see [Application Sandbox](../../file-management/app-sandbox-directory.md).<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 45| databaseDir | string | No | No | Database directory. For details, see [Application Sandbox](../../file-management/app-sandbox-directory.md).<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 46| preferencesDir | string | No | No | Preferences directory. For details, see [Application Sandbox](../../file-management/app-sandbox-directory.md).<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 47| bundleCodeDir | string | No | No | Bundle code directory. Do not access resource files using concatenated paths. Use [resource manager APIs](../apis-localization-kit/js-apis-resource-manager.md) instead. For details, see [Application Sandbox](../../file-management/app-sandbox-directory.md).<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 48| distributedFilesDir | string | No | No | Distributed file directory. For details, see [Application Sandbox](../../file-management/app-sandbox-directory.md).<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 49| cloudFileDir<sup>12+</sup> | string | No | No | Cloud file directory.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 50| eventHub | [EventHub](js-apis-inner-application-eventHub.md) | No | No | Event hub that implements event subscription, unsubscription, and triggering.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 51| area | contextConstant.[AreaMode](js-apis-app-ability-contextConstant.md#areamode) | No | No | Information about file partitions, which are divided according to the encryption level specified by [AreaMode](js-apis-app-ability-contextConstant.md#areamode).<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 52| processName<sup>18+</sup> | string | No | No| Process name of the current application.<br>**Atomic service API**: This API can be used in atomic services since API version 18.| 53 54### createModuleContext<sup>(deprecated)</sup> 55 56createModuleContext(moduleName: string): Context 57 58Creates the context based on the module name. 59 60> **NOTE** 61> 62> - Only the context of other modules in the current application and the context of the intra-application HSP can be obtained. The context of other applications cannot be obtained. 63> 64> - This API is deprecated since API version 12. You are advised to use [application.createModuleContext](./js-apis-app-ability-application.md#applicationcreatemodulecontext12) instead. Otherwise, resource retrieval may be abnormal. 65 66**Atomic service API**: This API can be used in atomic services since API version 11. 67 68**System capability**: SystemCapability.Ability.AbilityRuntime.Core 69 70**Parameters** 71 72| Name | Type | Mandatory | Description | 73| -------- | ---------------------- | ---- | ------------- | 74| moduleName | string | Yes | Module name.| 75 76**Return value** 77 78| Type| Description| 79| -------- | -------- | 80| Context | Context created.| 81 82**Error codes** 83 84For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 85 86| ID| Error Message| 87| ------- | -------------------------------- | 88| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 89 90**Example** 91 92```ts 93import { common, UIAbility } from '@kit.AbilityKit'; 94import { BusinessError } from '@kit.BasicServicesKit'; 95 96export default class EntryAbility extends UIAbility { 97 onCreate() { 98 console.log('MyAbility onCreate'); 99 let moduleContext: common.Context; 100 try { 101 moduleContext = this.context.createModuleContext('entry'); 102 } catch (error) { 103 console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 104 } 105 } 106} 107``` 108 109### getApplicationContext 110 111getApplicationContext(): ApplicationContext 112 113Obtains the context of this application. 114 115**Atomic service API**: This API can be used in atomic services since API version 11. 116 117**System capability**: SystemCapability.Ability.AbilityRuntime.Core 118 119**Return value** 120 121| Type| Description| 122| -------- | -------- | 123| [ApplicationContext](js-apis-inner-application-applicationContext.md) | Application context obtained.| 124 125**Error codes** 126 127For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 128 129| ID| Error Message| 130| ------- | -------------------------------- | 131| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 132 133**Example** 134 135```ts 136import { common, UIAbility } from '@kit.AbilityKit'; 137import { BusinessError } from '@kit.BasicServicesKit'; 138 139export default class EntryAbility extends UIAbility { 140 onCreate() { 141 console.log('MyAbility onCreate'); 142 let applicationContext: common.Context; 143 try { 144 applicationContext = this.context.getApplicationContext(); 145 } catch (error) { 146 console.error(`getApplicationContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 147 } 148 } 149} 150``` 151 152### getGroupDir<sup>10+</sup> 153 154getGroupDir(dataGroupID: string): Promise\<string> 155 156Obtains the shared directory based on a group ID. This API uses a promise to return the result. 157 158**Atomic service API**: This API can be used in atomic services since API version 11. 159 160**System capability**: SystemCapability.Ability.AbilityRuntime.Core 161 162**Parameters** 163 164| Name | Type | Mandatory | Description | 165| -------- | ---------------------- | ---- | ------------- | 166| dataGroupID | string | Yes | Group ID, which is assigned by the system when an atomic service project is created.| 167 168**Return value** 169 170| Type| Description| 171| -------- | -------- | 172| Promise\<string> | Promise used to return the result. If no shared directory exists, null is returned. Only the encryption level EL2 is supported.| 173 174**Error codes** 175 176For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 177 178| ID| Error Message| 179| ------- | -------------------------------- | 180| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 181| 16000011 | The context does not exist. | 182 183**Example** 184 185```ts 186import { common, UIAbility } from '@kit.AbilityKit'; 187import { BusinessError } from '@kit.BasicServicesKit'; 188 189export default class EntryAbility extends UIAbility { 190 onCreate() { 191 console.log('MyAbility onCreate'); 192 let groupId = "1"; 193 let getGroupDirContext: common.Context = this.context; 194 try { 195 getGroupDirContext.getGroupDir(groupId).then(data => { 196 console.log("getGroupDir result:" + data); 197 }) 198 } catch (error) { 199 console.error(`getGroupDirContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 200 } 201 } 202} 203``` 204 205### getGroupDir<sup>10+</sup> 206 207getGroupDir(dataGroupID: string, callback: AsyncCallback\<string>): void 208 209Obtains the shared directory based on a group ID. This API uses an asynchronous callback to return the result. 210 211**Atomic service API**: This API can be used in atomic services since API version 11. 212 213**System capability**: SystemCapability.Ability.AbilityRuntime.Core 214 215**Parameters** 216 217| Name | Type | Mandatory | Description | 218| -------- | ---------------------- | ---- | ------------- | 219| dataGroupID | string | Yes | Group ID, which is assigned by the system when an atomic service project is created.| 220| callback | AsyncCallback\<string> | Yes | Callback used to return the result. If no shared directory exists, null is returned. Only the encryption level EL2 is supported.| 221 222**Error codes** 223 224For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 225 226| ID| Error Message| 227| ------- | -------------------------------- | 228| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 229| 16000011 | The context does not exist. | 230 231**Example** 232 233```ts 234import { common, UIAbility } from '@kit.AbilityKit'; 235import { BusinessError } from '@kit.BasicServicesKit'; 236 237export default class EntryAbility extends UIAbility { 238 onCreate() { 239 console.log('MyAbility onCreate'); 240 let getGroupDirContext: common.Context = this.context; 241 242 getGroupDirContext.getGroupDir("1", (err: BusinessError, data) => { 243 if (err) { 244 console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`); 245 } else { 246 console.log(`getGroupDir result is: ${JSON.stringify(data)}`); 247 } 248 }); 249 } 250} 251``` 252 253### createAreaModeContext<sup>18+</sup> 254 255createAreaModeContext(areaMode: contextConstant.AreaMode): Context 256 257Creates the context for this application based on a data encryption level. This is required when an application needs to store different types of information in different directories. The application can obtain the corresponding directory. 258 259**Atomic service API**: This API can be used in atomic services since API version 18. 260 261**System capability**: SystemCapability.Ability.AbilityRuntime.Core 262 263**Parameters** 264 265| Name | Type | Mandatory| Description | 266| -------- | ------------------------------------------------------------ | ---- | ------------------------ | 267| areaMode | [contextConstant.AreaMode](js-apis-app-ability-contextConstant.md#areamode) | Yes | Data encryption level.| 268 269**Return value** 270 271| Type | Description | 272| ------- | ---------------------- | 273| Context | Context created based on the data encryption level.| 274 275**Example** 276 277```ts 278import { common, UIAbility, contextConstant } from '@kit.AbilityKit'; 279import { hilog } from '@kit.PerformanceAnalysisKit'; 280 281export default class EntryAbility extends UIAbility { 282 onCreate() { 283 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 284 let areaMode: contextConstant.AreaMode = contextConstant.AreaMode.EL2; 285 let areaModeContext: common.Context; 286 try { 287 areaModeContext = this.context.createAreaModeContext(areaMode); 288 } catch (error) { 289 hilog.error(0x0000, 'testTag', 'createAreaModeContext error is:%{public}s', JSON.stringify(error)); 290 } 291 } 292} 293``` 294 295### createDisplayContext<sup>15+</sup> 296 297createDisplayContext(displayId: number): Context 298 299Creates the context based on the specified display ID, so as to obtain and use other application contexts with screen information (including [ScreenDensity](../apis-localization-kit/js-apis-resource-manager.md#screendensity) and [Direction](../apis-localization-kit/js-apis-resource-manager.md#direction)). 300 301**Atomic service API**: This API can be used in atomic services since API version 15. 302 303**System capability**: SystemCapability.Ability.AbilityRuntime.Core 304 305**Parameters** 306 307| Name | Type | Mandatory| Description | 308| -------- | ------------------------------------------------------------ | ---- | ------------------------ | 309| displayId | number | Yes | Display ID.| 310 311**Return value** 312 313| Type | Description | 314| ------- | ---------------------- | 315| [Context](#context) | Context with the specified screen information.| 316 317**Error codes** 318 319For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 320 321| ID| Error Message | 322| -------- | ------------------------------------------------------------ | 323| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 324 325**Example** 326 327```ts 328import { common, UIAbility } from '@kit.AbilityKit'; 329import { hilog } from '@kit.PerformanceAnalysisKit'; 330 331export default class EntryAbility extends UIAbility { 332 onCreate() { 333 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 334 let displayContext: common.Context; 335 try { 336 displayContext = this.context.createDisplayContext(0); 337 } catch (error) { 338 hilog.error(0x0000, 'testTag', 'createDisplayContext error is:%{public}s', JSON.stringify(error)); 339 } 340 } 341} 342``` 343