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