js-apis-Context.md 13.8 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 66 67 68 69 70 71 72

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getOrCreateLocalDir().then((void) => {
	console.info("==========================>getOrCreateLocalDirCallback=======================>");
});
```



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 94 95 96 97 98 99 100

```js
import featureAbility from '@ohos.ability.featureAbility'
import bundle from '@ohos.bundle'
var context = featureAbility.getContext();
var datainfo = await bundle.getBundleInfo('com.context.test',1);
context.verifyPermission("com.example.permission",datainfo.uid)

```



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 149 150 151 152 153 154 155

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
var Permission = context.PermissionOptions(1,1);
context.getOrCreateLocalDir('com.context.permission',Permission).then((void) => {
	console.info("==========================>verifyPermissionCallback=======================>");
});
```



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

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

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

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


W
wusongqing 已提交
165 166 167 168 169 170
| 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 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getOrCreateLocalDir(    
    ["com.example.permission1",
     "com.example.permission2",
     "com.example.permission3",
     "com.example.permission4",
     "com.example.permission5"],
    1,
)
```



W
wusongqing 已提交
187
### getApplicationInfo
W
wusongqing 已提交
188

W
wusongqing 已提交
189
getApplicationInfo(callback: AsyncCallback\<ApplicationInfo>)
W
wusongqing 已提交
190

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

W
wusongqing 已提交
193
**Parameters**
W
wusongqing 已提交
194

W
wusongqing 已提交
195 196 197
| Name| Type| Mandatory| Description|
| -------- | ------------------------------- | ---- | ------------------------ |
| callback | AsyncCallback\<ApplicationInfo> | Yes| Callback used to return the application information.|
W
wusongqing 已提交
198

W
wusongqing 已提交
199
**Example**
W
wusongqing 已提交
200 201 202 203 204 205 206 207 208

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



W
wusongqing 已提交
209
### getApplicationInfo
W
wusongqing 已提交
210

W
wusongqing 已提交
211
getApplicationInfo(): Promise\<ApplicationInfo>
W
wusongqing 已提交
212

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

W
wusongqing 已提交
215
**Return value**
W
wusongqing 已提交
216

W
wusongqing 已提交
217 218 219
| Type| Description|
| ------------------------- | ------------------ |
| Promise\<ApplicationInfo> | Promise used to return the application information.|
W
wusongqing 已提交
220

W
wusongqing 已提交
221
**Example**
W
wusongqing 已提交
222 223 224 225 226 227 228 229 230 231 232

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getApplicationInfo().then((void) => {
	console.info("==========================>getApplicationInfoCallback=======================>");
});
```



W
wusongqing 已提交
233
### getBundleName
W
wusongqing 已提交
234

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

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

W
wusongqing 已提交
239
**Parameters**
W
wusongqing 已提交
240

W
wusongqing 已提交
241 242 243
| Name| Type| Mandatory| Description|
| -------- | ---------------------- | ---- | ----------------------------- |
| callback | AsyncCallback\<string> | Yes| Callback used to return the bundle name.|
W
wusongqing 已提交
244

W
wusongqing 已提交
245
**Example**
W
wusongqing 已提交
246 247 248 249 250 251 252 253 254

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



W
wusongqing 已提交
255
### getBundleName
W
wusongqing 已提交
256

W
wusongqing 已提交
257
getBundleName(): Promise\<string>
W
wusongqing 已提交
258

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

W
wusongqing 已提交
261
**Return value**
W
wusongqing 已提交
262

W
wusongqing 已提交
263 264 265
| Type| Description|
| ---------------- | ------------------------- |
| Promise\<string> | Promise used to return the bundle name.|
W
wusongqing 已提交
266

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

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getBundleName().then((void) => {
	console.info("==========================>getBundleNameCallback=======================>");
});
```



W
wusongqing 已提交
279
### getProcessInfo
W
wusongqing 已提交
280

W
wusongqing 已提交
281
getProcessInfo(callback: AsyncCallback\<ProcessInfo>)
W
wusongqing 已提交
282

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

W
wusongqing 已提交
285
**Parameters**
W
wusongqing 已提交
286

W
wusongqing 已提交
287 288 289
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | -------------------- |
| callback | AsyncCallback\<ProcessInfo> | Yes| Callback used to return the process information.|
W
wusongqing 已提交
290

W
wusongqing 已提交
291
**Example**
W
wusongqing 已提交
292 293 294 295 296 297 298 299 300

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



W
wusongqing 已提交
301
### getProcessInfo
W
wusongqing 已提交
302

W
wusongqing 已提交
303
getProcessInfo(): Promise\<ProcessInfo>
W
wusongqing 已提交
304

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

W
wusongqing 已提交
307
**Return value**
W
wusongqing 已提交
308

W
wusongqing 已提交
309 310 311
| Type| Description|
| --------------------- | -------------- |
| Promise\<ProcessInfo> | Promise used to return the process information.|
W
wusongqing 已提交
312

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

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getProcessInfo().then((void) => {
	console.info("==========================>getProcessInfoCallback=======================>");
});
```



