• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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## Attributes
11
12**System capability**: SystemCapability.Ability.AbilityRuntime.Core
13
14| Name         | Type    | Readable  | Writable  | Description     |
15| ----------- | ------ | ---- | ---- | ------- |
16| resourceManager     | resmgr.[ResourceManager](js-apis-resource-manager.md) | Yes   | No   | Object for resource management.  |
17| applicationInfo | [ApplicationInfo](js-apis-bundle-ApplicationInfo.md) | Yes   | No   | Application information.|
18| cacheDir | string | Yes   | No   | Cache directory.|
19| tempDir | string | Yes   | No   | Temporary directory.|
20| filesDir | string | Yes   | No   | File directory.|
21| databaseDir | string | Yes   | No   | Database directory.|
22| preferencesDir | string | Yes   | No   | Preferences directory.|
23| bundleCodeDir | string | Yes   | No   | Bundle code directory. A resource file cannot be accessed by combining paths. Use [Resource Manager](js-apis-resource-manager.md) to access it. |
24| distributedFilesDir | string | Yes   | No   | Distributed file directory.|
25| eventHub | [EventHub](js-apis-inner-application-eventHub.md) | Yes   | No   | Event hub that implements event subscription, unsubscription, and triggering.|
26| area | [AreaMode](#areamode) | Yes   | No   | Area in which the file to be access is located.|
27
28
29## Context.createBundleContext
30
31createBundleContext(bundleName: string): Context;
32
33Creates the context based on the bundle name.
34
35**System capability**: SystemCapability.Ability.AbilityRuntime.Core
36
37**Parameters**
38
39| Name      | Type                    | Mandatory  | Description           |
40| -------- | ---------------------- | ---- | ------------- |
41| bundleName | string | Yes   | Bundle name.|
42
43**Return value**
44
45| Type| Description|
46| -------- | -------- |
47| Context | Context created.|
48
49**Example**
50
51```ts
52let bundleContext = this.context.createBundleContext('com.example.test');
53```
54
55## Context.createModuleContext
56
57createModuleContext(moduleName: string): Context;
58
59Creates the context based on the module name.
60
61**System capability**: SystemCapability.Ability.AbilityRuntime.Core
62
63**Parameters**
64
65| Name      | Type                    | Mandatory  | Description           |
66| -------- | ---------------------- | ---- | ------------- |
67| moduleName | string | Yes   | Module name.|
68
69**Return value**
70
71| Type| Description|
72| -------- | -------- |
73| Context | Context created.|
74
75**Example**
76
77```ts
78let moduleContext = this.context.createModuleContext('entry');
79```
80
81## Context.createModuleContext
82
83createModuleContext(bundleName: string, moduleName: string): Context;
84
85Creates the context based on the bundle name and module name.
86
87**System capability**: SystemCapability.Ability.AbilityRuntime.Core
88
89**Parameters**
90
91| Name      | Type                    | Mandatory  | Description           |
92| -------- | ---------------------- | ---- | ------------- |
93| bundleName | string | Yes   | Bundle name.|
94| moduleName | string | Yes   | Module name.|
95
96**Return value**
97
98| Type| Description|
99| -------- | -------- |
100| Context | Context created.|
101
102**Example**
103
104```ts
105let moduleContext = this.context.createModuleContext('com.example.test', 'entry');
106```
107
108## Context.getApplicationContext
109
110getApplicationContext(): ApplicationContext;
111
112Obtains the context of this application.
113
114**System capability**: SystemCapability.Ability.AbilityRuntime.Core
115
116**Return value**
117
118| Type| Description|
119| -------- | -------- |
120| Context | Application context obtained.|
121
122**Example**
123
124```ts
125let applicationContext = this.context.getApplicationContext();
126```
127
128## AreaMode
129
130Enumerates the areas in which the file to be access can be located.
131
132**System capability**: SystemCapability.Ability.AbilityRuntime.Core
133
134| Name| Value| Description|
135| -------- | -------- | -------- |
136| EL1 | 0 | Device-level encryption area, which is accessible after the device is powered on.|
137| EL2 | 1 | User-level encryption area, which is accessible only after the device is powered on and the password is entered (for the first time).|
138