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

W
wusongqing 已提交
3 4 5 6
> **NOTE**
> 
> 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.  
> The APIs of this module can be used only in the FA model.
W
wusongqing 已提交
7

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

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

W
wusongqing 已提交
14 15
## Usage

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

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

W
wusongqing 已提交
24
## Context.getOrCreateLocalDir<sup>7+</sup>
W
wusongqing 已提交
25

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

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

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

W
wusongqing 已提交
32
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
33

W
wusongqing 已提交
34
**Parameters**
W
wusongqing 已提交
35

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

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

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



W
wusongqing 已提交
52
## Context.getOrCreateLocalDir<sup>7+</sup>
W
wusongqing 已提交
53

W
wusongqing 已提交
54
getOrCreateLocalDir(): Promise\<string>
W
wusongqing 已提交
55

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

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

W
wusongqing 已提交
60
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
61

W
wusongqing 已提交
62
**Return value**
W
wusongqing 已提交
63

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

W
wusongqing 已提交
68
**Example**
W
wusongqing 已提交
69 70 71 72

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



W
wusongqing 已提交
80
## Context.verifyPermission<sup>7+</sup>
W
wusongqing 已提交
81

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

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

W
wusongqing 已提交
86
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
87

W
wusongqing 已提交
88
**Parameters**
W
wusongqing 已提交
89

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

W
wusongqing 已提交
96
**Example**
W
wusongqing 已提交
97 98 99 100 101

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



W
wusongqing 已提交
109
## Context.verifyPermission<sup>7+</sup>
W
wusongqing 已提交
110

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

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

W
wusongqing 已提交
115
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
116

W
wusongqing 已提交
117
**Parameters**
W
wusongqing 已提交
118

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

W
wusongqing 已提交
124
**Example**
W
wusongqing 已提交
125 126 127 128 129 130 131

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

W
wusongqing 已提交
132
## Context.verifyPermission<sup>7+</sup>
W
wusongqing 已提交
133

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

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

W
wusongqing 已提交
138
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
139

W
wusongqing 已提交
140
**Parameters**
W
wusongqing 已提交
141

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

W
wusongqing 已提交
147
**Return value**
W
wusongqing 已提交
148

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

W
wusongqing 已提交
153
**Example**
W
wusongqing 已提交
154 155 156 157

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



W
wusongqing 已提交
167
## Context.requestPermissionsFromUser<sup>7+</sup>
W
wusongqing 已提交
168

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

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

W
wusongqing 已提交
173
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
174

W
wusongqing 已提交
175
**Parameters**
W
wusongqing 已提交
176

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

W
wusongqing 已提交
183
**Example**
W
wusongqing 已提交
184 185 186 187

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



W
wusongqing 已提交
203
## Context.getApplicationInfo<sup>7+</sup>
W
wusongqing 已提交
204

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

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

W
wusongqing 已提交
209
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
210

W
wusongqing 已提交
211
**Parameters**
W
wusongqing 已提交
212

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

W
wusongqing 已提交
217
**Example**
W
wusongqing 已提交
218 219 220 221 222 223 224 225 226

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



W
wusongqing 已提交
227
## Context.getApplicationInfo<sup>7+</sup>
W
wusongqing 已提交
228

W
wusongqing 已提交
229
getApplicationInfo(): Promise\<ApplicationInfo>
W
wusongqing 已提交
230

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

W
wusongqing 已提交
233
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
234

W
wusongqing 已提交
235
**Return value**
W
wusongqing 已提交
236

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

W
wusongqing 已提交
241
**Example**
W
wusongqing 已提交
242 243 244 245

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



W
wusongqing 已提交
254
## Context.getBundleName<sup>7+</sup>
W
wusongqing 已提交
255

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

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

W
wusongqing 已提交
260
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
261

W
wusongqing 已提交
262
**Parameters**
W
wusongqing 已提交
263

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

W
wusongqing 已提交
268
**Example**
W
wusongqing 已提交
269 270 271 272 273 274 275 276 277

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



W
wusongqing 已提交
278
## Context.getBundleName<sup>7+</sup>
W
wusongqing 已提交
279

W
wusongqing 已提交
280
getBundleName(): Promise\<string>
W
wusongqing 已提交
281

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

