js-apis-Context.md 30.4 KB
Newer Older
W
wusongqing 已提交
1 2 3 4 5 6 7 8 9
# Context Module

## Modules to Import

```js
import featureAbility from '@ohos.ability.featureAbility'
import bundle from '@ohos.bundle'
```

W
wusongqing 已提交
10
The **Context** object is created in a **featureAbility** and returned through its **getContext()** API. Therefore, you must import the **@ohos.ability.featureAbility** package before using the Context module. An example is as follows:
W
wusongqing 已提交
11 12 13 14 15 16 17

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getOrCreateLocalDir()
```

W
wusongqing 已提交
18
## Context.getOrCreateLocalDir
W
wusongqing 已提交
19

W
wusongqing 已提交
20
getOrCreateLocalDir(callback: AsyncCallback\<string>): void
W
wusongqing 已提交
21

W
wusongqing 已提交
22
Obtains the local root directory of the application. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
23

W
wusongqing 已提交
24
If this API is called for the first time, a root directory will be created.
W
wusongqing 已提交
25

W
wusongqing 已提交
26
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
27

W
wusongqing 已提交
28
**Parameters**
W
wusongqing 已提交
29

W
wusongqing 已提交
30
| Name    | Type                  | Mandatory| Description                      |
W
wusongqing 已提交
31
| -------- | ---------------------- | ---- | -------------------------- |
W
wusongqing 已提交
32
| callback | AsyncCallback\<string> | Yes  | Callback used to return the local root directory.|
W
wusongqing 已提交
33

W
wusongqing 已提交
34
**Example**
W
wusongqing 已提交
35 36 37 38 39 40 41 42 43 44 45

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getOrCreateLocalDir((err, data)=>{
    console.info("data=" + data);
})
```



W
wusongqing 已提交
46
## Context.getOrCreateLocalDir
W
wusongqing 已提交
47

W
wusongqing 已提交
48
getOrCreateLocalDir(): Promise\<string>
W
wusongqing 已提交
49

W
wusongqing 已提交
50
Obtains the local root directory of the application. This API uses a promise to return the result.
W
wusongqing 已提交
51

W
wusongqing 已提交
52
If this API is called for the first time, a root directory will be created.
W
wusongqing 已提交
53

W
wusongqing 已提交
54
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
55

W
wusongqing 已提交
56
**Return value**
W
wusongqing 已提交
57

W
wusongqing 已提交
58
| Type            | Description                  |
W
wusongqing 已提交
59
| ---------------- | ---------------------- |
W
wusongqing 已提交
60
| Promise\<string> | Promise used to return the local root directory.|
W
wusongqing 已提交
61

W
wusongqing 已提交
62
**Example**
W
wusongqing 已提交
63 64 65 66

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
67 68
context.getOrCreateLocalDir().then((data) => {
    console.info("data=" + data);
W
wusongqing 已提交
69 70 71 72 73
});
```



W
wusongqing 已提交
74
## Context.verifyPermission
W
wusongqing 已提交
75

W
wusongqing 已提交
76
verifyPermission(permission: string, options: PermissionOptions, callback: AsyncCallback\<number>): void
W
wusongqing 已提交
77

W
wusongqing 已提交
78
Verifies whether a specific PID and UID have the given permission. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
79

W
wusongqing 已提交
80
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
81

W
wusongqing 已提交
82
**Parameters**
W
wusongqing 已提交
83

W
wusongqing 已提交
84
| Name      | Type                                   | Mandatory| Description                                 |
W
wusongqing 已提交
85
| ---------- | --------------------------------------- | ---- | ------------------------------------- |
W
wusongqing 已提交
86 87 88
| permission | string                                  | Yes  | Name of the permission to verify.                     |
| options    | [PermissionOptions](#permissionoptions) | Yes  | Permission options.                           |
| callback   | AsyncCallback\<number>                  | Yes  | Callback used to return the permission verification result. The value **0** indicates that the PID and UID have the given permission, and the value **-1** indicates that the PID and UID do not have the given permission.|
W
wusongqing 已提交
89

W
wusongqing 已提交
90
**Example**
W
wusongqing 已提交
91 92 93 94 95

```js
import featureAbility from '@ohos.ability.featureAbility'
import bundle from '@ohos.bundle'
var context = featureAbility.getContext();
W
wusongqing 已提交
96 97 98
bundle.getBundleInfo('com.context.test', 1, (datainfo) =>{
	context.verifyPermission("com.example.permission", datainfo.uid);
});
W
wusongqing 已提交
99 100 101 102
```



W
wusongqing 已提交
103
## Context.verifyPermission
W
wusongqing 已提交
104

W
wusongqing 已提交
105
verifyPermission(permission: string, callback: AsyncCallback\<number>): void
W
wusongqing 已提交
106

W
wusongqing 已提交
107
Verifies whether the current PID and UID have the given permission. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
108

W
wusongqing 已提交
109
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
110

W
wusongqing 已提交
111
**Parameters**
W
wusongqing 已提交
112

W
wusongqing 已提交
113
| Name      | Type                  | Mandatory| Description                                 |
W
wusongqing 已提交
114
| ---------- | ---------------------- | ---- | ------------------------------------- |
W
wusongqing 已提交
115 116
| permission | string                 | Yes  | Name of the permission to verify.                     |
| callback   | AsyncCallback\<number> | Yes  | Callback used to return the permission verification result. The value **0** indicates that the PID and UID have the given permission, and the value **-1** indicates that the PID and UID do not have the given permission.|
W
wusongqing 已提交
117

W
wusongqing 已提交
118
**Example**
W
wusongqing 已提交
119 120 121 122 123 124 125

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.verifyPermission("com.example.permission")
```

