js-apis-inner-application-context.md 5.3 KB
Newer Older
1 2
# Context

3
The **Context** module provides context for abilities or applications. It allows access to application-specific resources.
4 5 6

> **NOTE**
>
7 8
>  - 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.
9 10 11 12 13 14 15

## Attributes

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

| Name         | Type    | Readable  | Writable  | Description     |
| ----------- | ------ | ---- | ---- | ------- |
16 17
| resourceManager     | resmgr.[ResourceManager](js-apis-resource-manager.md) | Yes   | No   | Object for resource management.  |
| applicationInfo | [ApplicationInfo](js-apis-bundle-ApplicationInfo.md) | Yes   | No   | Application information.|
18 19 20 21 22 23 24
| cacheDir | string | Yes   | No   | Cache directory.|
| tempDir | string | Yes   | No   | Temporary directory.|
| filesDir | string | Yes   | No   | File directory.|
| databaseDir | string | Yes   | No   | Database directory.|
| preferencesDir | string | Yes   | No   | Preferences directory.|
| bundleCodeDir | string | Yes   | No   | Bundle code directory.|
| distributedFilesDir | string | Yes   | No   | Distributed file directory.|
G
Gloria 已提交
25 26
| eventHub | [EventHub](js-apis-inner-application-eventHub.md) | Yes   | No   | Event hub that implements event subscription, unsubscription, and triggering.|
| area | contextConstant.[AreaMode](js-apis-app-ability-contextConstant.md) | Yes   | No   | Area in which the file to be access is located.|
27 28 29 30 31 32 33

## Context.createBundleContext

createBundleContext(bundleName: string): Context;

Creates the context based on the bundle name.

34 35
**Required permissions**: ohos.permission.GET_BUNDLE_INFO_PRIVILEGED

36 37 38 39 40 41
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name      | Type                    | Mandatory  | Description           |
| -------- | ---------------------- | ---- | ------------- |
42
| bundleName | string | Yes   | Bundle name.|
43 44 45 46 47 48 49

**Return value**

| Type| Description|
| -------- | -------- |
| Context | Context created.|

50 51 52 53 54 55 56 57
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | If the input parameter is not valid parameter. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

58 59 60
**Example**

```ts
61 62
let bundleContext;
try {
G
Gloria 已提交
63
    bundleContext = this.context.createBundleContext('com.example.test');
64
} catch (error) {
65
    console.log('createBundleContext failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
66
}
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
```

## Context.createModuleContext

createModuleContext(moduleName: string): Context;

Creates the context based on the module name.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name      | Type                    | Mandatory  | Description           |
| -------- | ---------------------- | ---- | ------------- |
| moduleName | string | Yes   | Module name.|

**Return value**

| Type| Description|
| -------- | -------- |
| Context | Context created.|

89 90 91 92 93 94 95 96
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | If the input parameter is not valid parameter. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

97 98 99
**Example**

```ts
100 101
let moduleContext;
try {
G
Gloria 已提交
102
    moduleContext = this.context.createModuleContext('entry');
103
} catch (error) {
104
    console.log('createModuleContext failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
105
}
106 107
```

108 109
## Context.createModuleContext

110 111 112 113 114 115 116 117 118 119
createModuleContext(bundleName: string, moduleName: string): Context;

Creates the context based on the bundle name and module name.

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Parameters**

| Name      | Type                    | Mandatory  | Description           |
| -------- | ---------------------- | ---- | ------------- |
120
| bundleName | string | Yes   | Bundle name.|
121 122 123 124 125 126 127 128
| moduleName | string | Yes   | Module name.|

**Return value**

| Type| Description|
| -------- | -------- |
| Context | Context created.|

129 130 131 132 133 134 135 136
**Error codes**

| ID| Error Message|
| ------- | -------------------------------- |
| 401 | If the input parameter is not valid parameter. |

For details about the error codes, see [Ability Error Codes](../errorcodes/errorcode-ability.md).

137 138 139
**Example**

```ts
140 141
let moduleContext;
try {
G
Gloria 已提交
142
    moduleContext = this.context.createModuleContext('com.example.test', 'entry');
143
} catch (error) {
144
    console.log('createModuleContext failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
145
}
146 147 148 149 150 151
```

## Context.getApplicationContext

getApplicationContext(): ApplicationContext;

152
Obtains the context of this application.
153 154 155 156 157 158 159

**System capability**: SystemCapability.Ability.AbilityRuntime.Core

**Return value**

| Type| Description|
| -------- | -------- |
160
| [ApplicationContext](js-apis-inner-application-applicationContext.md) | Application context obtained.|
161 162 163 164

**Example**

```ts
165 166 167 168
let applicationContext;
try {
    applicationContext = this.context.getApplicationContext();
} catch (error) {
169
    console.log('getApplicationContext failed, error.code: ${JSON.stringify(error.code)}, error.message: ${JSON.stringify(error.message)}');
170
}
171
```