js-apis-Context.md 29.0 KB
Newer Older
W
wusongqing 已提交
1 2
# Context Module

3
> **NOTE**<br/>
W
wusongqing 已提交
4 5
> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.

W
wusongqing 已提交
6 7 8 9 10 11 12
## Modules to Import

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

W
wusongqing 已提交
13
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 已提交
14 15 16 17 18 19 20

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

W
wusongqing 已提交
21
## Context.getOrCreateLocalDir<sup>7+</sup>
W
wusongqing 已提交
22

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

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

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

W
wusongqing 已提交
29
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
30

W
wusongqing 已提交
31
**Parameters**
W
wusongqing 已提交
32

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

W
wusongqing 已提交
37
**Example**
W
wusongqing 已提交
38 39 40 41 42 43 44 45 46 47 48

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



W
wusongqing 已提交
49
## Context.getOrCreateLocalDir<sup>7+</sup>
W
wusongqing 已提交
50

W
wusongqing 已提交
51
getOrCreateLocalDir(): Promise\<string>
W
wusongqing 已提交
52

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

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

W
wusongqing 已提交
57
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
58

W
wusongqing 已提交
59
**Return value**
W
wusongqing 已提交
60

61
| Type | Description |
W
wusongqing 已提交
62
| ---------------- | ---------------------- |
63
| Promise\<string> | Promise used to return the local root directory. |
W
wusongqing 已提交
64

W
wusongqing 已提交
65
**Example**
W
wusongqing 已提交
66 67 68 69

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



W
wusongqing 已提交
77
## Context.verifyPermission<sup>7+</sup>
W
wusongqing 已提交
78

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

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

W
wusongqing 已提交
83
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
84

W
wusongqing 已提交
85
**Parameters**
W
wusongqing 已提交
86

87
| Name | Type | Mandatory | Description |
W
wusongqing 已提交
88
| ---------- | --------------------------------------- | ---- | ------------------------------------- |
89 90 91
| 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 已提交
92

W
wusongqing 已提交
93
**Example**
W
wusongqing 已提交
94 95 96 97 98

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



W
wusongqing 已提交
106
## Context.verifyPermission<sup>7+</sup>
W
wusongqing 已提交
107

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

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

W
wusongqing 已提交
112
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
113

W
wusongqing 已提交
114
**Parameters**
W
wusongqing 已提交
115

116
| Name | Type | Mandatory | Description |
W
wusongqing 已提交
117
| ---------- | ---------------------- | ---- | ------------------------------------- |
118 119
| 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 已提交
120

W
wusongqing 已提交
121
**Example**
W
wusongqing 已提交
122 123 124 125 126 127 128

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

W
wusongqing 已提交
129
## Context.verifyPermission<sup>7+</sup>
W
wusongqing 已提交
130

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

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

W
wusongqing 已提交
135
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
136

W
wusongqing 已提交
137
**Parameters**
W
wusongqing 已提交
138

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

W
wusongqing 已提交
144
**Return value**
W
wusongqing 已提交
145

146
| Type | Description |
W
wusongqing 已提交
147
| ---------------- | ----------------------------------------------------------- |
148
| 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 已提交
149

W
wusongqing 已提交
150
**Example**
W
wusongqing 已提交
151 152 153 154 155

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



W
wusongqing 已提交
164
## Context.requestPermissionsFromUser<sup>7+</sup>
W
wusongqing 已提交
165

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

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

W
wusongqing 已提交
170
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
171

W
wusongqing 已提交
172
**Parameters**
W
wusongqing 已提交
173

174
| Name | Type | Mandatory | Description |
W
wusongqing 已提交
175
| -------------- | ------------------------------------------------------------ | ---- | ----------------------------------------------- |
176 177 178
| 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 已提交
179

W
wusongqing 已提交
180
**Example**
W
wusongqing 已提交
181 182 183 184

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



W
wusongqing 已提交
200
## Context.getApplicationInfo<sup>7+</sup>
W
wusongqing 已提交
201

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

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

W
wusongqing 已提交
206
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
207

W
wusongqing 已提交
208
**Parameters**
W
wusongqing 已提交
209

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

W
wusongqing 已提交
214
**Example**
W
wusongqing 已提交
215 216 217 218 219 220 221 222 223

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



W
wusongqing 已提交
224
## Context.getApplicationInfo<sup>7+</sup>
W
wusongqing 已提交
225

W
wusongqing 已提交
226
getApplicationInfo(): Promise\<ApplicationInfo>
W
wusongqing 已提交
227

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

W
wusongqing 已提交
230
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
231