W
wusongqing 已提交
325
### getElementName
W
wusongqing 已提交
326

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

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

W
wusongqing 已提交
331
This method is available only to Page abilities.
W
wusongqing 已提交
332

W
wusongqing 已提交
333
**Parameters**
W
wusongqing 已提交
334

W
wusongqing 已提交
335 336 337
| Name| Type| Mandatory| Description|
| -------- | --------------------------- | ---- | ---------------------------------------------- |
| callback | AsyncCallback\<ElementName> | Yes| Callback used to return the **ohos.bundle.ElementName** object.|
W
wusongqing 已提交
338

W
wusongqing 已提交
339
**Example**
W
wusongqing 已提交
340 341 342 343 344 345 346 347 348

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



W
wusongqing 已提交
349
### getElementName
W
wusongqing 已提交
350

W
wusongqing 已提交
351
getElementName(): Promise\<ElementName>
W
wusongqing 已提交
352

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

W
wusongqing 已提交
355
This method is available only to Page abilities.
W
wusongqing 已提交
356

W
wusongqing 已提交
357
**Return value**
W
wusongqing 已提交
358

W
wusongqing 已提交
359 360
| Type| Description|
| --------------------- | ------------------------------------------ |
W
wusongqing 已提交
361
| Promise\<ElementName> | Promise used to return the **ohos.bundle.ElementName** object.|
W
wusongqing 已提交
362

W
wusongqing 已提交
363
**Example**
W
wusongqing 已提交
364 365 366 367 368 369 370 371 372

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getElementName().then((void) => {
	console.info("==========================>getElementNameCallback=======================>");
});
```

W
wusongqing 已提交
373
### getProcessName
W
wusongqing 已提交
374

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

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

W
wusongqing 已提交
379 380 381
| Name| Type| Mandatory| Description|
| -------- | ---------------------- | ---- | -------------------- |
| callback | AsyncCallback\<string> | Yes| Callback used to return the process name.|
W
wusongqing 已提交
382

W
wusongqing 已提交
383
**Example**
W
wusongqing 已提交
384 385 386 387 388 389 390 391 392

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



W
wusongqing 已提交
393
### getProcessName
W
wusongqing 已提交
394

W
wusongqing 已提交
395
getProcessName(): Promise\<string>
W
wusongqing 已提交
396

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

W
wusongqing 已提交
399
**Return value**
W
wusongqing 已提交
400

W
wusongqing 已提交
401 402
| Type| Description|
| ---------------- | -------------------- |
W
wusongqing 已提交
403
| Promise\<string> | Promise used to return the process name.|
W
wusongqing 已提交
404

W
wusongqing 已提交
405
**Example**
W
wusongqing 已提交
406 407 408 409 410 411 412 413 414 415 416

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getProcessName().then((void) => {
	console.info("==========================>getProcessNameCallback=======================>");
});
```



W
wusongqing 已提交
417
### getCallingBundle
W
wusongqing 已提交
418

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

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

W
wusongqing 已提交
423
**Parameters**
W
wusongqing 已提交
424

W
wusongqing 已提交
425 426 427
| Name| Type| Mandatory| Description|
| -------- | ---------------------- | ---- | ------------------------- |
| callback | AsyncCallback\<string> | Yes| Callback used to return the bundle name.|
W
wusongqing 已提交
428

W
wusongqing 已提交
429
**Example**
W
wusongqing 已提交
430 431 432 433 434 435 436 437 438

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



W
wusongqing 已提交
439
### getCallingBundle
W
wusongqing 已提交
440

W
wusongqing 已提交
441
getCallingBundle(): Promise\<string>
W
wusongqing 已提交
442

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

W
wusongqing 已提交
445
**Return value**
W
wusongqing 已提交
446

W
wusongqing 已提交
447 448 449
| Type| Description|
| --------------- | ------------------------- |
| Promise\<string> | Promise used to return the bundle name.|
W
wusongqing 已提交
450

W
wusongqing 已提交
451
**Example**
W
wusongqing 已提交
452 453 454 455 456 457 458 459

```js
import featureAbility from '@ohos.ability.featureAbility'
var context = featureAbility.getContext();
context.getCallingBundle().then((void) => {
	console.info("==========================>getCallingBundleCallback=======================>");
});
```
W
wusongqing 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

## 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.|