W
wusongqing 已提交
284
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
285

W
wusongqing 已提交
286
**Return value**
W
wusongqing 已提交
287

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

W
wusongqing 已提交
292
**Example**
W
wusongqing 已提交
293 294 295 296

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



W
wusongqing 已提交
305
## Context.getProcessInfo<sup>7+</sup>
W
wusongqing 已提交
306

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

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

W
wusongqing 已提交
311
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
312

W
wusongqing 已提交
313
**Parameters**
W
wusongqing 已提交
314

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

W
wusongqing 已提交
319
**Example**
W
wusongqing 已提交
320 321 322 323 324 325 326 327 328

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



W
wusongqing 已提交
329
## Context.getProcessInfo<sup>7+</sup>
W
wusongqing 已提交
330

W
wusongqing 已提交
331
getProcessInfo(): Promise\<ProcessInfo>
W
wusongqing 已提交
332

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

W
wusongqing 已提交
335
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
336

W
wusongqing 已提交
337
**Return value**
W
wusongqing 已提交
338

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

W
wusongqing 已提交
343
**Example**
W
wusongqing 已提交
344 345 346 347

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



W
wusongqing 已提交
356
## Context.getElementName<sup>7+</sup>
W
wusongqing 已提交
357

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

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

W
wusongqing 已提交
362
This API is available only to Page abilities.
W
wusongqing 已提交
363

W
wusongqing 已提交
364
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
365

W
wusongqing 已提交
366
**Parameters**
W
wusongqing 已提交
367

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

W
wusongqing 已提交
372
**Example**
W
wusongqing 已提交
373 374 375 376 377 378 379 380 381

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



W
wusongqing 已提交
382
## Context.getElementName<sup>7+</sup>
W
wusongqing 已提交
383

W
wusongqing 已提交
384
getElementName(): Promise\<ElementName>
W
wusongqing 已提交
385

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

W
wusongqing 已提交
388
This API is available only to Page abilities.
W
wusongqing 已提交
389

W
wusongqing 已提交
390
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
391

W
wusongqing 已提交
392
**Return value**
W
wusongqing 已提交
393

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

W
wusongqing 已提交
398
**Example**
W
wusongqing 已提交
399 400 401 402

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

W
wusongqing 已提交
409
## Context.getProcessName<sup>7+</sup>
W
wusongqing 已提交
410

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

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

W
wusongqing 已提交
415
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
416

W
wusongqing 已提交
417
**Parameters**
W
wusongqing 已提交
418

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

W
wusongqing 已提交
423
**Example**
W
wusongqing 已提交
424 425 426 427 428 429 430 431 432

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



W
wusongqing 已提交
433
## Context.getProcessName<sup>7+</sup>
W
wusongqing 已提交
434

W
wusongqing 已提交
435
getProcessName(): Promise\<string>
W
wusongqing 已提交
436

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

W
wusongqing 已提交
439
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
440

W
wusongqing 已提交
441
**Return value**
W
wusongqing 已提交
442

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

W
wusongqing 已提交
447
**Example**
W
wusongqing 已提交
448 449 450 451

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



W
wusongqing 已提交
460
## Context.getCallingBundle<sup>7+</sup>
W
wusongqing 已提交
461

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

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

W
wusongqing 已提交
466
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
467

W
wusongqing 已提交
468
**Parameters**
W
wusongqing 已提交
469

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

W
wusongqing 已提交
474
**Example**
W
wusongqing 已提交
475 476 477 478 479 480 481 482 483

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



W
wusongqing 已提交
484
## Context.getCallingBundle<sup>7+</sup>
W
wusongqing 已提交
485

W
wusongqing 已提交
486
getCallingBundle(): Promise\<string>
W
wusongqing 已提交
487

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

W
wusongqing 已提交
490
**System capability**: SystemCapability.Ability.AbilityRuntime.Core
W
wusongqing 已提交
491

W
wusongqing 已提交
492
**Return value**
W
wusongqing 已提交
493

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

W
wusongqing 已提交
498
**Example**
W
wusongqing 已提交
499 500 501 502

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