W
wusongqing 已提交
232
**Return value**
W
wusongqing 已提交
233

234
| Type | Description |
W
wusongqing 已提交
235
| ------------------------- | ------------------ |
236
| Promise\<ApplicationInfo> | Promise used to return the application information. |
W
wusongqing 已提交
237

W
wusongqing 已提交
238
**Example**
W
wusongqing 已提交
239 240 241 242

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



W
wusongqing 已提交
251
## Context.getBundleName<sup>7+</sup>
W
wusongqing 已提交
252

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

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

W
wusongqing 已提交
257
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
258

W
wusongqing 已提交
259
**Parameters**
W
wusongqing 已提交
260

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

W
wusongqing 已提交
265
**Example**
W
wusongqing 已提交
266 267 268 269 270 271 272 273 274

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



W
wusongqing 已提交
275
## Context.getBundleName<sup>7+</sup>
W
wusongqing 已提交
276

W
wusongqing 已提交
277
getBundleName(): Promise\<string>
W
wusongqing 已提交
278

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

W
wusongqing 已提交
281
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
282

W
wusongqing 已提交
283
**Return value**
W
wusongqing 已提交
284

285
| Type | Description |
W
wusongqing 已提交
286
| ---------------- | ------------------------- |
287
| Promise\<string> | Promise used to return the bundle name. |
W
wusongqing 已提交
288

W
wusongqing 已提交
289
**Example**
W
wusongqing 已提交
290 291 292 293

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



W
wusongqing 已提交
302
## Context.getProcessInfo<sup>7+</sup>
W
wusongqing 已提交
303

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

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

W
wusongqing 已提交
308
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
309

W
wusongqing 已提交
310
**Parameters**
W
wusongqing 已提交
311

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

W
wusongqing 已提交
316
**Example**
W
wusongqing 已提交
317 318 319 320 321 322 323 324 325

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



W
wusongqing 已提交
326
## Context.getProcessInfo<sup>7+</sup>
W
wusongqing 已提交
327

W
wusongqing 已提交
328
getProcessInfo(): Promise\<ProcessInfo>
W
wusongqing 已提交
329

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

W
wusongqing 已提交
332
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
333

W
wusongqing 已提交
334
**Return value**
W
wusongqing 已提交
335

336
| Type | Description |
W
wusongqing 已提交
337
| --------------------- | -------------- |
338
| Promise\<ProcessInfo> | Promise used to return the process information. |
W
wusongqing 已提交
339

W
wusongqing 已提交
340
**Example**
W
wusongqing 已提交
341 342 343 344

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



W
wusongqing 已提交
353
## Context.getElementName<sup>7+</sup>
W
wusongqing 已提交
354

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

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

W
wusongqing 已提交
359
This API is available only to Page abilities.
W
wusongqing 已提交
360

W
wusongqing 已提交
361
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
362

W
wusongqing 已提交
363
**Parameters**
W
wusongqing 已提交
364

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

W
wusongqing 已提交
369
**Example**
W
wusongqing 已提交
370 371 372 373 374 375 376 377 378

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



W
wusongqing 已提交
379
## Context.getElementName<sup>7+</sup>
W
wusongqing 已提交
380

W
wusongqing 已提交
381
getElementName(): Promise\<ElementName>
W
wusongqing 已提交
382

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

W
wusongqing 已提交
385
This API is available only to Page abilities.
W
wusongqing 已提交
386

W
wusongqing 已提交
387
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
388

W
wusongqing 已提交
389
**Return value**
W
wusongqing 已提交
390

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

W
wusongqing 已提交
395
**Example**
W
wusongqing 已提交
396 397 398 399

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

W
wusongqing 已提交
406
## Context.getProcessName<sup>7+</sup>
W
wusongqing 已提交
407

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

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

W
wusongqing 已提交
412
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
413

W
wusongqing 已提交
414
**Parameters**
W
wusongqing 已提交
415

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

W
wusongqing 已提交
420
**Example**
W
wusongqing 已提交
421 422 423 424 425 426 427 428 429

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



W
wusongqing 已提交
430
## Context.getProcessName<sup>7+</sup>
W
wusongqing 已提交
431

W
wusongqing 已提交
432
getProcessName(): Promise\<string>
W
wusongqing 已提交
433

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

W
wusongqing 已提交
436
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
437

W
wusongqing 已提交
438
**Return value**
W
wusongqing 已提交
439

440
| Type | Description |
W
wusongqing 已提交
441
| ---------------- | -------------------- |
442
| Promise\<string> | Promise used to return the process name. |
W
wusongqing 已提交
443

