1# Context 2 3The **Context** module provides context for abilities or applications. It allows access to application-specific resources, as well as permission requests and verification. 4 5> **NOTE** 6> 7> 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. 8> The APIs of this module can be used only in the FA model. 9 10## Modules to Import 11 12```ts 13import featureAbility from '@ohos.ability.featureAbility'; 14``` 15 16## Usage 17 18The **Context** object is created in a **featureAbility** and returned through its [getContext](js-apis-ability-featureAbility.md#featureabilitygetcontext) API. Therefore, you must import the **@ohos.ability.featureAbility** package before using the **Context** module. An example is as follows: 19 20```ts 21import featureAbility from '@ohos.ability.featureAbility'; 22let context: featureAbility.Context = featureAbility.getContext(); 23context.getOrCreateLocalDir().then((data) => { 24 console.info('getOrCreateLocalDir data: ' + JSON.stringify(data)); 25}); 26``` 27 28## Context.getOrCreateLocalDir<sup>7+</sup> 29 30getOrCreateLocalDir(callback: AsyncCallback\<string>): void 31 32Obtains the local root directory of the application. This API uses an asynchronous callback to return the result. 33 34If this API is called for the first time, a root directory will be created. 35 36**System capability**: SystemCapability.Ability.AbilityRuntime.Core 37 38**Parameters** 39 40| Name | Type | Mandatory | Description | 41| -------- | ---------------------- | ---- | ------------- | 42| callback | AsyncCallback\<string> | Yes | Callback used to return the local root directory.| 43 44**Example** 45 46```ts 47import featureAbility from '@ohos.ability.featureAbility'; 48let context: featureAbility.Context = featureAbility.getContext(); 49context.getOrCreateLocalDir((error, data)=>{ 50 if (error && error.code !== 0) { 51 console.error('getOrCreateLocalDir fail, error: ${JSON.stringify(error)}'); 52 } else { 53 console.log('getOrCreateLocalDir success, data: ${JSON.stringify(data)}'); 54 } 55}); 56``` 57 58 59 60## Context.getOrCreateLocalDir<sup>7+</sup> 61 62getOrCreateLocalDir(): Promise\<string> 63 64Obtains the local root directory of the application. This API uses a promise to return the result. 65 66If this API is called for the first time, a root directory will be created. 67 68**System capability**: SystemCapability.Ability.AbilityRuntime.Core 69 70**Return value** 71 72| Type | Description | 73| ---------------- | ----------- | 74| Promise\<string> | Promise used to return the local root directory.| 75 76**Example** 77 78```ts 79import featureAbility from '@ohos.ability.featureAbility'; 80let context: featureAbility.Context = featureAbility.getContext(); 81context.getOrCreateLocalDir().then((data) => { 82 console.info('getOrCreateLocalDir data: ' + JSON.stringify(data)); 83}); 84``` 85 86## Context.verifyPermission<sup>7+</sup> 87 88verifyPermission(permission: string, options: PermissionOptions, callback: AsyncCallback\<number>): void 89 90Verifies whether a specific PID and UID have the given permission. This API uses an asynchronous callback to return the result. 91 92**System capability**: SystemCapability.Ability.AbilityRuntime.Core 93 94**Parameters** 95 96| Name | Type | Mandatory | Description | 97| ---------- | --------------------------------------- | ---- | -------------------- | 98| permission | string | Yes | Name of the permission to verify. | 99| options | [PermissionOptions](#permissionoptions7) | Yes | Permission options. | 100| callback | AsyncCallback\<number> | Yes | Callback used to return the permission verification result. The value **0** means that the PID and UID have the given permission, and the value **-1** means the opposite.| 101 102**Example** 103 104```ts 105import featureAbility from '@ohos.ability.featureAbility'; 106import bundle from '@ohos.bundle.bundleManager'; 107let context: featureAbility.Context = featureAbility.getContext(); 108bundle.getBundleInfo('com.context.test', 1, (err, datainfo) =>{ 109 context.verifyPermission('com.example.permission', {uid:datainfo.appInfo.uid}, (err, data) =>{ 110 console.info('verifyPermission err: ' + JSON.stringify(err) + 'data: ' + JSON.stringify(data)); 111 }); 112}); 113``` 114 115 116 117## Context.verifyPermission<sup>7+</sup> 118 119verifyPermission(permission: string, callback: AsyncCallback\<number>): void 120 121Verifies whether the current PID and UID have the given permission. This API uses an asynchronous callback to return the result. 122 123**System capability**: SystemCapability.Ability.AbilityRuntime.Core 124 125**Parameters** 126 127| Name | Type | Mandatory | Description | 128| ---------- | ---------------------- | ---- | -------------------- | 129| permission | string | Yes | Name of the permission to verify. | 130| callback | AsyncCallback\<number> | Yes | Callback used to return the permission verification result. The value **0** means that the PID and UID have the given permission, and the value **-1** means the opposite.| 131 132**Example** 133 134```ts 135import featureAbility from '@ohos.ability.featureAbility'; 136let context: featureAbility.Context = featureAbility.getContext(); 137context.verifyPermission('com.example.permission', (error, data) =>{ 138 if (error && error.code !== 0) { 139 console.error('verifyPermission fail, error: ${JSON.stringify(error)}'); 140 } else { 141 console.log('verifyPermission success, data: ${JSON.stringify(data)}'); 142 } 143}); 144``` 145 146## Context.verifyPermission<sup>7+</sup> 147 148verifyPermission(permission: string, options?: PermissionOptions): Promise\<number> 149 150Verifies whether a specific PID and UID have the given permission. This API uses a promise to return the result. 151 152**System capability**: SystemCapability.Ability.AbilityRuntime.Core 153 154**Parameters** 155 156| Name | Type | Mandatory | Description | 157| ---------- | --------------------------------------- | ---- | -------- | 158| permission | string | Yes | Name of the permission to verify.| 159| options | [PermissionOptions](#permissionoptions) | No | Permission options. | 160 161**Return value** 162 163| Type | Description | 164| ---------------- | ---------------------------------- | 165| Promise\<number> | Promise used to return the permission verification result. The value **0** means that the PID and UID have the given permission, and the value **-1** means the opposite.| 166 167**Example** 168 169```ts 170import featureAbility from '@ohos.ability.featureAbility'; 171let context: featureAbility.Context = featureAbility.getContext(); 172let Permission = {pid:1}; 173context.verifyPermission('com.context.permission',Permission).then((data) => { 174 console.info('verifyPermission data: ' + JSON.stringify(data)); 175}); 176``` 177 178 179 180## Context.requestPermissionsFromUser<sup>7+</sup> 181 182requestPermissionsFromUser(permissions: Array\<string>, requestCode: number, resultCallback: AsyncCallback<[PermissionRequestResult](#permissionrequestresult)>): void 183 184Requests certain permissions from the system. This API uses an asynchronous callback to return the result. 185 186**System capability**: SystemCapability.Ability.AbilityRuntime.Core 187 188**Parameters** 189 190| Name | Type | Mandatory | Description | 191| -------------- | ---------------------------------------- | ---- | ----------------------------------- | 192| permissions | Array\<string> | Yes | Permissions to request. This parameter cannot be **null**. | 193| requestCode | number | Yes | Request code to be passed to **PermissionRequestResult**.| 194| resultCallback | AsyncCallback<[PermissionRequestResult](#permissionrequestresult)> | Yes | Callback used to return the permission request result. | 195 196**Example** 197 198```ts 199import featureAbility from '@ohos.ability.featureAbility'; 200let context: featureAbility.Context = featureAbility.getContext(); 201context.requestPermissionsFromUser( 202 ['com.example.permission1', 203 'com.example.permission2', 204 'com.example.permission3', 205 'com.example.permission4', 206 'com.example.permission5'], 207 1, 208 (err, data) => { 209 console.info('requestPermissionsFromUser err: ' + JSON.stringify(err) + 'data: ' + JSON.stringify(data)); 210 } 211); 212``` 213 214 215## Context.requestPermissionsFromUser<sup>7+</sup> 216 217requestPermissionsFromUser(permissions: Array\<string>, requestCode: number): Promise\<[PermissionRequestResult](#permissionrequestresult7)> 218 219Requests certain permissions from the system. This API uses a promise to return the result. 220 221**System capability**: SystemCapability.Ability.AbilityRuntime.Core 222 223**Parameters** 224 225| Name | Type | Mandatory | Description | 226| -------------- | ------------------- | ----- | -------------------------------------------- | 227| permissions | Array\<string> | Yes | Permissions to request. This parameter cannot be **null**. | 228| requestCode | number | Yes | Request code to be passed to **PermissionRequestResult**.| 229 230**Return value** 231 232| Type | Description | 233| ------------------------------------------------------------- | ---------------- | 234| Promise\<[PermissionRequestResult](#permissionrequestresult7)> | Promise used to return the permission request result.| 235 236**Example** 237 238```ts 239import featureAbility from '@ohos.ability.featureAbility'; 240let context: featureAbility.Context = featureAbility.getContext(); 241context.requestPermissionsFromUser( 242 ['com.example.permission1', 243 'com.example.permission2', 244 'com.example.permission3', 245 'com.example.permission4', 246 'com.example.permission5'], 247 1).then((data)=>{ 248 console.info('requestPermissionsFromUser data: ' + JSON.stringify(data)); 249 } 250); 251``` 252 253 254 255## Context.getApplicationInfo<sup>7+</sup> 256 257getApplicationInfo(callback: AsyncCallback\<ApplicationInfo>): void 258 259Obtains information about the current application. This API uses an asynchronous callback to return the result. 260 261**System capability**: SystemCapability.Ability.AbilityRuntime.Core 262 263**Parameters** 264 265| Name | Type | Mandatory | Description | 266| -------- | ------------------------------- | ---- | ------------ | 267| callback | AsyncCallback\<[ApplicationInfo](js-apis-bundle-ApplicationInfo.md)> | Yes | Callback used to return the application information.| 268 269**Example** 270 271```ts 272import featureAbility from '@ohos.ability.featureAbility'; 273let context: featureAbility.Context = featureAbility.getContext(); 274context.getApplicationInfo((error, data) => { 275 if (error && error.code !== 0) { 276 console.error('getApplicationInfo fail, error: ${JSON.stringify(error)}'); 277 } else { 278 console.log('getApplicationInfo success, data: ${JSON.stringify(data)}'); 279 } 280}); 281``` 282 283 284 285## Context.getApplicationInfo<sup>7+</sup> 286 287getApplicationInfo(): Promise\<ApplicationInfo> 288 289Obtains information about the current application. This API uses a promise to return the result. 290 291**System capability**: SystemCapability.Ability.AbilityRuntime.Core 292 293**Return value** 294 295| Type | Description | 296| ------------------------- | --------- | 297| Promise\<[ApplicationInfo](js-apis-bundle-ApplicationInfo.md)> | Promise used to return the application information.| 298 299**Example** 300 301```ts 302import featureAbility from '@ohos.ability.featureAbility'; 303let context: featureAbility.Context = featureAbility.getContext(); 304context.getApplicationInfo().then((data) => { 305 console.info('getApplicationInfo data: ' + JSON.stringify(data)); 306}); 307``` 308 309 310 311## Context.getBundleName<sup>7+</sup> 312 313getBundleName(callback: AsyncCallback\<string>): void 314 315Obtains the bundle name of this ability. This API uses an asynchronous callback to return the result. 316 317**System capability**: SystemCapability.Ability.AbilityRuntime.Core 318 319**Parameters** 320 321| Name | Type | Mandatory | Description | 322| -------- | ---------------------- | ---- | ------------------ | 323| callback | AsyncCallback\<string> | Yes | Callback used to return the bundle name.| 324 325**Example** 326 327```ts 328import featureAbility from '@ohos.ability.featureAbility'; 329let context: featureAbility.Context = featureAbility.getContext(); 330context.getBundleName((error, data) => { 331 if (error && error.code !== 0) { 332 console.error('getBundleName fail, error: ${JSON.stringify(error)}'); 333 } else { 334 console.log('getBundleName success, data: ${JSON.stringify(data)}'); 335 } 336}); 337``` 338 339 340 341## Context.getBundleName<sup>7+</sup> 342 343getBundleName(): Promise\<string> 344 345Obtains the bundle name of this ability. This API uses a promise to return the result. 346 347**System capability**: SystemCapability.Ability.AbilityRuntime.Core 348 349**Return value** 350 351| Type | Description | 352| ---------------- | ---------------- | 353| Promise\<string> | Promise used to return the bundle name.| 354 355**Example** 356 357```ts 358import featureAbility from '@ohos.ability.featureAbility'; 359let context: featureAbility.Context = featureAbility.getContext(); 360context.getBundleName().then((data) => { 361 console.info('getBundleName data: ' + JSON.stringify(data)); 362}); 363``` 364 365## Context.getDisplayOrientation<sup>7+</sup> 366 367getDisplayOrientation(callback: AsyncCallback\<bundle.DisplayOrientation>): void 368 369Obtains the display orientation of this ability. This API uses an asynchronous callback to return the result. 370 371**System capability**: SystemCapability.Ability.AbilityRuntime.Core 372 373**Parameters** 374 375| Name | Type | Mandatory| Description | 376| -------- | ------------------------------------------------------------ | ---- | ------------------ | 377| callback | AsyncCallback\<[bundle.DisplayOrientation](js-apis-Bundle.md#displayorientation)> | Yes | Callback used to return the display orientation.| 378 379**Example** 380 381```ts 382import featureAbility from '@ohos.ability.featureAbility'; 383let context: featureAbility.Context = featureAbility.getContext(); 384context.getDisplayOrientation((error, data) => { 385 if (error && error.code !== 0) { 386 console.error('getDisplayOrientation fail, error: ${JSON.stringify(error)}'); 387 } else { 388 console.log('getDisplayOrientation success, data: ${JSON.stringify(data)}'); 389 } 390}); 391``` 392 393## Context.getDisplayOrientation<sup>7+</sup> 394 395getDisplayOrientation(): Promise\<bundle.DisplayOrientation>; 396 397Obtains the display orientation of this ability. This API uses a promise to return the result. 398 399**System capability**: SystemCapability.Ability.AbilityRuntime.Core 400 401**Return value** 402 403| Type | Description | 404| ---------------------------------------- | --------- | 405| Promise\<[bundle.DisplayOrientation](js-apis-Bundle.md#displayorientation)> | Promise used to return the display orientation.| 406 407**Example** 408 409```ts 410import featureAbility from '@ohos.ability.featureAbility'; 411let context: featureAbility.Context = featureAbility.getContext(); 412context.getDisplayOrientation().then((data) => { 413 console.info('getDisplayOrientation data: ' + JSON.stringify(data)); 414}); 415``` 416 417## Context.getExternalCacheDir 418 419getExternalCacheDir(callback: AsyncCallback\<string>): void 420 421Obtains the external cache directory of the application. This API uses an asynchronous callback to return the result. 422 423**System capability**: SystemCapability.Ability.AbilityRuntime.Core 424 425**Parameters** 426 427| Name | Type | Mandatory | Description | 428| -------- | ---------------------- | ---- | ------------------ | 429| callback | AsyncCallback\<string> | Yes | Callback used to return the absolute path of the cache directory.| 430 431**Example** 432 433```ts 434import featureAbility from '@ohos.ability.featureAbility'; 435let context: featureAbility.Context = featureAbility.getContext(); 436context.getExternalCacheDir((error, data) => { 437 if (error && error.code !== 0) { 438 console.error('getExternalCacheDir fail, error: ${JSON.stringify(error)}'); 439 } else { 440 console.log('getExternalCacheDir success, data: ${JSON.stringify(data)}'); 441 } 442}); 443``` 444 445## Context.getExternalCacheDir 446 447getExternalCacheDir(): Promise\<string>; 448 449Obtains the external cache directory of the application. This API uses a promise to return the result. 450 451**System capability**: SystemCapability.Ability.AbilityRuntime.Core 452 453**Return value** 454 455| Type | Description | 456| ---------------- | ---------------- | 457| Promise\<string> | Promise used to return the absolute path of the cache directory.| 458 459**Example** 460 461```ts 462import featureAbility from '@ohos.ability.featureAbility'; 463let context: featureAbility.Context = featureAbility.getContext(); 464context.getExternalCacheDir().then((data) => { 465 console.info('getExternalCacheDir data: ' + JSON.stringify(data)); 466}); 467``` 468 469## Context.setDisplayOrientation<sup>7+</sup> 470 471setDisplayOrientation(orientation: bundle.DisplayOrientation, callback: AsyncCallback\<void>): void 472 473Sets the display orientation for this ability. This API uses an asynchronous callback to return the result. 474 475**System capability**: SystemCapability.Ability.AbilityRuntime.Core 476 477**Parameters** 478 479| Name | Type | Mandatory | Description | 480| ----------- | ---------------------------------------- | ---- | ------------ | 481| orientation | [bundle.DisplayOrientation](js-apis-Bundle.md#displayorientation) | Yes | Display orientation to set.| 482| callback | AsyncCallback\<void> | Yes | Callback used to return the display orientation. | 483 484**Example** 485 486```ts 487import featureAbility from '@ohos.ability.featureAbility'; 488import bundle from '@ohos.bundle'; 489let context: featureAbility.Context = featureAbility.getContext(); 490let orientation = bundle.DisplayOrientation.UNSPECIFIED; 491context.setDisplayOrientation(orientation, (error) => { 492 console.error('setDisplayOrientation fail, error: ${JSON.stringify(error)}'); 493}); 494``` 495 496## Context.setDisplayOrientation<sup>7+</sup> 497 498setDisplayOrientation(orientation: bundle.DisplayOrientation): Promise\<void>; 499 500Sets the display orientation for this ability. This API uses a promise to return the result. 501 502**System capability**: SystemCapability.Ability.AbilityRuntime.Core 503 504**Return value** 505 506| Type | Description | 507| ---------------------------------------- | ---------------------------------------- | 508| orientation | [bundle.DisplayOrientation](js-apis-Bundle.md#displayorientation) | 509| Promise\<void> | Promise used to return the display orientation. | 510 511**Example** 512 513```ts 514import featureAbility from '@ohos.ability.featureAbility'; 515import bundle from '@ohos.bundle'; 516let context: featureAbility.Context = featureAbility.getContext(); 517let orientation = bundle.DisplayOrientation.UNSPECIFIED; 518context.setDisplayOrientation(orientation).then((data) => { 519 console.info('setDisplayOrientation data: ' + JSON.stringify(data)); 520}); 521``` 522 523## Context.setShowOnLockScreen<sup>(deprecated)</sup> 524 525setShowOnLockScreen(show: boolean, callback: AsyncCallback\<void>): void 526 527Sets whether to show this feature at the top of the lock screen so that the feature remains activated. This API uses an asynchronous callback to return the result. 528> This API is deprecated since API version 9. You are advised to use [window.setShowOnLockScreen](js-apis-window.md#setShowOnLockScreen9). 529 530**System capability**: SystemCapability.Ability.AbilityRuntime.Core 531 532**Parameters** 533 534| Name | Type | Mandatory | Description | 535| -------- | -------------------- | ---- | ---------------------------------------- | 536| show | boolean | Yes | Whether to show this feature at the top of the lock screen. The value **true** means to show this feature at the top of the lock screen, and **false** means the opposite.| 537| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 538 539**Example** 540 541```ts 542import featureAbility from '@ohos.ability.featureAbility'; 543let context: featureAbility.Context = featureAbility.getContext(); 544let show = true; 545context.setShowOnLockScreen(show, (error) => { 546 console.error('setShowOnLockScreen fail, error: ${JSON.stringify(error)}'); 547}); 548``` 549 550## Context.setShowOnLockScreen<sup>(deprecated)</sup> 551 552setShowOnLockScreen(show: boolean): Promise\<void>; 553 554Sets whether to show this feature at the top of the lock screen so that the feature remains activated. This API uses a promise to return the result. 555> This API is deprecated since API version 9. You are advised to use [window.setShowOnLockScreen](js-apis-window.md#setShowOnLockScreen9). 556 557**System capability**: SystemCapability.Ability.AbilityRuntime.Core 558 559**Parameters** 560 561| Name | Type | Mandatory | Description | 562| ---- | ------- | ---- | ---------------------------------------- | 563| show | boolean | Yes | Whether to show this feature at the top of the lock screen. The value **true** means to show this feature at the top of the lock screen, and **false** means the opposite.| 564 565**Return value** 566 567| Type | Description | 568| -------------- | --------------- | 569| Promise\<void> | Promise used to return the result.| 570 571**Example** 572 573```ts 574import featureAbility from '@ohos.ability.featureAbility'; 575let context: featureAbility.Context = featureAbility.getContext(); 576let show = true; 577context.setShowOnLockScreen(show).then((data) => { 578 console.info('setShowOnLockScreen data: ' + JSON.stringify(data)); 579}); 580``` 581 582## Context.setWakeUpScreen<sup>7+</sup> 583 584setWakeUpScreen(wakeUp: boolean, callback: AsyncCallback\<void>): void 585 586Sets whether to wake up the screen when this feature is restored. This API uses an asynchronous callback to return the result. 587 588**System capability**: SystemCapability.Ability.AbilityRuntime.Core 589 590**Parameters** 591 592| Name | Type | Mandatory | Description | 593| -------- | -------------------- | ---- | --------------------------------- | 594| wakeUp | boolean | Yes | Whether to wake up the screen. The value **true** means to wake up the screen, and **false** means the opposite.| 595| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 596 597**Example** 598 599```ts 600import featureAbility from '@ohos.ability.featureAbility'; 601let context: featureAbility.Context = featureAbility.getContext(); 602let wakeUp = true; 603context.setWakeUpScreen(wakeUp, (error) => { 604 console.error('setWakeUpScreen fail, error: ${JSON.stringify(error)}'); 605}); 606``` 607 608## Context.setWakeUpScreen<sup>7+</sup> 609 610setWakeUpScreen(wakeUp: boolean): Promise\<void>; 611 612Sets whether to wake up the screen when this feature is restored. This API uses a promise to return the result. 613 614**System capability**: SystemCapability.Ability.AbilityRuntime.Core 615 616**Parameters** 617 618| Name | Type | Mandatory | Description | 619| ------ | ------- | ---- | --------------------------------- | 620| wakeUp | boolean | Yes | Whether to wake up the screen. The value **true** means to wake up the screen, and **false** means the opposite.| 621 622**Return value** 623 624| Type | Description | 625| -------------- | --------------- | 626| Promise\<void> | Promise used to return the result.| 627 628**Example** 629 630```ts 631import featureAbility from '@ohos.ability.featureAbility'; 632let context: featureAbility.Context = featureAbility.getContext(); 633let wakeUp = true; 634context.setWakeUpScreen(wakeUp).then((data) => { 635 console.info('setWakeUpScreen data: ' + JSON.stringify(data)); 636}); 637``` 638 639 640 641 642## Context.getProcessInfo<sup>7+</sup> 643 644getProcessInfo(callback: AsyncCallback\<ProcessInfo>): void 645 646Obtains information about the current process, including the PID and process name. This API uses an asynchronous callback to return the result. 647 648**System capability**: SystemCapability.Ability.AbilityRuntime.Core 649 650**Parameters** 651 652| Name | Type | Mandatory | Description | 653| -------- | --------------------------- | ---- | ---------- | 654| callback | AsyncCallback\<[ProcessInfo](js-apis-inner-app-processInfo.md)> | Yes | Callback used to return the process information.| 655 656**Example** 657 658```ts 659import featureAbility from '@ohos.ability.featureAbility'; 660let context: featureAbility.Context = featureAbility.getContext(); 661context.getProcessInfo((error, data) => { 662 if (error && error.code !== 0) { 663 console.error('getProcessInfo fail, error: ${JSON.stringify(error)}'); 664 } else { 665 console.log('getProcessInfo success, data: ${JSON.stringify(data)}'); 666 } 667}); 668``` 669 670 671 672## Context.getProcessInfo<sup>7+</sup> 673 674getProcessInfo(): Promise\<ProcessInfo> 675 676Obtains information about the current process, including the PID and process name. This API uses a promise to return the result. 677 678**System capability**: SystemCapability.Ability.AbilityRuntime.Core 679 680**Return value** 681 682| Type | Description | 683| --------------------- | ------- | 684| Promise\<[ProcessInfo](js-apis-inner-app-processInfo.md)> | Promise used to return the process information.| 685 686**Example** 687 688```ts 689import featureAbility from '@ohos.ability.featureAbility'; 690let context: featureAbility.Context = featureAbility.getContext(); 691context.getProcessInfo().then((data) => { 692 console.info('getProcessInfo data: ' + JSON.stringify(data)); 693}); 694``` 695 696 697 698## Context.getElementName<sup>7+</sup> 699 700getElementName(callback: AsyncCallback\<ElementName>): void 701 702Obtains the **ohos.bundle.ElementName** object of this ability. This API uses an asynchronous callback to return the result. 703 704This API is available only to Page abilities. 705 706**System capability**: SystemCapability.Ability.AbilityRuntime.Core 707 708**Parameters** 709 710| Name | Type | Mandatory | Description | 711| -------- | --------------------------- | ---- | -------------------------------------- | 712| callback | AsyncCallback\<[ElementName](js-apis-bundle-ElementName.md)> | Yes | Callback used to return the **ohos.bundle.ElementName** object.| 713 714**Example** 715 716```ts 717import featureAbility from '@ohos.ability.featureAbility'; 718let context: featureAbility.Context = featureAbility.getContext(); 719context.getElementName((error, data) => { 720 if (error && error.code !== 0) { 721 console.error('getElementName fail, error: ${JSON.stringify(error)}'); 722 } else { 723 console.log('getElementName success, data: ${JSON.stringify(data)}'); 724 } 725}); 726``` 727 728 729 730## Context.getElementName<sup>7+</sup> 731 732getElementName(): Promise\<ElementName> 733 734Obtains the **ohos.bundle.ElementName** object of this ability. This API uses a promise to return the result. 735 736This API is available only to Page abilities. 737 738**System capability**: SystemCapability.Ability.AbilityRuntime.Core 739 740**Return value** 741 742| Type | Description | 743| --------------------- | ------------------------------------ | 744| Promise\<[ElementName](js-apis-bundle-ElementName.md)> | Promise used to return the **ohos.bundle.ElementName** object.| 745 746**Example** 747 748```ts 749import featureAbility from '@ohos.ability.featureAbility'; 750let context: featureAbility.Context = featureAbility.getContext(); 751context.getElementName().then((data) => { 752 console.info('getElementName data: ' + JSON.stringify(data)); 753}); 754``` 755 756## Context.getProcessName<sup>7+</sup> 757 758getProcessName(callback: AsyncCallback\<string>): void 759 760Obtains the name of the current process. This API uses an asynchronous callback to return the result. 761 762**System capability**: SystemCapability.Ability.AbilityRuntime.Core 763 764**Parameters** 765 766| Name | Type | Mandatory | Description | 767| -------- | ---------------------- | ---- | ---------- | 768| callback | AsyncCallback\<string> | Yes | Callback used to return the process name.| 769 770**Example** 771 772```ts 773import featureAbility from '@ohos.ability.featureAbility'; 774let context: featureAbility.Context = featureAbility.getContext(); 775context.getProcessName((error, data) => { 776 if (error && error.code !== 0) { 777 console.error('getProcessName fail, error: ${JSON.stringify(error)}'); 778 } else { 779 console.log('getProcessName success, data: ${JSON.stringify(data)}'); 780 } 781}); 782``` 783 784 785 786## Context.getProcessName<sup>7+</sup> 787 788getProcessName(): Promise\<string> 789 790Obtains the name of the current process. This API uses a promise to return the result. 791 792**System capability**: SystemCapability.Ability.AbilityRuntime.Core 793 794**Return value** 795 796| Type | Description | 797| ---------------- | ---------- | 798| Promise\<string> | Promise used to return the process name.| 799 800**Example** 801 802```ts 803import featureAbility from '@ohos.ability.featureAbility'; 804let context: featureAbility.Context = featureAbility.getContext(); 805context.getProcessName().then((data) => { 806 console.info('getProcessName data: ' + JSON.stringify(data)); 807}); 808``` 809 810 811 812## Context.getCallingBundle<sup>7+</sup> 813 814getCallingBundle(callback: AsyncCallback\<string>): void 815 816Obtains the bundle name of the calling ability. This API uses an asynchronous callback to return the result. 817 818**System capability**: SystemCapability.Ability.AbilityRuntime.Core 819 820**Parameters** 821 822| Name | Type | Mandatory | Description | 823| -------- | ---------------------- | ---- | ---------------- | 824| callback | AsyncCallback\<string> | Yes | Callback used to return the bundle name.| 825 826**Example** 827 828```ts 829import featureAbility from '@ohos.ability.featureAbility'; 830let context: featureAbility.Context = featureAbility.getContext(); 831context.getCallingBundle((error, data) => { 832 if (error && error.code !== 0) { 833 console.error('getCallingBundle fail, error: ${JSON.stringify(error)}'); 834 } else { 835 console.log('getCallingBundle success, data: ${JSON.stringify(data)}'); 836 } 837}); 838``` 839 840 841 842## Context.getCallingBundle<sup>7+</sup> 843 844getCallingBundle(): Promise\<string> 845 846Obtains the bundle name of the calling ability. This API uses a promise to return the result. 847 848**System capability**: SystemCapability.Ability.AbilityRuntime.Core 849 850**Return value** 851 852| Type | Description | 853| ---------------- | -------------- | 854| Promise\<string> | Promise used to return the bundle name.| 855 856**Example** 857 858```ts 859import featureAbility from '@ohos.ability.featureAbility'; 860let context: featureAbility.Context = featureAbility.getContext(); 861context.getCallingBundle().then((data) => { 862 console.info('getCallingBundle data: ' + JSON.stringify(data)); 863}); 864``` 865 866## Context.getCacheDir 867 868getCacheDir(callback: AsyncCallback\<string>): void 869 870Obtains the cache directory of the application in the internal storage. This API uses an asynchronous callback to return the result. 871 872**System capability**: SystemCapability.Ability.AbilityRuntime.Core 873 874**Parameters** 875 876| Name | Type | Mandatory | Description | 877| -------- | ---------------------- | ---- | --------------- | 878| callback | AsyncCallback\<string> | Yes | Callback used to return the cache directory.| 879 880**Example** 881 882```ts 883import featureAbility from '@ohos.ability.featureAbility'; 884let context: featureAbility.Context = featureAbility.getContext(); 885context.getCacheDir((error, data) => { 886 if (error && error.code !== 0) { 887 console.error('getCacheDir fail, error: ${JSON.stringify(error)}'); 888 } else { 889 console.log('getCacheDir success, data: ${JSON.stringify(data)}'); 890 } 891}); 892``` 893 894## Context.getCacheDir 895 896getCacheDir(): Promise\<string> 897 898Obtains the cache directory of the application in the internal storage. This API uses a promise to return the result. 899 900**System capability**: SystemCapability.Ability.AbilityRuntime.Core 901 902**Return value** 903 904| Type | Description | 905| ---------------- | --------------- | 906| Promise\<string> | Promise used to return the cache directory.| 907 908**Example** 909 910```ts 911import featureAbility from '@ohos.ability.featureAbility'; 912let context: featureAbility.Context = featureAbility.getContext(); 913context.getCacheDir().then((data) => { 914 console.info('getCacheDir data: ' + JSON.stringify(data)); 915}); 916``` 917 918## Context.getFilesDir 919 920getFilesDir(callback: AsyncCallback\<string>): void 921 922Obtains the file directory of the application in the internal storage. This API uses an asynchronous callback to return the result. 923 924**System capability**: SystemCapability.Ability.AbilityRuntime.Core 925 926**Parameters** 927 928| Name | Type | Mandatory | Description | 929| -------- | ---------------------- | ---- | ------------------- | 930| callback | AsyncCallback\<string> | Yes | Callback used to return the file directory.| 931 932**Example** 933 934```ts 935import featureAbility from '@ohos.ability.featureAbility'; 936let context: featureAbility.Context = featureAbility.getContext(); 937context.getFilesDir((error, data) => { 938 if (error && error.code !== 0) { 939 console.error('getFilesDir fail, error: ${JSON.stringify(error)}'); 940 } else { 941 console.log('getFilesDir success, data: ${JSON.stringify(data)}'); 942 } 943}); 944``` 945 946## Context.getFilesDir 947 948getFilesDir(): Promise\<string> 949 950Obtains the file directory of the application in the internal storage. This API uses a promise to return the result. 951 952**System capability**: SystemCapability.Ability.AbilityRuntime.Core 953 954**Return value** 955 956| Type | Description | 957| ---------------- | ------------------- | 958| Promise\<string> | Promise used to return the file directory.| 959 960**Example** 961 962```ts 963import featureAbility from '@ohos.ability.featureAbility'; 964let context: featureAbility.Context = featureAbility.getContext(); 965context.getFilesDir().then((data) => { 966 console.info('getFilesDir data: ' + JSON.stringify(data)); 967}); 968``` 969 970## Context.getOrCreateDistributedDir<sup>7+</sup> 971 972getOrCreateDistributedDir(callback: AsyncCallback\<string>): void 973 974Obtains the distributed file path for storing ability or application data files. This API uses an asynchronous callback to return the result. 975 976If the file path does not exist, the system will create one and return the created path. 977 978**System capability**: SystemCapability.Ability.AbilityRuntime.Core 979 980**Parameters** 981 982| Name | Type | Mandatory | Description | 983| -------- | ---------------------- | ---- | ---------------------------------------- | 984| callback | AsyncCallback\<string> | Yes | Callback used to return the distributed file path. If the file path does not exist, the system will create one and return the created path.| 985 986**Example** 987 988```ts 989import featureAbility from '@ohos.ability.featureAbility'; 990let context: featureAbility.Context = featureAbility.getContext(); 991context.getOrCreateDistributedDir((error, data) => { 992 if (error && error.code !== 0) { 993 console.error('getOrCreateDistributedDir fail, error: ${JSON.stringify(error)}'); 994 } else { 995 console.log('getOrCreateDistributedDir success, data: ${JSON.stringify(data)}'); 996 } 997}); 998``` 999 1000## Context.getOrCreateDistributedDir<sup>7+</sup> 1001 1002getOrCreateDistributedDir(): Promise\<string> 1003 1004Obtains the distributed file path for storing ability or application data files. This API uses a promise to return the result. 1005 1006If the distributed file path does not exist, the system will create one and return the created path. 1007 1008**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1009 1010**Return value** 1011 1012| Type | Description | 1013| ---------------- | ----------------------------------- | 1014| Promise\<string> | Promise used to return the distributed file path. If this API is called for the first time, a path will be created.| 1015 1016**Example** 1017 1018```ts 1019import featureAbility from '@ohos.ability.featureAbility'; 1020let context: featureAbility.Context = featureAbility.getContext(); 1021context.getOrCreateDistributedDir().then((data) => { 1022 console.info('getOrCreateDistributedDir data: ' + JSON.stringify(data)); 1023}); 1024``` 1025 1026## Context.getAppType<sup>7+</sup> 1027 1028getAppType(callback: AsyncCallback\<string>): void 1029 1030Obtains the application type. This API uses an asynchronous callback to return the result. 1031 1032**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1033 1034**Parameters** 1035 1036| Name | Type | Mandatory | Description | 1037| -------- | ---------------------- | ---- | -------------------------------- | 1038| callback | AsyncCallback\<string> | Yes | Callback used to return the application type.| 1039 1040**Example** 1041 1042```ts 1043import featureAbility from '@ohos.ability.featureAbility'; 1044let context: featureAbility.Context = featureAbility.getContext(); 1045context.getAppType((error, data) => { 1046 if (error && error.code !== 0) { 1047 console.error('getAppType fail, error: ${JSON.stringify(error)}'); 1048 } else { 1049 console.log('getAppType success, data: ${JSON.stringify(data)}'); 1050 } 1051}); 1052``` 1053 1054## Context.getAppType<sup>7+</sup> 1055 1056getAppType(): Promise\<string> 1057 1058Obtains the application type. This API uses a promise to return the result. 1059 1060**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1061 1062**Return value** 1063 1064| Type | Description | 1065| ---------------- | ------------------ | 1066| Promise\<string> | Promise used to return the application type.| 1067 1068**Example** 1069 1070```ts 1071import featureAbility from '@ohos.ability.featureAbility'; 1072let context: featureAbility.Context = featureAbility.getContext(); 1073context.getAppType().then((data) => { 1074 console.info('getAppType data: ' + JSON.stringify(data)); 1075}); 1076``` 1077 1078## Context.getHapModuleInfo<sup>7+</sup> 1079 1080getHapModuleInfo(callback: AsyncCallback\<HapModuleInfo>): void 1081 1082Obtains the **ModuleInfo** object of the application. This API uses an asynchronous callback to return the result. 1083 1084**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1085 1086**Parameters** 1087 1088| Name | Type | Mandatory | Description | 1089| -------- | ---------------------------------------- | ---- | --------------------------------------- | 1090| callback | AsyncCallback\<[HapModuleInfo](js-apis-bundle-HapModuleInfo.md)> | Yes | Callback used to return the **ModuleInfo** object.| 1091 1092**Example** 1093 1094```ts 1095import featureAbility from '@ohos.ability.featureAbility'; 1096let context: featureAbility.Context = featureAbility.getContext(); 1097context.getHapModuleInfo((error, data) => { 1098 if (error && error.code !== 0) { 1099 console.error('getHapModuleInfo fail, error: ${JSON.stringify(error)}'); 1100 } else { 1101 console.log('getHapModuleInfo success, data: ${JSON.stringify(data)}'); 1102 } 1103}); 1104``` 1105 1106## Context.getHapModuleInfo<sup>7+</sup> 1107 1108getHapModuleInfo(): Promise\<HapModuleInfo> 1109 1110Obtains the **ModuleInfo** object of the application. This API uses a promise to return the result. 1111 1112**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1113 1114**Return value** 1115 1116| Type | Description | 1117| ---------------------------------------- | ------------------ | 1118| Promise\<[HapModuleInfo](js-apis-bundle-HapModuleInfo.md)> | Promise used to return the **ModuleInfo** object.| 1119 1120**Example** 1121 1122```ts 1123import featureAbility from '@ohos.ability.featureAbility'; 1124let context: featureAbility.Context = featureAbility.getContext(); 1125context.getHapModuleInfo().then((data) => { 1126 console.info('getHapModuleInfo data: ' + JSON.stringify(data)); 1127}); 1128``` 1129 1130## Context.getAppVersionInfo<sup>7+</sup> 1131 1132getAppVersionInfo(callback: AsyncCallback\<AppVersionInfo>): void 1133 1134Obtains the version information of the application. This API uses an asynchronous callback to return the result. 1135 1136**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1137 1138**Parameters** 1139 1140| Name | Type | Mandatory | Description | 1141| -------- | ---------------------------------------- | ---- | ------------------------------ | 1142| callback | AsyncCallback\<[AppVersionInfo](js-apis-inner-app-appVersionInfo.md)> | Yes | Callback used to return the version information.| 1143 1144**Example** 1145 1146```ts 1147import featureAbility from '@ohos.ability.featureAbility'; 1148let context: featureAbility.Context = featureAbility.getContext(); 1149context.getAppVersionInfo((error, data) => { 1150 if (error && error.code !== 0) { 1151 console.error('getAppVersionInfo fail, error: ${JSON.stringify(error)}'); 1152 } else { 1153 console.log('getAppVersionInfo success, data: ${JSON.stringify(data)}'); 1154 } 1155}); 1156``` 1157 1158## Context.getAppVersionInfo<sup>7+</sup> 1159 1160getAppVersionInfo(): Promise\<AppVersionInfo> 1161 1162Obtains the version information of the application. This API uses a promise to return the result. 1163 1164**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1165 1166**Return value** 1167 1168| Type | Description | 1169| ---------------------------------------- | --------- | 1170| Promise\<[AppVersionInfo](js-apis-inner-app-appVersionInfo.md)> | Promise used to return the version information.| 1171 1172**Example** 1173 1174```ts 1175import featureAbility from '@ohos.ability.featureAbility'; 1176let context: featureAbility.Context = featureAbility.getContext(); 1177context.getAppVersionInfo().then((data) => { 1178 console.info('getAppVersionInfo data: ' + JSON.stringify(data)); 1179}); 1180``` 1181 1182## Context.getAbilityInfo<sup>7+</sup> 1183 1184getAbilityInfo(callback: AsyncCallback\<AbilityInfo>): void 1185 1186Obtains information about this ability. This API uses an asynchronous callback to return the result. 1187 1188**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1189 1190**Parameters** 1191 1192| Name | Type | Mandatory | Description | 1193| -------- | ---------------------------------------- | ---- | --------------------------------------- | 1194| callback | AsyncCallback\<[AbilityInfo](js-apis-bundle-AbilityInfo.md)> | Yes | Callback used to return the ability information.| 1195 1196**Example** 1197 1198```ts 1199import featureAbility from '@ohos.ability.featureAbility'; 1200let context: featureAbility.Context = featureAbility.getContext(); 1201context.getAbilityInfo((error, data) => { 1202 if (error && error.code !== 0) { 1203 console.error('getAbilityInfo fail, error: ${JSON.stringify(error)}'); 1204 } else { 1205 console.log('getAbilityInfo success, data: ${JSON.stringify(data)}'); 1206 } 1207}); 1208``` 1209 1210## Context.getAbilityInfo<sup>7+</sup> 1211 1212getAbilityInfo(): Promise\<AbilityInfo> 1213 1214Obtains information about this ability. This API uses a promise to return the result. 1215 1216**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1217 1218**Return value** 1219 1220| Type | Description | 1221| ---------------------------------------- | ------------------ | 1222| Promise\<[AbilityInfo](js-apis-bundle-AbilityInfo.md)> | Promise used to return the ability information.| 1223 1224**Example** 1225 1226```ts 1227import featureAbility from '@ohos.ability.featureAbility'; 1228let context: featureAbility.Context = featureAbility.getContext(); 1229context.getAbilityInfo().then((data) => { 1230 console.info('getAbilityInfo data: ' + JSON.stringify(data)); 1231}); 1232``` 1233 1234## Context.getApplicationContext<sup>7+</sup> 1235 1236getApplicationContext(): Context 1237 1238Obtains the context of the application. 1239 1240**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1241 1242**Return value** 1243 1244| Type | Description | 1245| ------- | ---------- | 1246| Context | Application context.| 1247 1248**Example** 1249 1250```ts 1251import featureAbility from '@ohos.ability.featureAbility'; 1252let context: featureAbility.Context = featureAbility.getContext().getApplicationContext(); 1253``` 1254 1255## Context.isUpdatingConfigurations<sup>7+</sup> 1256 1257isUpdatingConfigurations(callback: AsyncCallback\<boolean>): void; 1258 1259Checks whether the configuration of this ability is being updated. This API uses an asynchronous callback to return the result. 1260 1261**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1262 1263**Parameters** 1264 1265| Name | Type | Mandatory | Description | 1266| -------- | ----------------------- | ---- | ----------------------------- | 1267| callback | AsyncCallback\<boolean> | Yes | Returns **true** if the configuration of the capability is being updated; returns **false** otherwise.| 1268 1269**Example** 1270 1271```ts 1272import featureAbility from '@ohos.ability.featureAbility'; 1273let context: featureAbility.Context = featureAbility.getContext(); 1274context.isUpdatingConfigurations((error, data) => { 1275 if (error && error.code !== 0) { 1276 console.error('isUpdatingConfigurations fail, error: ${JSON.stringify(error)}'); 1277 } else { 1278 console.log('isUpdatingConfigurations success, data: ${JSON.stringify(data)}'); 1279 } 1280}); 1281``` 1282 1283## Context.isUpdatingConfigurations<sup>7+</sup> 1284 1285isUpdatingConfigurations(): Promise\<boolean>; 1286 1287Checks whether the configuration of this ability is being updated. This API uses a promise to return the result. 1288 1289**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1290 1291**Return value** 1292 1293| Type | Description | 1294| ----------------- | ----------------------------- | 1295| Promise\<boolean> | Returns **true** if the configuration of the capability is being updated; returns **false** otherwise.| 1296 1297**Example** 1298 1299```ts 1300import featureAbility from '@ohos.ability.featureAbility'; 1301let context: featureAbility.Context = featureAbility.getContext(); 1302context.isUpdatingConfigurations().then((data) => { 1303 console.info('isUpdatingConfigurations data: ' + JSON.stringify(data)); 1304}); 1305``` 1306 1307## Context.printDrawnCompleted<sup>7+</sup> 1308 1309printDrawnCompleted(callback: AsyncCallback\<void>): void; 1310 1311Notifies the system of the time required to draw this page function. This API uses an asynchronous callback to return the result. 1312 1313**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1314 1315**Parameters** 1316 1317| Name | Type | Mandatory | Description | 1318| -------- | -------------------- | ---- | ----------- | 1319| callback | AsyncCallback\<void> | Yes | Callback used to return the result.| 1320 1321**Example** 1322 1323```ts 1324import featureAbility from '@ohos.ability.featureAbility'; 1325let context: featureAbility.Context = featureAbility.getContext(); 1326context.printDrawnCompleted((err) => { 1327 console.error('printDrawnCompleted err: ' + JSON.stringify(err)); 1328}); 1329``` 1330 1331## Context.printDrawnCompleted<sup>7+</sup> 1332 1333printDrawnCompleted(): Promise\<void>; 1334 1335Notifies the system of the time required to draw this page function. This API uses a promise to return the result. 1336 1337**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1338 1339**Return value** 1340 1341| Type | Description | 1342| -------------- | --------------- | 1343| Promise\<void> | Promise used to return the result.| 1344 1345**Example** 1346 1347```ts 1348import featureAbility from '@ohos.ability.featureAbility'; 1349let context: featureAbility.Context = featureAbility.getContext(); 1350context.printDrawnCompleted().then((data) => { 1351 console.info('printDrawnCompleted data: ' + JSON.stringify(data)); 1352}); 1353``` 1354 1355 1356## PermissionOptions<sup>7+</sup> 1357 1358**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1359 1360| Name | Readable/Writable| Type | Mandatory | Description | 1361| ---- | ---- | ------ | ---- | ----- | 1362| pid | Read-only | number | No | Process ID.| 1363| uid | Read-only | number | No | User ID.| 1364 1365## PermissionRequestResult<sup>7+</sup> 1366 1367**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1368 1369| Name | Readable/Writable| Type | Mandatory | Description | 1370| ----------- | ---- | -------------- | ---- | ---------- | 1371| requestCode | Read-only | number | Yes | Request code passed.| 1372| permissions | Read-only | Array\<string> | Yes | Permissions requested. | 1373| authResults | Read-only | Array\<number> | Yes | Permission request result. | 1374