W
wusongqing 已提交
509 510 511 512 513 514 515 516 517 518
## 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 已提交
519
| Name    | Type                  | Mandatory| Description                     |
W
wusongqing 已提交
520
| -------- | ---------------------- | ---- | ------------------------- |
W
wusongqing 已提交
521
| callback | AsyncCallback\<string> | Yes  | Callback used to return the cache directory.|
W
wusongqing 已提交
522 523 524 525 526

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
527
var context = featureAbility.getContext();
W
wusongqing 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
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 已提交
547
| Type           | Description                     |
W
wusongqing 已提交
548
| --------------- | ------------------------- |
W
wusongqing 已提交
549
| Promise\<string> | Promise used to return the cache directory.|
W
wusongqing 已提交
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

**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 已提交
572
| Name    | Type                  | Mandatory| Description                     |
W
wusongqing 已提交
573
| -------- | ---------------------- | ---- | ------------------------- |
W
wusongqing 已提交
574
| callback | AsyncCallback\<string> | Yes  | Callback used to return the file directory.|
W
wusongqing 已提交
575 576 577 578 579

**Example**

```js
import featureAbility from '@ohos.ability.featureAbility'
W
wusongqing 已提交
580
var context = featureAbility.getContext();
W
wusongqing 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
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 已提交
600
| Type           | Description                     |
W
wusongqing 已提交
601
| --------------- | ------------------------- |
W
wusongqing 已提交
602
| Promise\<string> | Promise used to return the file directory.|
W
wusongqing 已提交
603 604 605 606 607 608 609 610 611 612 613 614

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

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

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

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

**Example**

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

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

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

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

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

**Example**

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

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

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

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

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

**Example**

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

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

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

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

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

**Example**

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

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

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 已提交
837
| Name    | Type                  | Mandatory| Description                     |
W
wusongqing 已提交
838
| -------- | ---------------------- | ---- | ------------------------- |
839
| callback | AsyncCallback\<[AbilityInfo](js-apis-bundle-AbilityInfo.md)> | Yes  | Callback used to return the ability information.|
W
wusongqing 已提交
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

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

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 已提交
865
| Type           | Description                     |
W
wusongqing 已提交
866
| --------------- | ------------------------- |
867
| Promise\<[AbilityInfo](js-apis-bundle-AbilityInfo.md)> | Promise used to return the ability information.|
W
wusongqing 已提交
868 869 870 871 872 873 874 875 876 877 878

**Example**

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

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

getApplicationContext(): Context

Obtains the context of the application.

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

**Return value**

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

**Example**

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

W
wusongqing 已提交
900
## PermissionOptions<sup>7+</sup>
W
wusongqing 已提交
901

W
wusongqing 已提交
902 903
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

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

W
wusongqing 已提交
909
## PermissionRequestResult<sup>7+</sup>
W
wusongqing 已提交
910

W
wusongqing 已提交
911 912
**System capability**: SystemCapability.Ability.AbilityRuntime.Core

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

W
wusongqing 已提交
919
## HapModuleInfo<sup>7+</sup>
W
wusongqing 已提交
920 921 922

Describes the HAP module information.

W
wusongqing 已提交
923
| Name              | Type| Readable| Writable| Description|
W
wusongqing 已提交
924
| ------ | ------ | ------ | ------ | ------ |
W
wusongqing 已提交
925 926 927 928 929 930 931 932
| 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.      |
W
wusongqing 已提交
933
| supportedModes   | number        | Yes  | No  | Running modes supported by the module.    |
W
wusongqing 已提交
934 935 936
| reqCapabilities  | Array\<string> | Yes  | No  | Capabilities required for module running.|
| deviceTypes      | Array\<string> | Yes  | No  | Device types supported by the module.|
| abilityInfo      | Array\<AbilityInfo> | Yes  | No  | Ability information. |
W
wusongqing 已提交
937
| moduleName       | string        | Yes  | No  | Module name.            |
W
wusongqing 已提交
938 939 940
| mainAbilityName  | string        | Yes  | No  | Name of the main ability.   |
| installationFree | boolean       | Yes  | No  | Whether installation-free is supported.    |
| mainElementName | string | Yes| No| Information about the main ability.|
W
wusongqing 已提交
941

W
wusongqing 已提交
942
## AppVersionInfo<sup>7+</sup>
W
wusongqing 已提交
943

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