js-apis-Context.md 14.3 KB
Newer Older
W
wusongqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Context Module

## Modules to Import

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

The **Context** object is created in a **featureAbility** and returned through its **getContext()** method. Therefore, you must import the **@ohos.ability.featureAbility** package before using the Context module. An example is as follows:

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

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

W
wusongqing 已提交
20
### getOrCreateLocalDir
W
wusongqing 已提交
21

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

W
wusongqing 已提交
24
Obtains the local root directory of the application. This method uses a callback to return the result.
W
wusongqing 已提交
25

W
wusongqing 已提交
26
If this method is called for the first time, a root directory is created.
W
wusongqing 已提交
27

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


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

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

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



W
wusongqing 已提交
47
### getOrCreateLocalDir
W
wusongqing 已提交
48

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

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

W
wusongqing 已提交
53
If this method is called for the first time, a root directory is created.
W
wusongqing 已提交
54

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

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

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

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



W
wusongqing 已提交
73
### verifyPermission
W
wusongqing 已提交
74

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

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

W
wusongqing 已提交
79
**Parameters**
W
wusongqing 已提交
80 81


W
wusongqing 已提交
82 83 84 85 86
| Name| Type| Mandatory| Description|
| ---------- | --------------------------------------- | ---- | ------------------------------------- |
| 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 已提交
87

W
wusongqing 已提交
88
**Example**
W
wusongqing 已提交
89 90 91 92 93

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



W
wusongqing 已提交
101
### verifyPermission
W
wusongqing 已提交
102

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

W
wusongqing 已提交
105
Verifies whether the current PID and UID have the given permission. This method uses a callback to return the result.
W
wusongqing 已提交
106

W
wusongqing 已提交
107
**Parameters**
W
wusongqing 已提交
108 109


W
wusongqing 已提交
110 111 112 113
| Name| Type| Mandatory| Description|
| ---------- | ---------------------- | ---- | ------------------------------------- |
| 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 已提交
114

W
wusongqing 已提交
115
**Example**
W
wusongqing 已提交
116 117 118 119 120 121 122

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

W
wusongqing 已提交
123
### verifyPermission
W
wusongqing 已提交
124

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

W
wusongqing 已提交
127
Verifies whether a specific PID and UID have the given permission. This method uses a promise to return the result.
W
wusongqing 已提交
128

W
wusongqing 已提交
129
**Parameters**
W
wusongqing 已提交
130 131