W
wusongqing 已提交
444
**Example**
W
wusongqing 已提交
445 446 447 448

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



W
wusongqing 已提交
457
## Context.getCallingBundle<sup>7+</sup>
W
wusongqing 已提交
458

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

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

W
wusongqing 已提交
463
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
464

W
wusongqing 已提交
465
**Parameters**
W
wusongqing 已提交
466

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

W
wusongqing 已提交
471
**Example**
W
wusongqing 已提交
472 473 474 475 476 477 478 479 480

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



W
wusongqing 已提交
481
## Context.getCallingBundle<sup>7+</sup>
W
wusongqing 已提交
482

W
wusongqing 已提交
483
getCallingBundle(): Promise\<string>
W
wusongqing 已提交
484

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

W
wusongqing 已提交
487
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
488

W
wusongqing 已提交
489
**Return value**
W
wusongqing 已提交
490

491
| Type | Description |
W
wusongqing 已提交
492
| --------------- | ------------------------- |
493
| Promise\<string> | Promise used to return the bundle name. |
W
wusongqing 已提交
494

W
wusongqing 已提交
495
**Example**
W
wusongqing 已提交
496 497 498 499

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

W
wusongqing 已提交
506 507 508 509 510 511 512 513 514 515
## 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**

516
| Name | Type | Mandatory | Description |
W
wusongqing 已提交
517
| -------- | ---------------------- | ---- | ------------------------- |
518
| callback | AsyncCallback\<string> | Yes | Callback used to return the cache directory. |
W
wusongqing 已提交
519 520 521 522 523

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
524
var context = featureAbility.getContext();
W
wusongqing 已提交
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
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**

544
| Type | Description |
W
wusongqing 已提交
545
| --------------- | ------------------------- |
546
| Promise\<string> | Promise used to return the cache directory. |
W
wusongqing 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568

**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**

569
| Name | Type | Mandatory | Description |
W
wusongqing 已提交
570
| -------- | ---------------------- | ---- | ------------------------- |
571
| callback | AsyncCallback\<string> | Yes | Callback used to return the file directory. |
W
wusongqing 已提交
572 573 574 575 576

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
577
var context = featureAbility.getContext();
W
wusongqing 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
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**

597
| Type | Description |
W
wusongqing 已提交
598
| --------------- | ------------------------- |
599
| Promise\<string> | Promise used to return the file directory. |
W
wusongqing 已提交
600 601 602 603 604 605 606 607 608 609 610 611

**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));
});
```

W
wusongqing 已提交
612
## Context.getOrCreateDistributedDir<sup>7+</sup>
W
wusongqing 已提交
613 614 615 616 617 618 619 620 621 622 623

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**

624
| Name | Type | Mandatory | Description |
W
wusongqing 已提交
625
| -------- | ---------------------- | ---- | ------------------------- |
626
| 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. |
W
wusongqing 已提交
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641

**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));
});
```

W
wusongqing 已提交
642
## Context.getOrCreateDistributedDir<sup>7+</sup>
W
wusongqing 已提交
643 644 645 646 647 648 649 650 651 652 653

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**

654
| Type | Description |
W
wusongqing 已提交
655
| --------------- | ------------------------- |
656
| 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. |
W
wusongqing 已提交
657 658 659 660 661 662 663 664 665 666 667

**Example**

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

W
wusongqing 已提交
668
## Context.getAppType<sup>7+</sup>
W
wusongqing 已提交
669 670 671 672 673 674 675 676 677

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**

678
| Name | Type | Mandatory | Description |
W
wusongqing 已提交
679
| -------- | ---------------------- | ---- | ------------------------- |
680
| callback | AsyncCallback\<string> | Yes | Callback used to return the application type. |
W
wusongqing 已提交
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

**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));
});
```

W
wusongqing 已提交
696
## Context.getAppType<sup>7+</sup>
W
wusongqing 已提交
697 698 699 700 701 702 703 704 705

getAppType(): Promise\<string>

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

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

**Return value**

706
| Type | Description |
W
wusongqing 已提交
707
| --------------- | ------------------------- |
708
| Promise\<string> | Promise used to return the application type. |
W
wusongqing 已提交
709 710 711 712 713 714 715 716 717 718 719

**Example**

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

W
wusongqing 已提交
720
## Context.getHapModuleInfo<sup>7+</sup>
W
wusongqing 已提交
721 722 723 724 725 726 727 728 729

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**

730
| Name | Type | Mandatory | Description |
W
wusongqing 已提交
731
| -------- | ---------------------- | ---- | ------------------------- |
732
| callback | AsyncCallback\<[HapModuleInfo](#hapmoduleinfo)> | Yes | Callback used to return the **ModuleInfo** object. |
W
wusongqing 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

**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));
});
```

