js-apis-Context.md 31.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
## Modules to Import

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

W
wusongqing 已提交
12 13
## Usage

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

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

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

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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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



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

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

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

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

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

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

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

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

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

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

**Example**

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

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

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

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

**Example**

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

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

**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 已提交
613
## Context.getOrCreateDistributedDir<sup>7+</sup>
W
wusongqing 已提交
614 615 616 617 618 619 620 621 622 623 624

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

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

**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 已提交
643
## Context.getOrCreateDistributedDir<sup>7+</sup>
W
wusongqing 已提交
644 645 646 647 648 649 650 651 652 653 654

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

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

**Example**

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

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

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

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

**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 已提交
697
## Context.getAppType<sup>7+</sup>
W
wusongqing 已提交
698 699 700 701 702 703 704 705 706

getAppType(): Promise\<string>

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

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

**Return value**

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

**Example**

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

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

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

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

**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 已提交
749
## Context.getHapModuleInfo<sup>7+</sup>
W
wusongqing 已提交
750 751 752 753 754 755 756 757 758

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

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

**Example**

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

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

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

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

**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 已提交
801
## Context.getAppVersionInfo<sup>7+</sup>
W
wusongqing 已提交
802 803 804 805 806 807 808 809 810

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

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

**Example**

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

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

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

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

**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 已提交
853
## Context.getAbilityInfo<sup>7+</sup>
W
wusongqing 已提交
854 855 856 857 858 859 860 861 862

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

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

**Example**

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

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

getApplicationContext(): Context

Obtains the context of the application.

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

**Return value**

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

**Example**

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

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

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

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

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

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

W
wusongqing 已提交
911
| Name       | Readable/Writable| Type          | Mandatory| Description              |
W
wusongqing 已提交
912
| ----------- | -------- | -------------- | ---- | ------------------ |
W
wusongqing 已提交
913 914 915
| 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 已提交
916

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

Describes the HAP module information.

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

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

W
wusongqing 已提交
942 943 944 945 946
| Name            | Type| Readable   | Writable  | Description|
| ------          | ------ | ------| ------ | ------    |
| appName         | string | Yes   | No    | Module name.     |
| versionCode     | number | Yes   | No    | Module description.  |
| versionName     | string | Yes   | No    | Module description ID.    |