W
wusongqing 已提交
126
## Context.verifyPermission
W
wusongqing 已提交
127

W
wusongqing 已提交
128
verifyPermission(permission: string, options?: PermissionOptions): Promise\<number>
W
wusongqing 已提交
129

W
wusongqing 已提交
130
Verifies whether a specific PID and UID have the given permission. This API uses a promise to return the result.
W
wusongqing 已提交
131

W
wusongqing 已提交
132
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
133

W
wusongqing 已提交
134
**Parameters**
W
wusongqing 已提交
135

W
wusongqing 已提交
136
| Name      | Type                                   | Mandatory| Description            |
W
wusongqing 已提交
137
| ---------- | --------------------------------------- | ---- | ---------------- |
W
wusongqing 已提交
138 139
| permission | string                                  | Yes  | Name of the permission to verify.|
| options    | [PermissionOptions](#permissionoptions) | No  | Permission options.      |
W
wusongqing 已提交
140

W
wusongqing 已提交
141
**Return value**
W
wusongqing 已提交
142

W
wusongqing 已提交
143
| Type            | Description                                                       |
W
wusongqing 已提交
144 145
| ---------------- | ----------------------------------------------------------- |
| Promise\<number> | Promise used to return the permission verification result. The value **0** indicates that the PID and UID have the given permission, and the value **-1** indicates that the PID and UID do not have the given permission.|
W
wusongqing 已提交
146

W
wusongqing 已提交
147
**Example**
W
wusongqing 已提交
148 149 150 151 152

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
var Permission = context.PermissionOptions(1,1);
W
wusongqing 已提交
153 154 155
context.verifyPermission('com.context.permission',Permission).then((data) => {
    console.info("======================>verifyPermissionCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
156 157 158 159 160
});
```



W
wusongqing 已提交
161
## Context.requestPermissionsFromUser
W
wusongqing 已提交
162

W
wusongqing 已提交
163
requestPermissionsFromUser(permissions: Array\<string>, requestCode: number, resultCallback: AsyncCallback<[PermissionRequestResult](#permissionrequestresult)>): void
W
wusongqing 已提交
164

W
wusongqing 已提交
165
Requests certain permissions from the system. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
166

W
wusongqing 已提交
167
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
168

W
wusongqing 已提交
169
**Parameters**
W
wusongqing 已提交
170

W
wusongqing 已提交
171
| Name          | Type                                                        | Mandatory| Description                                           |
W
wusongqing 已提交
172
| -------------- | ------------------------------------------------------------ | ---- | ----------------------------------------------- |
W
wusongqing 已提交
173 174 175
| permissions    | Array\<string>                                               | Yes  | Permissions to request. This parameter cannot be **null**.       |
| requestCode    | number                                                       | Yes  | Request code to be passed to **PermissionRequestResult**.|
| resultCallback | AsyncCallback<[PermissionRequestResult](#permissionrequestresult)> | Yes  | Permission request result.                             |
W
wusongqing 已提交
176

W
wusongqing 已提交
177
**Example**
W
wusongqing 已提交
178 179 180 181

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
182
context.requestPermissionsFromUser(
W
wusongqing 已提交
183 184 185 186 187
    ["com.example.permission1",
     "com.example.permission2",
     "com.example.permission3",
     "com.example.permission4",
     "com.example.permission5"],
W
wusongqing 已提交
188 189 190 191
    1,(err, data)=>{
        console.info("====>requestdata====>" + JSON.stringify(data));
        console.info("====>requesterrcode====>" + JSON.stringify(err.code));
    }
W
wusongqing 已提交
192 193 194 195 196
)
```



W
wusongqing 已提交
197
## Context.getApplicationInfo
W
wusongqing 已提交
198

W
wusongqing 已提交
199
getApplicationInfo(callback: AsyncCallback\<ApplicationInfo>): void
W
wusongqing 已提交
200

W
wusongqing 已提交
201
Obtains information about the current application. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
202

W
wusongqing 已提交
203
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
204

W
wusongqing 已提交
205
**Parameters**
W
wusongqing 已提交
206

W
wusongqing 已提交
207
| Name    | Type                           | Mandatory| Description                    |
W
wusongqing 已提交
208
| -------- | ------------------------------- | ---- | ------------------------ |
W
wusongqing 已提交
209
| callback | AsyncCallback\<ApplicationInfo> | Yes  | Callback used to return the application information.|
W
wusongqing 已提交
210

W
wusongqing 已提交
211
**Example**
W
wusongqing 已提交
212 213 214 215 216 217 218 219 220

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getApplicationInfo()
```



W
wusongqing 已提交
221
## Context.getApplicationInfo
W
wusongqing 已提交
222

W
wusongqing 已提交
223
getApplicationInfo(): Promise\<ApplicationInfo>
W
wusongqing 已提交
224

W
wusongqing 已提交
225
Obtains information about the current application. This API uses a promise to return the result.
W
wusongqing 已提交
226

W
wusongqing 已提交
227
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
228

W
wusongqing 已提交
229
**Return value**
W
wusongqing 已提交
230

W
wusongqing 已提交
231
| Type                     | Description              |
W
wusongqing 已提交
232 233
| ------------------------- | ------------------ |
| Promise\<ApplicationInfo> | Promise used to return the application information.|
W
wusongqing 已提交
234

W
wusongqing 已提交
235
**Example**
W
wusongqing 已提交
236 237 238 239

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
240 241 242
context.getApplicationInfo().then((data) => {
    console.info("=====================>getApplicationInfoCallback===================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
243 244 245 246 247
});
```



W
wusongqing 已提交
248
## Context.getBundleName
W
wusongqing 已提交
249

W
wusongqing 已提交
250
getBundleName(callback: AsyncCallback\<string>): void
W
wusongqing 已提交
251

W
wusongqing 已提交
252
Obtains the bundle name of the current ability. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
253

W
wusongqing 已提交
254
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
255

W
wusongqing 已提交
256
**Parameters**
W
wusongqing 已提交
257

W
wusongqing 已提交
258
| Name    | Type                  | Mandatory| Description                         |
W
wusongqing 已提交
259
| -------- | ---------------------- | ---- | ----------------------------- |
W
wusongqing 已提交
260
| callback | AsyncCallback\<string> | Yes  | Callback used to return the bundle name.|
W
wusongqing 已提交
261

W
wusongqing 已提交
262
**Example**
W
wusongqing 已提交
263 264 265 266 267 268 269 270 271

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getBundleName()
```



W
wusongqing 已提交
272
## Context.getBundleName
W
wusongqing 已提交
273

W
wusongqing 已提交
274
getBundleName(): Promise\<string>
W
wusongqing 已提交
275

W
wusongqing 已提交
276
Obtains the bundle name of the current ability. This API uses a promise to return the result.
W
wusongqing 已提交
277

W
wusongqing 已提交
278
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
279

W
wusongqing 已提交
280
**Return value**
W
wusongqing 已提交
281

W
wusongqing 已提交
282
| Type            | Description                     |
W
wusongqing 已提交
283 284
| ---------------- | ------------------------- |
| Promise\<string> | Promise used to return the bundle name.|
W
wusongqing 已提交
285

W
wusongqing 已提交
286
**Example**
W
wusongqing 已提交
287 288 289 290

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
291 292 293
context.getBundleName().then((data) => {
    console.info("=======================>getBundleNameCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
294 295 296 297 298
});
```



W
wusongqing 已提交
299
## Context.getProcessInfo
W
wusongqing 已提交
300

W
wusongqing 已提交
301
getProcessInfo(callback: AsyncCallback\<ProcessInfo>): void
W
wusongqing 已提交
302

W
wusongqing 已提交
303
Obtains information about the current process, including the PID and process name. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
304

W
wusongqing 已提交
305
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
306

W
wusongqing 已提交
307
**Parameters**
W
wusongqing 已提交
308

W
wusongqing 已提交
309
| Name    | Type                       | Mandatory| Description                |
W
wusongqing 已提交
310
| -------- | --------------------------- | ---- | -------------------- |
W
wusongqing 已提交
311
| callback | AsyncCallback\<ProcessInfo> | Yes  | Callback used to return the process information.|
W
wusongqing 已提交
312

W
wusongqing 已提交
313
**Example**
W
wusongqing 已提交
314 315 316 317 318 319 320 321 322

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getProcessInfo()
```



W
wusongqing 已提交
323
## Context.getProcessInfo
W
wusongqing 已提交
324

W
wusongqing 已提交
325
getProcessInfo(): Promise\<ProcessInfo>
W
wusongqing 已提交
326

W
wusongqing 已提交
327
Obtains information about the current process, including the PID and process name. This API uses a promise to return the result.
W
wusongqing 已提交
328

W
wusongqing 已提交
329
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
330

W
wusongqing 已提交
331
**Return value**
W
wusongqing 已提交
332

W
wusongqing 已提交
333
| Type                 | Description          |
W
wusongqing 已提交
334 335
| --------------------- | -------------- |
| Promise\<ProcessInfo> | Promise used to return the process information.|
W
wusongqing 已提交
336

W
wusongqing 已提交
337
**Example**
W
wusongqing 已提交
338 339 340 341

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
342 343 344
context.getProcessInfo().then((data) => {
    console.info("=======================>getProcessInfoCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
345 346 347 348 349
});
```



W
wusongqing 已提交
350
## Context.getElementName
W
wusongqing 已提交
351

W
wusongqing 已提交
352
getElementName(callback: AsyncCallback\<ElementName>): void
W
wusongqing 已提交
353

W
wusongqing 已提交
354
Obtains the **ohos.bundle.ElementName** object of the current ability. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
355

W
wusongqing 已提交
356
This API is available only to Page abilities.
W
wusongqing 已提交
357

W
wusongqing 已提交
358
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
359

W
wusongqing 已提交
360
**Parameters**
W
wusongqing 已提交
361

W
wusongqing 已提交
362
| Name    | Type                       | Mandatory| Description                                          |
W
wusongqing 已提交
363
| -------- | --------------------------- | ---- | ---------------------------------------------- |
W
wusongqing 已提交
364
| callback | AsyncCallback\<ElementName> | Yes  | Callback used to return the **ohos.bundle.ElementName** object.|
W
wusongqing 已提交
365

W
wusongqing 已提交
366
**Example**
W
wusongqing 已提交
367 368 369 370 371 372 373 374 375

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getElementName()
```



W
wusongqing 已提交
376
## Context.getElementName
W
wusongqing 已提交
377

W
wusongqing 已提交
378
getElementName(): Promise\<ElementName>
W
wusongqing 已提交
379

W
wusongqing 已提交
380
Obtains the **ohos.bundle.ElementName** object of the current ability. This API uses a promise to return the result.
W
wusongqing 已提交
381

W
wusongqing 已提交
382
This API is available only to Page abilities.
W
wusongqing 已提交
383

W
wusongqing 已提交
384
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
385

W
wusongqing 已提交
386
**Return value**
W
wusongqing 已提交
387

W
wusongqing 已提交
388
| Type                 | Description                                      |
W
wusongqing 已提交
389
| --------------------- | ------------------------------------------ |
W
wusongqing 已提交
390
| Promise\<ElementName> | Promise used to return the **ohos.bundle.ElementName** object.|
W
wusongqing 已提交
391

W
wusongqing 已提交
392
**Example**
W
wusongqing 已提交
393 394 395 396

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
397 398 399
context.getElementName().then((data) => {
    console.info("=======================>getElementNameCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
400 401 402
});
```

W
wusongqing 已提交
403
## Context.getProcessName
W
wusongqing 已提交
404

W
wusongqing 已提交
405
getProcessName(callback: AsyncCallback\<string>): void
W
wusongqing 已提交
406

W
wusongqing 已提交
407
Obtains the name of the current process. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
408

W
wusongqing 已提交
409
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
410

W
wusongqing 已提交
411
**Parameters**
W
wusongqing 已提交
412 413

| Name    | Type                  | Mandatory| Description                |
W
wusongqing 已提交
414
| -------- | ---------------------- | ---- | -------------------- |
W
wusongqing 已提交
415
| callback | AsyncCallback\<string> | Yes  | Callback used to return the process name.|
W
wusongqing 已提交
416

W
wusongqing 已提交
417
**Example**
W
wusongqing 已提交
418 419 420 421 422 423 424 425 426

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getProcessName()
```



W
wusongqing 已提交
427
## Context.getProcessName
W
wusongqing 已提交
428

W
wusongqing 已提交
429
getProcessName(): Promise\<string>
W
wusongqing 已提交
430

W
wusongqing 已提交
431
Obtains the name of the current process. This API uses a promise to return the result.
W
wusongqing 已提交
432

W
wusongqing 已提交
433
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
434

W
wusongqing 已提交
435
**Return value**
W
wusongqing 已提交
436

W
wusongqing 已提交
437
| Type            | Description                |
W
wusongqing 已提交
438
| ---------------- | -------------------- |
W
wusongqing 已提交
439
| Promise\<string> | Promise used to return the process name.|
W
wusongqing 已提交
440

W
wusongqing 已提交
441
**Example**
W
wusongqing 已提交
442 443 444 445

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
446 447 448
context.getProcessName().then((data) => {
    console.info("=======================>getProcessNameCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
449 450 451 452 453
});
```



W
wusongqing 已提交
454
## Context.getCallingBundle
W
wusongqing 已提交
455

W
wusongqing 已提交
456
getCallingBundle(callback: AsyncCallback\<string>): void
W
wusongqing 已提交
457

W
wusongqing 已提交
458
Obtains the bundle name of the calling ability. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
459

W
wusongqing 已提交
460
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
461

W
wusongqing 已提交
462
**Parameters**
W
wusongqing 已提交
463

W
wusongqing 已提交
464
| Name    | Type                  | Mandatory| Description                     |
W
wusongqing 已提交
465
| -------- | ---------------------- | ---- | ------------------------- |
W
wusongqing 已提交
466
| callback | AsyncCallback\<string> | Yes  | Callback used to return the bundle name.|
W
wusongqing 已提交
467

W
wusongqing 已提交
468
**Example**
W
wusongqing 已提交
469 470 471 472 473 474 475 476 477

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getCallingBundle()
```



W
wusongqing 已提交
478
## Context.getCallingBundle
W
wusongqing 已提交
479

W
wusongqing 已提交
480
getCallingBundle(): Promise\<string>
W
wusongqing 已提交
481

W
wusongqing 已提交
482
Obtains the bundle name of the calling ability. This API uses a promise to return the result.
W
wusongqing 已提交
483

W
wusongqing 已提交
484
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
485

W
wusongqing 已提交
486
**Return value**
W
wusongqing 已提交
487

W
wusongqing 已提交
488
| Type           | Description                     |
W
wusongqing 已提交
489 490
| --------------- | ------------------------- |
| Promise\<string> | Promise used to return the bundle name.|
W
wusongqing 已提交
491

W
wusongqing 已提交
492
**Example**
W
wusongqing 已提交
493 494 495 496

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
497 498 499
context.getCallingBundle().then((data) => {
    console.info("======================>getCallingBundleCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
500 501
});
```
W
wusongqing 已提交
502

W
wusongqing 已提交
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
## Context.getCacheDir

getCacheDir(callback: AsyncCallback\<string>): void

Obtains the cache directory of the application on the internal storage. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name    | Type                  | Mandatory| Description                     |
| -------- | ---------------------- | ---- | ------------------------- |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the cache directory.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
521
var context = featureAbility.getContext();
W
wusongqing 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
context.getCacheDir((err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. Data:' + JSON.stringify(data));
});
```

## Context.getCacheDir

getCacheDir(): Promise\<string>

Obtains the cache directory of the application on the internal storage. This API uses a promise to return the result.

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

**Return value**

| Type           | Description                     |
| --------------- | ------------------------- |
| Promise\<string> | Promise used to return the cache directory.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getCacheDir().then((data) => {
    console.info("======================>getCacheDirPromsie====================>");
    console.info("====>data====>" + JSON.stringify(data));
});
```

## Context.getFilesDir

getFilesDir(callback: AsyncCallback\<string>): void

Obtains the file directory of the application on the internal storage. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name    | Type                  | Mandatory| Description                     |
| -------- | ---------------------- | ---- | ------------------------- |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the file directory.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
574
var context = featureAbility.getContext();
W
wusongqing 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
context.getFilesDir((err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. Data:' + JSON.stringify(data));
});
```

## Context.getFilesDir

getFilesDir(): Promise\<string>

Obtains the file directory of the application on the internal storage. This API uses a promise to return the result.

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

**Return value**

| Type           | Description                     |
| --------------- | ------------------------- |
| Promise\<string> | Promise used to return the file directory.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getFilesDir().then((data) => {
    console.info("======================>getFilesDirPromsie====================>");
    console.info("====>data====>" + JSON.stringify(data));
});
```

## Context.getOrCreateDistributedDir

getOrCreateDistributedDir(callback: AsyncCallback\<string>): void

Obtains the distributed file path for storing ability or application data files. This API uses an asynchronous callback to return the result.

If the distributed file path does not exist, the system will create one and return the created path.

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

**Parameters**

| Name    | Type                  | Mandatory| Description                     |
| -------- | ---------------------- | ---- | ------------------------- |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the distributed file path. If the distributed file path does not exist, the system will create one and return the created path.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getOrCreateDistributedDir((err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. Data:' + JSON.stringify(data));
});
```

## Context.getOrCreateDistributedDir

getOrCreateDistributedDir(): Promise\<string>

Obtains the distributed file path for storing ability or application data files. This API uses a promise to return the result.

If the distributed file path does not exist, the system will create one and return the created path.

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

**Return value**

| Type           | Description                     |
| --------------- | ------------------------- |
| Promise\<string> | Promise used to return the distributed file path. If this API is called for the first time, a new path will be created.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getOrCreateDistributedDir().then((data) => {
    console.info("====>data====>" + JSON.stringify(data));
});
```

## Context.getAppType

getAppType(callback: AsyncCallback\<string>): void

Obtains the application type. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name    | Type                  | Mandatory| Description                     |
| -------- | ---------------------- | ---- | ------------------------- |
| callback | AsyncCallback\<string> | Yes  | Callback used to return the application type.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getAppType((err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. Data:' + JSON.stringify(data));
});
```

## Context.getAppType

getAppType(): Promise\<string>

Obtains the application type. This API uses a promise to return the result.

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

**Return value**

| Type           | Description                     |
| --------------- | ------------------------- |
| Promise\<string> | Promise used to return the application type.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getAppType().then((data) => {
    console.info("====>data====>" + JSON.stringify(data));
});
```

## Context.getHapModuleInfo

getHapModuleInfo(callback: AsyncCallback\<HapModuleInfo>): void

Obtains the **ModuleInfo** object of the application. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name    | Type                  | Mandatory| Description                     |
| -------- | ---------------------- | ---- | ------------------------- |
| callback | AsyncCallback\<[HapModuleInfo](#hapmoduleinfo)> | Yes  | Callback used to return the **ModuleInfo** object.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getHapModuleInfo((err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. Data:' + JSON.stringify(data));
});
```

## Context.getHapModuleInfo

getHapModuleInfo(): Promise\<HapModuleInfo>

Obtains the **ModuleInfo** object of the application. This API uses a promise to return the result.

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

**Return value**

| Type           | Description                     |
| --------------- | ------------------------- |
| Promise\<[HapModuleInfo](#hapmoduleinfo)> | Promise used to return the **ModuleInfo** object.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getHapModuleInfo().then((data) => {
    console.info("====>data====>" + JSON.stringify(data));
});
```

## Context.getAppVersionInfo

getAppVersionInfo(callback: AsyncCallback\<HapModuleInfo>): void

Obtains the version information of the application. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name    | Type                  | Mandatory| Description                     |
| -------- | ---------------------- | ---- | ------------------------- |
| callback | AsyncCallback\<[AppVersionInfo](#appversioninfo)> | Yes  | Callback used to return the version information.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getAppVersionInfo((err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. Data:' + JSON.stringify(data));
});
```

## Context.getAppVersionInfo

getAppVersionInfo(): Promise\<AppVersionInfo>

Obtains the version information of the application. This API uses a promise to return the result.

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

**Return value**

| Type           | Description                     |
| --------------- | ------------------------- |
| Promise\<[AppVersionInfo](#appversioninfo)> | Promise used to return the version information.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getAppVersionInfo().then((data) => {
    console.info("====>data====>" + JSON.stringify(data));
});
```