W
wusongqing 已提交
132 133 134 135
| Name| Type| Mandatory| Description|
| ---------- | --------------------------------------- | ---- | ---------------- |
| permission | string                                  | Yes| Name of the permission to verify.|
| options    | [PermissionOptions](#permissionoptions) | No| Permission options.|
W
wusongqing 已提交
136

W
wusongqing 已提交
137
**Return value**
W
wusongqing 已提交
138

W
wusongqing 已提交
139 140 141
| Type| Description|
| ---------------- | ----------------------------------------------------------- |
| 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 已提交
142

W
wusongqing 已提交
143
**Example**
W
wusongqing 已提交
144 145 146 147 148

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



W
wusongqing 已提交
157
### requestPermissionsFromUser
W
wusongqing 已提交
158

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

W
wusongqing 已提交
161
Requests certain permissions from the system. This method uses a callback to return the result.
W
wusongqing 已提交
162

W
wusongqing 已提交
163
**Parameters**
W
wusongqing 已提交
164 165


W
wusongqing 已提交
166 167 168 169 170 171
| Name| Type| Mandatory| Description|
| -------------- | ------------------------------------------------------------ | ---- | ----------------------------------------------- |
| 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.|
**Example**
W
wusongqing 已提交
172 173 174 175

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



W
wusongqing 已提交
191
### getApplicationInfo
W
wusongqing 已提交
192

W
wusongqing 已提交
193
getApplicationInfo(callback: AsyncCallback\<ApplicationInfo>)
W
wusongqing 已提交
194

W
wusongqing 已提交
195
Obtains information about the current application. This method uses a callback to return the result.
W
wusongqing 已提交
196

W
wusongqing 已提交
197
**Parameters**
W
wusongqing 已提交
198

W
wusongqing 已提交
199 200 201
| Name| Type| Mandatory| Description|
| -------- | ------------------------------- | ---- | ------------------------ |
| callback | AsyncCallback\<ApplicationInfo> | Yes| Callback used to return the application information.|
W
wusongqing 已提交
202

W
wusongqing 已提交
203
**Example**
W
wusongqing 已提交
204 205 206 207 208 209 210 211 212

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



W
wusongqing 已提交
213
### getApplicationInfo
W
wusongqing 已提交
214

W
wusongqing 已提交
215
getApplicationInfo(): Promise\<ApplicationInfo>
W
wusongqing 已提交
216

W
wusongqing 已提交
217
Obtains information about the current application. This method uses a promise to return the result.
W
wusongqing 已提交
218

W
wusongqing 已提交
219
**Return value**
W
wusongqing 已提交
220

W
wusongqing 已提交
221 222 223
| Type| Description|
| ------------------------- | ------------------ |
| Promise\<ApplicationInfo> | Promise used to return the application information.|
W
wusongqing 已提交
224

W
wusongqing 已提交
225
**Example**
W
wusongqing 已提交
226 227 228 229

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
230 231 232
context.getApplicationInfo().then((data) => {
    console.info("=====================>getApplicationInfoCallback===================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
233 234 235 236 237
});
```



W
wusongqing 已提交
238
### getBundleName
W
wusongqing 已提交
239

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

W
wusongqing 已提交
242
Obtains the bundle name of the current ability. This method uses a callback to return the result.
W
wusongqing 已提交
243

W
wusongqing 已提交
244
**Parameters**
W
wusongqing 已提交
245

W
wusongqing 已提交
246 247 248
| Name| Type| Mandatory| Description|
| -------- | ---------------------- | ---- | ----------------------------- |
| callback | AsyncCallback\<string> | Yes| Callback used to return the bundle name.|
W
wusongqing 已提交
249

W
wusongqing 已提交
250
**Example**
W
wusongqing 已提交
251 252 253 254 255 256 257 258 259

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



W
wusongqing 已提交
260
### getBundleName
W
wusongqing 已提交
261

W
wusongqing 已提交
262
getBundleName(): Promise\<string>
W
wusongqing 已提交
263

W
wusongqing 已提交
264
Obtains the bundle name of the current ability. This method uses a promise to return the result.
W
wusongqing 已提交
265

W
wusongqing 已提交
266
**Return value**
W
wusongqing 已提交
267

W
wusongqing 已提交
268 269 270
| Type| Description|
| ---------------- | ------------------------- |
| Promise\<string> | Promise used to return the bundle name.|
W
wusongqing 已提交
271

W
wusongqing 已提交
272
**Example**
W
wusongqing 已提交
273 274 275 276

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
277 278 279
context.getBundleName().then((data) => {
    console.info("=======================>getBundleNameCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
280 281 282 283 284
});
```



W
wusongqing 已提交
285
### getProcessInfo
W
wusongqing 已提交
286

W
wusongqing 已提交
287
getProcessInfo(callback: AsyncCallback\<ProcessInfo>)
W
wusongqing 已提交
288

W
wusongqing 已提交
289
Obtains information about the current process, including the PID and process name. This method uses a callback to return the result.
W
wusongqing 已提交
290

W
wusongqing 已提交
291
**Parameters**
W
wusongqing 已提交
292

W
wusongqing 已提交
293 294 295
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | -------------------- |
| callback | AsyncCallback\<ProcessInfo> | Yes| Callback used to return the process information.|
W
wusongqing 已提交
296

W
wusongqing 已提交
297
**Example**
W
wusongqing 已提交
298 299 300 301 302 303 304 305 306

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



W
wusongqing 已提交
307
### getProcessInfo
W
wusongqing 已提交
308

W
wusongqing 已提交
309
getProcessInfo(): Promise\<ProcessInfo>
W
wusongqing 已提交
310

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

W
wusongqing 已提交
313
**Return value**
W
wusongqing 已提交
314

W
wusongqing 已提交
315 316 317
| Type| Description|
| --------------------- | -------------- |
| Promise\<ProcessInfo> | Promise used to return the process information.|
W
wusongqing 已提交
318

W
wusongqing 已提交
319
**Example**
W
wusongqing 已提交
320 321 322 323

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
324 325 326
context.getProcessInfo().then((data) => {
    console.info("=======================>getProcessInfoCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
327 328 329 330 331
});
```



W
wusongqing 已提交
332
### getElementName
W
wusongqing 已提交
333

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

W
wusongqing 已提交
336
Obtains the **ohos.bundle.ElementName** object of the current ability. This method uses a callback to return the result.
W
wusongqing 已提交
337

W
wusongqing 已提交
338
This method is available only to Page abilities.
W
wusongqing 已提交
339

W
wusongqing 已提交
340
**Parameters**
W
wusongqing 已提交
341

W
wusongqing 已提交
342 343 344
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | ---------------------------------------------- |
| callback | AsyncCallback\<ElementName> | Yes| Callback used to return the **ohos.bundle.ElementName** object.|
W
wusongqing 已提交
345

W
wusongqing 已提交
346
**Example**
W
wusongqing 已提交
347 348 349 350 351 352 353 354 355

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



W
wusongqing 已提交
356
### getElementName
W
wusongqing 已提交
357

W
wusongqing 已提交
358
getElementName(): Promise\<ElementName>
W
wusongqing 已提交
359

W
wusongqing 已提交
360
Obtains the **ohos.bundle.ElementName** object of the current ability. This method uses a promise to return the result.
W
wusongqing 已提交
361

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

W
wusongqing 已提交
364
**Return value**
W
wusongqing 已提交
365

W
wusongqing 已提交
366 367
| Type| Description|
| --------------------- | ------------------------------------------ |
W
wusongqing 已提交
368
| Promise\<ElementName> | Promise used to return the **ohos.bundle.ElementName** object.|
W
wusongqing 已提交
369

W
wusongqing 已提交
370
**Example**
W
wusongqing 已提交
371 372 373 374

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
375 376 377
context.getElementName().then((data) => {
    console.info("=======================>getElementNameCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
378 379 380
});
```

W
wusongqing 已提交
381
### getProcessName
W
wusongqing 已提交
382

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

W
wusongqing 已提交
385
Obtains the name of the current process. This method uses a callback to return the result.
W
wusongqing 已提交
386

W
wusongqing 已提交
387 388 389
| Name| Type| Mandatory| Description|
| -------- | ---------------------- | ---- | -------------------- |
| callback | AsyncCallback\<string> | Yes| Callback used to return the process name.|
W
wusongqing 已提交
390

W
wusongqing 已提交
391
**Example**
W
wusongqing 已提交
392 393 394 395 396 397 398 399 400

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



W
wusongqing 已提交
401
### getProcessName
W
wusongqing 已提交
402

W
wusongqing 已提交
403
getProcessName(): Promise\<string>
W
wusongqing 已提交
404

W
wusongqing 已提交
405
Obtains the name of the current process. This method uses a promise to return the result.
W
wusongqing 已提交
406

W
wusongqing 已提交
407
**Return value**
W
wusongqing 已提交
408

W
wusongqing 已提交
409 410
| Type| Description|
| ---------------- | -------------------- |
W
wusongqing 已提交
411
| Promise\<string> | Promise used to return the process name.|
W
wusongqing 已提交
412

W
wusongqing 已提交
413
**Example**
W
wusongqing 已提交
414 415 416 417

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
418 419 420
context.getProcessName().then((data) => {
    console.info("=======================>getProcessNameCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
421 422 423 424 425
});
```



W
wusongqing 已提交
426
### getCallingBundle
W
wusongqing 已提交
427

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

W
wusongqing 已提交
430
Obtains the bundle name of the calling ability. This method uses a callback to return the result.
W
wusongqing 已提交
431

W
wusongqing 已提交
432
**Parameters**
W
wusongqing 已提交
433

W
wusongqing 已提交
434 435 436
| Name| Type| Mandatory| Description|
| -------- | ---------------------- | ---- | ------------------------- |
| callback | AsyncCallback\<string> | Yes| Callback used to return the bundle name.|
W
wusongqing 已提交
437

W
wusongqing 已提交
438
**Example**
W
wusongqing 已提交
439 440 441 442 443 444 445 446 447

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



W
wusongqing 已提交
448
### getCallingBundle
W
wusongqing 已提交
449

W
wusongqing 已提交
450
getCallingBundle(): Promise\<string>
W
wusongqing 已提交
451

W
wusongqing 已提交
452
Obtains the bundle name of the calling ability. This method uses a promise to return the result.
W
wusongqing 已提交
453

W
wusongqing 已提交
454
**Return value**
W
wusongqing 已提交
455

W
wusongqing 已提交
456 457 458
| Type| Description|
| --------------- | ------------------------- |
| Promise\<string> | Promise used to return the bundle name.|
W
wusongqing 已提交
459

W
wusongqing 已提交
460
**Example**
W
wusongqing 已提交
461 462 463 464

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
W
wusongqing 已提交
465 466 467
context.getCallingBundle().then((data) => {
    console.info("======================>getCallingBundleCallback====================>");
    console.info("====>data====>" + JSON.stringify(data));
W
wusongqing 已提交
468 469
});
```
W
wusongqing 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484

## PermissionOptions

| Name| Readable/Writable| Type| Mandatory| Description|
| ---- | -------- | ------ | ---- | ------ |
| pid  | Read-only| number | No| PID.|
| uid  | Read-only| number | No| UID.|

## PermissionRequestResult

| Name| Readable/Writable| Type| Mandatory| Description|
| ----------- | -------- | -------------- | ---- | ------------------ |
| requestCode | Read-only| number         | Yes| Request code passed.|
| permissions | Read-only| Array\<string> | Yes| Permissions passed.|
| authResults | Read-only| Array\<number> | Yes| Permission request result.|