Context (Context Base Class of the Stage Model)
Context is the context base class of the stage model. It is used to access application-specific resources and perform callbacks for application-level operations.
NOTE
- 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.
- The APIs of this module can be used only in the stage model.
Inheritance and Holding Relationships of Different Context Types
-
Inheritance relationships among different types of context

-
Holding relationships among different types of context

NOTE
UIContext refers to the context of a UI instance, which is used to associate windows with UI pages. It is not directly related to the application context discussed in this topic and does not involve inheritance or holding relationships.
Modules to Import
import { common } from '@kit.AbilityKit';
Context
Represents the context for the ability or application. It allows access to application-specific resources.
Properties
System capability: SystemCapability.Ability.AbilityRuntime.Core
| Name | Type | Read-only | Optional | Description |
|---|---|---|---|---|
| resourceManager | resmgr.ResourceManager | No | No | Object for resource management. Atomic service API: This API can be used in atomic services since API version 11. |
| applicationInfo | ApplicationInfo | No | No | Application information. Atomic service API: This API can be used in atomic services since API version 11. |
| cacheDir | string | No | No | Cache directory. For details, see Application Sandbox. Atomic service API: This API can be used in atomic services since API version 11. |
| tempDir | string | No | No | Temporary directory. For details, see Application Sandbox. Atomic service API: This API can be used in atomic services since API version 11. |
| resourceDir11+ | string | No | No | Resource directory. 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. Atomic service API: This API can be used in atomic services since API version 11. |
| filesDir | string | No | No | File directory. For details, see Application Sandbox. Atomic service API: This API can be used in atomic services since API version 11. |
| databaseDir | string | No | No | Database directory. For details, see Application Sandbox. Atomic service API: This API can be used in atomic services since API version 11. |
| preferencesDir | string | No | No | Preferences directory. For details, see Application Sandbox. Atomic service API: This API can be used in atomic services since API version 11. |
| bundleCodeDir | string | No | No | Bundle code directory. Do not access resource files using concatenated paths. Use resource manager APIs instead. For details, see Application Sandbox. Atomic service API: This API can be used in atomic services since API version 11. |
| distributedFilesDir | string | No | No | Distributed file directory. For details, see Application Sandbox. Atomic service API: This API can be used in atomic services since API version 11. |
| cloudFileDir12+ | string | No | No | Cloud file directory. Atomic service API: This API can be used in atomic services since API version 12. |
| logFileDir22+ | string | No | No | Directory for storing log files. Atomic service API: This API can be used in atomic services since API version 22. |
| eventHub | EventHub | No | No | Event hub that implements event subscription, unsubscription, and triggering. Atomic service API: This API can be used in atomic services since API version 11. |
| area | contextConstant.AreaMode | No | No | Information about file partitions, which are divided according to the encryption level specified by AreaMode. Atomic service API: This API can be used in atomic services since API version 11. |
| processName18+ | string | No | No | Process name of the current application. Atomic service API: This API can be used in atomic services since API version 18. |
createModuleContext(deprecated)
createModuleContext(moduleName: string): Context
Creates the context based on the module name.
NOTE
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.
This API has been supported since API version 9 and deprecated since API version 12. You are advised to use application.createModuleContext instead. Otherwise, resource acquisition may fail.
Creating a module context involves resource querying and initialization, which can be time-consuming. In scenarios where application fluidity is critical, avoid frequently or repeatedly calling the createModuleContext API to create multiple context instances, as this may negatively impact user experience.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| moduleName | string | Yes | Module name. |
Return value
| Type | Description |
|---|---|
| Context | Context created. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
Example
import { common, UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate() {
console.info('MyAbility onCreate');
let moduleContext: common.Context;
try {
moduleContext = this.context.createModuleContext('entry');
} catch (error) {
console.error(`createModuleContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
}
}
}
getApplicationContext
getApplicationContext(): ApplicationContext
Obtains the application context.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Return value
| Type | Description |
|---|---|
| ApplicationContext | Application context. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
Example
import { common, UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate() {
console.info('MyAbility onCreate');
let applicationContext: common.Context;
try {
applicationContext = this.context.getApplicationContext();
} catch (error) {
console.error(`getApplicationContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
}
}
}
getGroupDir10+
getGroupDir(dataGroupID: string): Promise<string>
Obtains the shared directory based on a group ID. This API uses a promise to return the result.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataGroupID | string | Yes | Group ID, which is assigned by the system when an application of the atomic service type is created. |
Return value
| Type | Description |
|---|---|
| Promise<string> | Promise used to return the result. If no shared directory exists, null is returned. Only the encryption level EL2 is supported. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
| 16000011 | The context does not exist. |
Example
import { common, UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate() {
console.info('MyAbility onCreate');
let groupId = "1";
let getGroupDirContext: common.Context = this.context;
try {
getGroupDirContext.getGroupDir(groupId).then(data => {
console.info("getGroupDir result:" + data);
})
} catch (error) {
console.error(`getGroupDirContext failed, error.code: ${(error as BusinessError).code}, error.message: ${(error as BusinessError).message}`);
}
}
}
getGroupDir10+
getGroupDir(dataGroupID: string, callback: AsyncCallback<string>): void
Obtains the shared directory based on a group ID. This API uses an asynchronous callback to return the result.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| dataGroupID | string | Yes | Group ID, which is assigned by the system when an application of the atomic service type is created. |
| callback | AsyncCallback<string> | Yes | Callback used to return the result. If the API call is successful, err is undefined and data is the shared directory obtained (or empty if or is empty if non-existent). Otherwise, an error object is returned. Note: Only the EL2 encryption level is supported. |
Error codes
For details about the error codes, see Universal Error Codes and Ability Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
| 16000011 | The context does not exist. |
Example
import { common, UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends UIAbility {
onCreate() {
console.info('MyAbility onCreate');
let getGroupDirContext: common.Context = this.context;
getGroupDirContext.getGroupDir("1", (err: BusinessError, data) => {
if (err) {
console.error(`getGroupDir failed, err: ${JSON.stringify(err)}`);
} else {
console.info(`getGroupDir result is: ${JSON.stringify(data)}`);
}
});
}
}
createAreaModeContext18+
createAreaModeContext(areaMode: contextConstant.AreaMode): Context
Creates an application context with a specific data encryption level. You can call this API to create contexts with different encryption levels, thereby obtaining the corresponding sandbox paths.
Atomic service API: This API can be used in atomic services since API version 18.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| areaMode | contextConstant.AreaMode | Yes | Data encryption level. |
Return value
| Type | Description |
|---|---|
| Context | Context created based on the data encryption level. |
Example
import { common, UIAbility, contextConstant } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
export default class EntryAbility extends UIAbility {
onCreate() {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
let areaMode: contextConstant.AreaMode = contextConstant.AreaMode.EL2;
let areaModeContext: common.Context;
try {
areaModeContext = this.context.createAreaModeContext(areaMode);
} catch (error) {
hilog.error(0x0000, 'testTag', 'createAreaModeContext error is:%{public}s', JSON.stringify(error));
}
}
}
createDisplayContext15+
createDisplayContext(displayId: number): Context
Creates an application context based on the specified display ID with screen information (including ScreenDensity and Direction).
Atomic service API: This API can be used in atomic services since API version 15.
System capability: SystemCapability.Ability.AbilityRuntime.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| displayId | number | Yes | Display ID. |
Return value
| Type | Description |
|---|---|
| Context | Context with the specified screen information. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
Example
import { common, UIAbility } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
export default class EntryAbility extends UIAbility {
onCreate() {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
let displayContext: common.Context;
try {
displayContext = this.context.createDisplayContext(0);
} catch (error) {
hilog.error(0x0000, 'testTag', 'createDisplayContext error is:%{public}s', JSON.stringify(error));
}
}
}