W
wusongqing 已提交
748
## Context.getHapModuleInfo<sup>7+</sup>
W
wusongqing 已提交
749 750 751 752 753 754 755 756 757

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**

758
| Type | Description |
W
wusongqing 已提交
759
| --------------- | ------------------------- |
760
| Promise\<[HapModuleInfo](#hapmoduleinfo)> | Promise used to return the **ModuleInfo** object. |
W
wusongqing 已提交
761 762 763 764 765 766 767 768 769 770 771

**Example**

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

W
wusongqing 已提交
772
## Context.getAppVersionInfo<sup>7+</sup>
W
wusongqing 已提交
773 774 775 776 777 778 779 780 781

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**

782
| Name | Type | Mandatory | Description |
W
wusongqing 已提交
783
| -------- | ---------------------- | ---- | ------------------------- |
784
| callback | AsyncCallback\<[AppVersionInfo](#appversioninfo)> | Yes | Callback used to return the version information. |
W
wusongqing 已提交
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799

**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));
});
```

W
wusongqing 已提交
800
## Context.getAppVersionInfo<sup>7+</sup>
W
wusongqing 已提交
801 802 803 804 805 806 807 808 809

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**

810
| Type | Description |
W
wusongqing 已提交
811
| --------------- | ------------------------- |
812
| Promise\<[AppVersionInfo](#appversioninfo)> | Promise used to return the version information. |
W
wusongqing 已提交
813 814 815 816 817 818 819 820 821 822 823

**Example**

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

W
wusongqing 已提交
824
## Context.getAbilityInfo<sup>7+</sup>
W
wusongqing 已提交
825 826 827 828 829 830 831 832 833

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**

834
| Name | Type | Mandatory | Description |
W
wusongqing 已提交
835
| -------- | ---------------------- | ---- | ------------------------- |
836
| callback | AsyncCallback\<[AbilityInfo](#abilityInfo)> | Yes |Callback used to return the ability information. |
W
wusongqing 已提交
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851

**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));
});
```

W
wusongqing 已提交
852
## Context.getAbilityInfo<sup>7+</sup>
W
wusongqing 已提交
853 854 855 856 857 858 859 860 861

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**

862
| Type | Description |
W
wusongqing 已提交
863
| --------------- | ------------------------- |
864
| Promise\<[AbilityInfo](#abilityInfo)> | Promise used to return the ability information. |
W
wusongqing 已提交
865 866 867 868 869 870 871 872 873 874 875

**Example**

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

W
wusongqing 已提交
876
## Context.getApplicationContext<sup>7+</sup>
W
wusongqing 已提交
877 878 879 880 881 882 883 884 885

getApplicationContext(): Context

Obtains the context of the application.

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

**Return value**

886
| Type | Description |
W
wusongqing 已提交
887
| --------- |------ |
888
|  Context |Application context. |
W
wusongqing 已提交
889 890 891 892 893 894 895 896

**Example**

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

W
wusongqing 已提交
897
## PermissionOptions<sup>7+</sup>
W
wusongqing 已提交
898

W
wusongqing 已提交
899 900
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

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

W
wusongqing 已提交
906
## PermissionRequestResult<sup>7+</sup>
W
wusongqing 已提交
907

W
wusongqing 已提交
908 909
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

910
| Name | Readable/Writable | Type | Mandatory | Description |
W
wusongqing 已提交
911
| ----------- | -------- | -------------- | ---- | ------------------ |
912 913 914
| requestCode | Read-only | number | Yes | Request code passed. |
| permissions | Read-only | Array\<string> | Yes | Permissions requested. |
| authResults | Read-only | Array\<number> | Yes | Permission request result. |
W
wusongqing 已提交
915

W
wusongqing 已提交
916
## HapModuleInfo<sup>7+</sup>
W
wusongqing 已提交
917 918 919

Describes the HAP module information.

920
| Name | Type| Readable | Writable | Description |
W
wusongqing 已提交
921
| ------ | ------ | ------ | ------ | ------ |
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
| 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. |
W
wusongqing 已提交
938

W
wusongqing 已提交
939
## AppVersionInfo<sup>7+</sup>
W
wusongqing 已提交
940

941 942 943 944 945
| Name | Type| Readable | Writable | Description |
| ------ | ------ | ------| ------ | ------ |
| appName | string | Yes | No | Module name. |
| versionCode | number | Yes | No | Module description. |
| versionName | string | Yes | No | Module description ID. |