1# Context 2 3The **Context** module provides context for abilities or applications. It allows 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 '@ohos.app.ability.common'; 14``` 15 16## Attributes 17 18**System capability**: SystemCapability.Ability.AbilityRuntime.Core 19 20| Name | Type | Readable | Writable | Description | 21| ----------- | ------ | ---- | ---- | ------- | 22| resourceManager | resmgr.[ResourceManager](js-apis-resource-manager.md) | Yes | No | Object for resource management. | 23| applicationInfo | [ApplicationInfo](js-apis-bundle-ApplicationInfo.md) | Yes | No | Application information.| 24| cacheDir | string | Yes | No | Cache directory.| 25| tempDir | string | Yes | No | Temporary directory.| 26| filesDir | string | Yes | No | File directory.| 27| databaseDir | string | Yes | No | Database directory.| 28| preferencesDir | string | Yes | No | Preferences directory.| 29| bundleCodeDir | string | Yes | No | Bundle code directory. Do not access resource files by concatenating paths. Use the [resourceManager API](js-apis-resource-manager.md) instead.| 30| distributedFilesDir | string | Yes | No | Distributed file directory.| 31| eventHub | [EventHub](js-apis-inner-application-eventHub.md) | Yes | No | Event hub that implements event subscription, unsubscription, and triggering.| 32| area | [AreaMode](#areamode) | Yes | No | Area in which the file to be access is located.| 33 34 35## Context.createBundleContext 36 37createBundleContext(bundleName: string): Context; 38 39Creates the context based on the bundle name. 40 41**System capability**: SystemCapability.Ability.AbilityRuntime.Core 42 43**System API**: This is a system API and cannot be called by third-party applications. 44 45**Parameters** 46 47| Name | Type | Mandatory | Description | 48| -------- | ---------------------- | ---- | ------------- | 49| bundleName | string | Yes | Bundle name.| 50 51**Return value** 52 53| Type| Description| 54| -------- | -------- | 55| Context | Context created.| 56 57**Example** 58 59```ts 60let bundleContext: common.Context; 61try { 62 bundleContext = this.context.createBundleContext('com.example.test'); 63} catch (error) { 64 console.error('createBundleContext failed, error.code: ${error.code}, error.message: ${error.message}'); 65} 66``` 67 68## Context.createModuleContext 69 70createModuleContext(moduleName: string): Context; 71 72Creates the context based on the module name. 73 74**System capability**: SystemCapability.Ability.AbilityRuntime.Core 75 76**Parameters** 77 78| Name | Type | Mandatory | Description | 79| -------- | ---------------------- | ---- | ------------- | 80| moduleName | string | Yes | Module name.| 81 82**Return value** 83 84| Type| Description| 85| -------- | -------- | 86| Context | Context created.| 87 88**Example** 89 90```ts 91let moduleContext: common.Context; 92try { 93 moduleContext = this.context.createModuleContext('entry'); 94} catch (error) { 95 console.error('createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}'); 96} 97``` 98 99## Context.createModuleContext 100 101createModuleContext(bundleName: string, moduleName: string): Context; 102 103Creates the context based on the bundle name and module name. 104 105**System capability**: SystemCapability.Ability.AbilityRuntime.Core 106 107**System API**: This is a system API and cannot be called by third-party applications. 108 109**Parameters** 110 111| Name | Type | Mandatory | Description | 112| -------- | ---------------------- | ---- | ------------- | 113| bundleName | string | Yes | Bundle name.| 114| moduleName | string | Yes | Module name.| 115 116**Return value** 117 118| Type| Description| 119| -------- | -------- | 120| Context | Context created.| 121 122**Example** 123 124```ts 125let moduleContext: common.Context; 126try { 127 moduleContext = this.context.createModuleContext('com.example.test', 'entry'); 128} catch (error) { 129 console.error('createModuleContext failed, error.code: ${error.code}, error.message: ${error.message}'); 130} 131``` 132 133## Context.getApplicationContext 134 135getApplicationContext(): ApplicationContext; 136 137Obtains the application context. 138 139**System capability**: SystemCapability.Ability.AbilityRuntime.Core 140 141**Return value** 142 143| Type| Description| 144| -------- | -------- | 145| Context | Application context obtained.| 146 147**Example** 148 149```ts 150let applicationContext: common.Context; 151try { 152 applicationContext = this.context.getApplicationContext(); 153} catch (error) { 154 console.error('getApplicationContext failed, error.code: ${error.code}, error.message: ${error.message}'); 155} 156``` 157 158## AreaMode 159 160Enumerates the encryption levels of directories. 161 162**System capability**: SystemCapability.Ability.AbilityRuntime.Core 163 164| Name| Value| Description| 165| -------- | -------- | -------- | 166| EL1 | 0 | Device-level encryption. Directories with this encryption level are accessible after the device is powered on.| 167| EL2 | 1 | User-level encryption. Directories with this encryption level are accessible only after the device is powered on and the password is entered (for the first time).| 168