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>**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 36## Context.createModuleContext<sup>(deprecated)</sup> 37 38createModuleContext(moduleName: string): Context 39 40Creates the context based on the module name. 41 42> **NOTE** 43> 44> This API is deprecated since API version 12. You are advised to use [application.createModuleContext](./js-apis-app-ability-application.md#applicationcreatemodulecontext12) instead. 45 46**Atomic service API**: This API can be used in atomic services since API version 11. 47 48**System capability**: SystemCapability.Ability.AbilityRuntime.Core 49 50**Parameters** 51 52| Name | Type | Mandatory | Description | 53| -------- | ---------------------- | ---- | ------------- | 54| moduleName | string | Yes | Module name.| 55 56**Return value** 57 58| Type| Description| 59| -------- | -------- | 60| Context | Context created.| 61 62**Error codes** 63 64For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 65 66| ID| Error Message| 67| ------- | -------------------------------- | 68| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 69 70**Example** 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> **NOTE** 90> 91> 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. 92 93## Context.getApplicationContext 94 95getApplicationContext(): ApplicationContext 96 97Obtains the context of this application. 98 99**Atomic service API**: This API can be used in atomic services since API version 11. 100 101**System capability**: SystemCapability.Ability.AbilityRuntime.Core 102 103**Return value** 104 105| Type| Description| 106| -------- | -------- | 107| [ApplicationContext](js-apis-inner-application-applicationContext.md) | Application context obtained.| 108 109**Error codes** 110 111For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 112 113| ID| Error Message| 114| ------- | -------------------------------- | 115| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 116 117**Example** 118 119```ts 120import { common, UIAbility } from '@kit.AbilityKit'; 121import { BusinessError } from '@kit.BasicServicesKit'; 122 123export default class EntryAbility extends UIAbility { 124 onCreate() { 125 console.log('MyAbility onCreate'); 126 let applicationContext: common.Context; 127 try { 128 applicationContext = this.context.getApplicationContext(); 129 } catch (error) { 130 console.error(`getApplicationContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 131 } 132 } 133} 134``` 135 136## Context.getGroupDir<sup>10+</sup> 137 138getGroupDir(dataGroupID: string): Promise\<string> 139 140Obtains the shared directory based on a group ID. This API uses a promise to return the result. 141 142**Atomic service API**: This API can be used in atomic services since API version 11. 143 144**System capability**: SystemCapability.Ability.AbilityRuntime.Core 145 146**Parameters** 147 148| Name | Type | Mandatory | Description | 149| -------- | ---------------------- | ---- | ------------- | 150| dataGroupID | string | Yes | Group ID, which is assigned by the system when an atomic service project is created.| 151 152**Return value** 153 154| Type| Description| 155| -------- | -------- | 156| Promise\<string> | Promise used to return the result. If no shared directory exists, null is returned. Only the encryption level EL2 is supported.| 157 158**Error codes** 159 160For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 161 162| ID| Error Message| 163| ------- | -------------------------------- | 164| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 165| 16000011 | The context does not exist. | 166 167**Example** 168 169```ts 170import { common, UIAbility } from '@kit.AbilityKit'; 171import { BusinessError } from '@kit.BasicServicesKit'; 172 173export default class EntryAbility extends UIAbility { 174 onCreate() { 175 console.log('MyAbility onCreate'); 176 let groupId = "1"; 177 let getGroupDirContext: common.Context = this.context; 178 try { 179 getGroupDirContext.getGroupDir(groupId).then(data => { 180 console.log("getGroupDir result:" + data); 181 }) 182 } catch (error) { 183 console.error(`getGroupDirContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`); 184 } 185 } 186} 187``` 188 189## Context.getGroupDir<sup>10+</sup> 190 191getGroupDir(dataGroupID: string, callback: AsyncCallback\<string>): void 192 193Obtains the shared directory based on a group ID. This API uses an asynchronous callback to return the result. 194 195**Atomic service API**: This API can be used in atomic services since API version 11. 196 197**System capability**: SystemCapability.Ability.AbilityRuntime.Core 198 199**Parameters** 200 201| Name | Type | Mandatory | Description | 202| -------- | ---------------------- | ---- | ------------- | 203| dataGroupID | string | Yes | Group ID, which is assigned by the system when an atomic service project is created.| 204| 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.| 205 206**Error codes** 207 208For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 209 210| ID| Error Message| 211| ------- | -------------------------------- | 212| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 213| 16000011 | The context does not exist. | 214 215**Example** 216 217```ts 218import { common, UIAbility } from '@kit.AbilityKit'; 219import { BusinessError } from '@kit.BasicServicesKit'; 220 221export default class EntryAbility extends UIAbility { 222 onCreate() { 223 console.log('MyAbility onCreate'); 224 let getGroupDirContext: common.Context = this.context; 225 226 getGroupDirContext.getGroupDir("1", (err: BusinessError, data) => { 227 if (err) { 228 console.error(`getGroupDir faile, err: ${JSON.stringify(err)}`); 229 } else { 230 console.log(`getGroupDir result is: ${JSON.stringify(data)}`); 231 } 232 }); 233 } 234} 235``` 236