1# Context 2 3The Context module, inherited frome [BaseContext](js-apis-inner-application-baseContext.md), 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## Modules to Import 11 12```ts 13import { common } from '@kit.AbilityKit'; 14``` 15 16## Properties 17 18**System capability**: SystemCapability.Ability.AbilityRuntime.Core 19 20| Name | Type | Read-only | Optional | Description | 21|---------------------| ------ | ---- | ---- |------------------------------------------------------------------| 22| 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.| 23| 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.| 24| cacheDir | string | No | No | Cache directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 25| tempDir | string | No | No | Temporary directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 26| 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.| 27| filesDir | string | No | No | File directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 28| databaseDir | string | No | No | Database directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 29| preferencesDir | string | No | No | Preferences directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 30| bundleCodeDir | string | No | No | Bundle code directory. Do not access resource files using concatenated paths. Use [@ohos.resourceManager](../apis-localization-kit/js-apis-resource-manager.md) instead.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 31| distributedFilesDir | string | No | No | Distributed file directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 32| 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. | 33| 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.| 34| area | contextConstant.[AreaMode](js-apis-app-ability-contextConstant.md) | No | No | Encryption level of the directory.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 35| 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.| 36 37## Context.createModuleContext<sup>(deprecated)</sup> 38 39createModuleContext(moduleName: string): Context 40 41Creates the context based on the module name. 42 43> **NOTE** 44> 45> This API is deprecated since API version 12. You are advised to use [application.createModuleContext](./js-apis-app-ability-application.md#applicationcreatemodulecontext12) instead. 46 47**Atomic service API**: This API can be used in atomic services since API version 11. 48 49**System capability**: SystemCapability.Ability.AbilityRuntime.Core 50 51**Parameters** 52 53| Name | Type | Mandatory | Description | 54| -------- | ---------------------- | ---- | ------------- | 55| moduleName | string | Yes | Module name.| 56 57**Return value** 58 59| Type| Description| 60| -------- | -------- | 61| Context | Context created.| 62 63**Error codes** 64 65For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 66 67| ID| Error Message| 68| ------- | -------------------------------- | 69| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 70 71**Example** 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> **NOTE** 91> 92> 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. 93 94## Context.getApplicationContext 95 96getApplicationContext(): ApplicationContext 97 98Obtains the context of this application. 99 100**Atomic service API**: This API can be used in atomic services since API version 11. 101 102**System capability**: SystemCapability.Ability.AbilityRuntime.Core 103 104**Return value** 105 106| Type| Description| 107| -------- | -------- | 108| [ApplicationContext](js-apis-inner-application-applicationContext.md) | Application context obtained.| 109 110**Error codes** 111 112For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 113 114| ID| Error Message| 115| ------- | -------------------------------- | 116| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 117 118**Example** 119 120```ts 121import { common, UIAbility } from '@kit.AbilityKit'; 122import { BusinessError } from '@kit.BasicServicesKit'; 123 124export default class EntryAbility extends UIAbility { 125 onCreate() { 126 console.log('MyAbility onCreate'); 127 let applicationContext: common.Context; 128 try { 129 applicationContext = this.context.getApplicationContext(); 130 } catch (error) { 131 console.error(`getApplicationContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 132 } 133 } 134} 135``` 136 137## Context.getGroupDir<sup>10+</sup> 138 139getGroupDir(dataGroupID: string): Promise\<string> 140 141Obtains the shared directory based on a group ID. This API uses a promise to return the result. 142 143**Atomic service API**: This API can be used in atomic services since API version 11. 144 145**System capability**: SystemCapability.Ability.AbilityRuntime.Core 146 147**Parameters** 148 149| Name | Type | Mandatory | Description | 150| -------- | ---------------------- | ---- | ------------- | 151| dataGroupID | string | Yes | Group ID, which is assigned by the system when an atomic service project is created.| 152 153**Return value** 154 155| Type| Description| 156| -------- | -------- | 157| Promise\<string> | Promise used to return the result. If no shared directory exists, null is returned. Only the encryption level EL2 is supported.| 158 159**Error codes** 160 161For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 162 163| ID| Error Message| 164| ------- | -------------------------------- | 165| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 166| 16000011 | The context does not exist. | 167 168**Example** 169 170```ts 171import { common, UIAbility } from '@kit.AbilityKit'; 172import { BusinessError } from '@kit.BasicServicesKit'; 173 174export default class EntryAbility extends UIAbility { 175 onCreate() { 176 console.log('MyAbility onCreate'); 177 let groupId = "1"; 178 let getGroupDirContext: common.Context = this.context; 179 try { 180 getGroupDirContext.getGroupDir(groupId).then(data => { 181 console.log("getGroupDir result:" + data); 182 }) 183 } catch (error) { 184 console.error(`getGroupDirContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 185 } 186 } 187} 188``` 189 190## Context.getGroupDir<sup>10+</sup> 191 192getGroupDir(dataGroupID: string, callback: AsyncCallback\<string>): void 193 194Obtains the shared directory based on a group ID. This API uses an asynchronous callback to return the result. 195 196**Atomic service API**: This API can be used in atomic services since API version 11. 197 198**System capability**: SystemCapability.Ability.AbilityRuntime.Core 199 200**Parameters** 201 202| Name | Type | Mandatory | Description | 203| -------- | ---------------------- | ---- | ------------- | 204| dataGroupID | string | Yes | Group ID, which is assigned by the system when an atomic service project is created.| 205| 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.| 206 207**Error codes** 208 209For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 210 211| ID| Error Message| 212| ------- | -------------------------------- | 213| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 214| 16000011 | The context does not exist. | 215 216**Example** 217 218```ts 219import { common, UIAbility } from '@kit.AbilityKit'; 220import { BusinessError } from '@kit.BasicServicesKit'; 221 222export default class EntryAbility extends UIAbility { 223 onCreate() { 224 console.log('MyAbility onCreate'); 225 let getGroupDirContext: common.Context = this.context; 226 227 getGroupDirContext.getGroupDir("1", (err: BusinessError, data) => { 228 if (err) { 229 console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`); 230 } else { 231 console.log(`getGroupDir result is: ${JSON.stringify(data)}`); 232 } 233 }); 234 } 235} 236``` 237 238## Context.createAreaModeContext<sup>18+</sup> 239 240createAreaModeContext(areaMode: contextConstant.AreaMode): Context 241 242Creates 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. 243 244**Atomic service API**: This API can be used in atomic services since API version 18. 245 246**System capability**: SystemCapability.Ability.AbilityRuntime.Core 247 248**Parameters** 249 250| Name | Type | Mandatory| Description | 251| -------- | ------------------------------------------------------------ | ---- | ------------------------ | 252| areaMode | [contextConstant.AreaMode](js-apis-app-ability-contextConstant.md#areamode) | Yes | Data encryption level.| 253 254**Return value** 255 256| Type | Description | 257| ------- | ---------------------- | 258| Context | Context created based on the data encryption level.| 259 260**Error codes** 261 262For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 263 264| ID| Error Message | 265| -------- | ------------------------------------------------------------ | 266| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 267 268**Example** 269 270```ts 271import { common, UIAbility, contextConstant } from '@kit.AbilityKit'; 272import { hilog } from '@kit.PerformanceAnalysisKit'; 273 274export default class EntryAbility extends UIAbility { 275 onCreate() { 276 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 277 let areaMode: contextConstant.AreaMode = contextConstant.AreaMode.EL2; 278 let areaModeContext: common.Context; 279 try { 280 areaModeContext = this.context.createAreaModeContext(areaMode); 281 } catch (error) { 282 hilog.error(0x0000, 'testTag', 'createAreaModeContext error is:%{public}s', JSON.stringify(error)); 283 } 284 } 285} 286``` 287 288## Context.createDisplayContext<sup>15+</sup> 289 290createDisplayContext(displayId: number): Context 291 292Creates 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)). 293 294**Atomic service API**: This API can be used in atomic services since API version 15. 295 296**System capability**: SystemCapability.Ability.AbilityRuntime.Core 297 298**Parameters** 299 300| Name | Type | Mandatory| Description | 301| -------- | ------------------------------------------------------------ | ---- | ------------------------ | 302| displayId | number | Yes | Display ID.| 303 304**Return value** 305 306| Type | Description | 307| ------- | ---------------------- | 308| [Context](#context) | Context with the specified screen information.| 309 310**Error codes** 311 312For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 313 314| ID| Error Message | 315| -------- | ------------------------------------------------------------ | 316| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 317 318**Example** 319 320```ts 321import { common, UIAbility } from '@kit.AbilityKit'; 322import { hilog } from '@kit.PerformanceAnalysisKit'; 323 324export default class EntryAbility extends UIAbility { 325 onCreate() { 326 hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 327 let displayContext: common.Context; 328 try { 329 displayContext = this.context.createDisplayContext(0); 330 } catch (error) { 331 hilog.error(0x0000, 'testTag', 'createDisplayContext error is:%{public}s', JSON.stringify(error)); 332 } 333 } 334} 335``` 336