## Context.getAbilityInfo

getAbilityInfo(callback: AsyncCallback\<AbilityInfo>): void

Obtains information of the current ability. This API uses an asynchronous callback to return the result.

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

**Parameters**

| Name    | Type                  | Mandatory| Description                     |
| -------- | ---------------------- | ---- | ------------------------- |
| callback | AsyncCallback\<[AbilityInfo](#abilityInfo)> | Yes  |Callback used to return the ability information.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getAbilityInfo((err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. Data:' + JSON.stringify(data));
});
```

## Context.getAbilityInfo

getAbilityInfo(): Promise\<AbilityInfo>

Obtains information of the current ability. This API uses a promise to return the result.

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

**Return value**

| Type           | Description                     |
| --------------- | ------------------------- |
| Promise\<[AbilityInfo](#abilityInfo)> | Promise used to return the ability information.|

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getAbilityInfo().then((data) => {
    console.info("====>data====>" + JSON.stringify(data));
});
```

## Context.getApplicationContext

getApplicationContext(): Context

Obtains the context of the application.

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

**Return value**

W
wusongqing 已提交
883 884 885
| Type     | Description  |
| --------- |------ |
|  Context |Application context.|
W
wusongqing 已提交
886 887 888 889 890 891 892 893

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext().getApplicationContext();
```

W
wusongqing 已提交
894 895
## PermissionOptions

W
wusongqing 已提交
896 897
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

W
wusongqing 已提交
898
| Name| Readable/Writable| Type  | Mandatory| Description  |
W
wusongqing 已提交
899
| ---- | -------- | ------ | ---- | ------ |
W
wusongqing 已提交
900 901
| pid  | Read-only    | number | No  | Process ID.|
| uid  | Read-only    | number | No  | User ID.|
W
wusongqing 已提交
902 903 904

## PermissionRequestResult

W
wusongqing 已提交
905 906
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

W
wusongqing 已提交
907
| Name       | Readable/Writable| Type          | Mandatory| Description              |
W
wusongqing 已提交
908
| ----------- | -------- | -------------- | ---- | ------------------ |
W
wusongqing 已提交
909 910
| requestCode | Read-only    | number         | Yes  | Request code passed.|
| permissions | Read-only    | Array\<string> | Yes  | Permissions requested.    |
W
wusongqing 已提交
911
| authResults | Read-only    | Array\<number> | Yes  | Permission request result.   |
W
wusongqing 已提交
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942

## HapModuleInfo

Describes the HAP module information.

| Name              | Type| Readable| Writable| Description|
| ------ | ------ | ------ | ------ | ------ |
| name             | string        | Yes  | No  | Module name.          |
| description      | string        | Yes  | No  | Module description.      |
| descriptionId    | number        | Yes  | No  | Module description ID.        |
| icon             | string        | Yes  | No  | Module icon.          |
| label            | string        | Yes  | No  | Module label.          |
| labelId          | number        | Yes  | No  | Module label ID.        |
| iconId           | number        | Yes  | No  | Module icon ID.        |
| backgroundImg    | string        | Yes  | No  | Module background image.      |
| supportedModes   | number        | Yes  | No  | Modes supported by the module.    |
| reqCapabilities  | Array<string> | Yes  | No  | Capabilities required for module running.|
| deviceTypes      | Array<string> | Yes  | No  | An array of supported device types.|
| abilityInfo      | Array<AbilityInfo> | Yes  | No  | Ability information.       |
| moduleName       | string        | Yes  | No  | Module name.            |
| mainAbilityName  | string        | Yes  | No  | Name of the entrance ability.   |
| installationFree | boolean       | Yes  | No  | When installation-free is supported.    |
| mainElementName | string | Yes| No| Information about the entry ability.|

## AppVersionInfo

| Name            | Type| Readable   | Writable  | Description|
| ------          | ------ | ------| ------ | ------    |
| appName         | string | Yes   | No    | Module name.     |
| versionCode     | number | Yes   | No    | Module description.  |
| versionName     | string | Yes   | No    | Module description ID.    |