1# UIAbilityContext 2 3The **UIAbilityContext** module, inherited from **Context**, implements the context for UIAbilities. 4 5This module provides APIs for accessing UIAbility-specific resources. You can use the APIs to start and terminate a UIAbility, obtain the caller interface, and request permissions from users by displaying a dialog box. 6 7> **NOTE** 8> 9> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 10> - The APIs of this module can be used only in the stage model. 11 12## Attributes 13 14**System capability**: SystemCapability.Ability.AbilityRuntime.Core 15 16| Name| Type| Readable| Writable| Description| 17| -------- | -------- | -------- | -------- | -------- | 18| abilityInfo | [AbilityInfo](js-apis-bundleManager-abilityInfo.md) | Yes| No| Ability information.| 19| currentHapModuleInfo | [HapModuleInfo](js-apis-bundleManager-hapModuleInfo.md) | Yes| No| Information about the current HAP.| 20| config | [Configuration](js-apis-app-ability-configuration.md) | Yes| No| Configuration information.| 21 22## UIAbilityContext.startAbility 23 24startAbility(want: Want, callback: AsyncCallback<void>): void; 25 26Starts an ability. This API uses an asynchronous callback to return the result. 27 28Observe the following when using this API: 29 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 30 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 31 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 32 33**System capability**: SystemCapability.Ability.AbilityRuntime.Core 34 35**Parameters** 36 37| Name| Type| Mandatory| Description| 38| -------- | -------- | -------- | -------- | 39| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 40| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 41 42**Error codes** 43 44| ID| Error Message| 45| ------- | -------------------------------- | 46| 16000001 | The specified ability does not exist. | 47| 16000002 | Incorrect ability type. | 48| 16000004 | Can not start invisible component. | 49| 16000005 | The specified process does not have the permission. | 50| 16000006 | Cross-user operations are not allowed. | 51| 16000008 | The crowdtesting application expires. | 52| 16000009 | An ability cannot be started or stopped in Wukong mode. | 53| 16000010 | The call with the continuation flag is forbidden. | 54| 16000011 | The context does not exist. | 55| 16000017 | The previous ability is starting, wait start later. | 56| 16000050 | Internal error. | 57| 16000053 | The ability is not on the top of the UI. | 58| 16000055 | Installation-free timed out. | 59| 16200001 | The caller has been released. | 60 61**Example** 62 63 ```ts 64 let want = { 65 bundleName: 'com.example.myapp', 66 abilityName: 'MyAbility' 67 }; 68 69 try { 70 this.context.startAbility(want, (error) => { 71 if (error.code) { 72 // Process service logic errors. 73 console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) + 74 ' error.message: ' + JSON.stringify(error.message)); 75 return; 76 } 77 // Carry out normal service processing. 78 console.log('startAbility succeed'); 79 }); 80 } catch (paramError) { 81 // Process input parameter errors. 82 console.log('error.code: ' + JSON.stringify(paramError.code) + 83 ' error.message: ' + JSON.stringify(paramError.message)); 84 } 85 ``` 86 87 88## UIAbilityContext.startAbility 89 90startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void; 91 92Starts an ability with the start options specified. This API uses an asynchronous callback to return the result. 93 94Observe the following when using this API: 95 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 96 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 97 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 98 99**System capability**: SystemCapability.Ability.AbilityRuntime.Core 100 101**Parameters** 102 103| Name| Type| Mandatory| Description| 104| -------- | -------- | -------- | -------- | 105| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 106| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 107| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 108 109**Error codes** 110 111| ID| Error Message| 112| ------- | -------------------------------- | 113| 16000001 | The specified ability does not exist. | 114| 16000002 | Incorrect ability type. | 115| 16000004 | Can not start invisible component. | 116| 16000005 | The specified process does not have the permission. | 117| 16000006 | Cross-user operations are not allowed. | 118| 16000008 | The crowdtesting application expires. | 119| 16000009 | An ability cannot be started or stopped in Wukong mode. | 120| 16000010 | The call with the continuation flag is forbidden. | 121| 16000011 | The context does not exist. | 122| 16000017 | The previous ability is starting, wait start later. | 123| 16000050 | Internal error. | 124| 16000053 | The ability is not on the top of the UI. | 125| 16000055 | Installation-free timed out. | 126| 16200001 | The caller has been released. | 127 128**Example** 129 130 ```ts 131 let want = { 132 deviceId: '', 133 bundleName: 'com.extreme.test', 134 abilityName: 'MainAbility' 135 }; 136 let options = { 137 windowMode: 0 138 }; 139 140 try { 141 this.context.startAbility(want, options, (error) => { 142 if (error.code) { 143 // Process service logic errors. 144 console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) + 145 ' error.message: ' + JSON.stringify(error.message)); 146 return; 147 } 148 // Carry out normal service processing. 149 console.log('startAbility succeed'); 150 }); 151 } catch (paramError) { 152 // Process input parameter errors. 153 console.log('error.code: ' + JSON.stringify(paramError.code) + 154 ' error.message: ' + JSON.stringify(paramError.message)); 155 } 156 ``` 157 158## UIAbilityContext.startAbility 159 160startAbility(want: Want, options?: StartOptions): Promise<void>; 161 162Starts an ability. This API uses a promise to return the result. 163 164Observe the following when using this API: 165 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 166 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 167 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 168 169**System capability**: SystemCapability.Ability.AbilityRuntime.Core 170 171**Parameters** 172 173| Name| Type| Mandatory| Description| 174| -------- | -------- | -------- | -------- | 175| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 176| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 177 178**Return value** 179 180| Type| Description| 181| -------- | -------- | 182| Promise<void> | Promise used to return the result.| 183 184**Error codes** 185 186| ID| Error Message| 187| ------- | -------------------------------- | 188| 16000001 | The specified ability does not exist. | 189| 16000002 | Incorrect ability type. | 190| 16000004 | Can not start invisible component. | 191| 16000005 | The specified process does not have the permission. | 192| 16000006 | Cross-user operations are not allowed. | 193| 16000008 | The crowdtesting application expires. | 194| 16000009 | An ability cannot be started or stopped in Wukong mode. | 195| 16000010 | The call with the continuation flag is forbidden. | 196| 16000011 | The context does not exist. | 197| 16000017 | The previous ability is starting, wait start later. | 198| 16000050 | Internal error. | 199| 16000053 | The ability is not on the top of the UI. | 200| 16000055 | Installation-free timed out. | 201| 16200001 | The caller has been released. | 202 203**Example** 204 205 ```ts 206 let want = { 207 bundleName: 'com.example.myapp', 208 abilityName: 'MyAbility' 209 }; 210 let options = { 211 windowMode: 0, 212 }; 213 214 try { 215 this.context.startAbility(want, options) 216 .then((data) => { 217 // Carry out normal service processing. 218 console.log('startAbility succeed'); 219 }) 220 .catch((error) => { 221 // Process service logic errors. 222 console.log('startAbility failed, error.code: ' + JSON.stringify(error.code) + 223 ' error.message: ' + JSON.stringify(error.message)); 224 }); 225 } catch (paramError) { 226 // Process input parameter errors. 227 console.log('error.code: ' + JSON.stringify(paramError.code) + 228 ' error.message: ' + JSON.stringify(paramError.message)); 229 } 230 ``` 231 232 233## UIAbilityContext.startAbilityForResult 234 235startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void; 236 237Starts an ability. This API uses an asynchronous callback to return the result when the ability is terminated. The following situations may be possible for a started ability: 238 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. 239 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 240 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an error message, in which **resultCode** is **-1**, is returned to others. 241 242Observe the following when using this API: 243 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 244 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 245 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 246 247**System capability**: SystemCapability.Ability.AbilityRuntime.Core 248 249**Parameters** 250 251| Name| Type| Mandatory| Description| 252| -------- | -------- | -------- | -------- | 253| want |[Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 254| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result.| 255 256**Error codes** 257 258| ID| Error Message| 259| ------- | -------------------------------- | 260| 16000001 | The specified ability does not exist. | 261| 16000002 | Incorrect ability type. | 262| 16000004 | Can not start invisible component. | 263| 16000005 | The specified process does not have the permission. | 264| 16000006 | Cross-user operations are not allowed. | 265| 16000008 | The crowdtesting application expires. | 266| 16000009 | An ability cannot be started or stopped in Wukong mode. | 267| 16000010 | The call with the continuation flag is forbidden. | 268| 16000011 | The context does not exist. | 269| 16000017 | The previous ability is starting, wait start later. | 270| 16000050 | Internal error. | 271| 16000053 | The ability is not on the top of the UI. | 272| 16000055 | Installation-free timed out. | 273| 16200001 | The caller has been released. | 274 275**Example** 276 277 ```ts 278 let want = { 279 deviceId: '', 280 bundleName: 'com.extreme.test', 281 abilityName: 'MainAbility' 282 }; 283 284 try { 285 this.context.startAbilityForResult(want, (error, result) => { 286 if (error.code) { 287 // Process service logic errors. 288 console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) + 289 ' error.message: ' + JSON.stringify(error.message)); 290 return; 291 } 292 // Carry out normal service processing. 293 console.log('startAbilityForResult succeed, result.resultCode = ' + 294 result.resultCode) 295 }); 296 } catch (paramError) { 297 // Process input parameter errors. 298 console.log('error.code: ' + JSON.stringify(paramError.code) + 299 ' error.message: ' + JSON.stringify(paramError.message)); 300 } 301 ``` 302 303## UIAbilityContext.startAbilityForResult 304 305startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void; 306 307Starts an ability with the start options specified. This API uses an asynchronous callback to return the result when the ability is terminated. The following situations may be possible for a started ability: 308 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. 309 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 310 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an error message, in which **resultCode** is **-1**, is returned to others. 311 312Observe the following when using this API: 313 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 314 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 315 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 316 317**System capability**: SystemCapability.Ability.AbilityRuntime.Core 318 319**Parameters** 320 321| Name| Type| Mandatory| Description| 322| -------- | -------- | -------- | -------- | 323| want |[Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 324| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 325| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result.| 326 327**Error codes** 328 329| ID| Error Message| 330| ------- | -------------------------------- | 331| 16000001 | The specified ability does not exist. | 332| 16000002 | Incorrect ability type. | 333| 16000004 | Can not start invisible component. | 334| 16000005 | The specified process does not have the permission. | 335| 16000006 | Cross-user operations are not allowed. | 336| 16000008 | The crowdtesting application expires. | 337| 16000009 | An ability cannot be started or stopped in Wukong mode. | 338| 16000010 | The call with the continuation flag is forbidden. | 339| 16000011 | The context does not exist. | 340| 16000017 | The previous ability is starting, wait start later. | 341| 16000050 | Internal error. | 342| 16000053 | The ability is not on the top of the UI. | 343| 16000055 | Installation-free timed out. | 344| 16200001 | The caller has been released. | 345 346**Example** 347 348 ```ts 349 let want = { 350 deviceId: '', 351 bundleName: 'com.extreme.test', 352 abilityName: 'MainAbility' 353 }; 354 let options = { 355 windowMode: 0, 356 }; 357 358 try { 359 this.context.startAbilityForResult(want, options, (error, result) => { 360 if (error.code) { 361 // Process service logic errors. 362 console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) + 363 ' error.message: ' + JSON.stringify(error.message)); 364 return; 365 } 366 // Carry out normal service processing. 367 console.log('startAbilityForResult succeed, result.resultCode = ' + 368 result.resultCode) 369 }); 370 } catch (paramError) { 371 // Process input parameter errors. 372 console.log('error.code: ' + JSON.stringify(paramError.code) + 373 ' error.message: ' + JSON.stringify(paramError.message)); 374 } 375 ``` 376 377 378## UIAbilityContext.startAbilityForResult 379 380startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult>; 381 382Starts an ability. This API uses a promise to return the result when the ability is terminated. The following situations may be possible to an ability after it is started: 383 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. 384 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 385 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an error message, in which **resultCode** is **-1**, is returned to others. 386 387Observe the following when using this API: 388 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 389 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 390 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 391 392**System capability**: SystemCapability.Ability.AbilityRuntime.Core 393 394**Parameters** 395 396| Name| Type| Mandatory| Description| 397| -------- | -------- | -------- | -------- | 398| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 399| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 400 401 402**Return value** 403 404| Type| Description| 405| -------- | -------- | 406| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise used to return the result.| 407 408**Error codes** 409 410| ID| Error Message| 411| ------- | -------------------------------- | 412| 16000001 | The specified ability does not exist. | 413| 16000002 | Incorrect ability type. | 414| 16000004 | Can not start invisible component. | 415| 16000005 | The specified process does not have the permission. | 416| 16000006 | Cross-user operations are not allowed. | 417| 16000008 | The crowdtesting application expires. | 418| 16000009 | An ability cannot be started or stopped in Wukong mode. | 419| 16000010 | The call with the continuation flag is forbidden. | 420| 16000011 | The context does not exist. | 421| 16000017 | The previous ability is starting, wait start later. | 422| 16000050 | Internal error. | 423| 16000053 | The ability is not on the top of the UI. | 424| 16000055 | Installation-free timed out. | 425| 16200001 | The caller has been released. | 426 427**Example** 428 429 ```ts 430 let want = { 431 bundleName: 'com.example.myapp', 432 abilityName: 'MyAbility' 433 }; 434 let options = { 435 windowMode: 0, 436 }; 437 438 try { 439 this.context.startAbilityForResult(want, options) 440 .then((result) => { 441 // Carry out normal service processing. 442 console.log('startAbilityForResult succeed, result.resultCode = ' + result.resultCode); 443 }) 444 .catch((error) => { 445 // Process service logic errors. 446 console.log('startAbilityForResult failed, error.code: ' + JSON.stringify(error.code) + 447 ' error.message: ' + JSON.stringify(error.message)); 448 }); 449 } catch (paramError) { 450 // Process input parameter errors. 451 console.log('error.code: ' + JSON.stringify(paramError.code) + 452 ' error.message: ' + JSON.stringify(paramError.message)); 453 } 454 ``` 455 456## UIAbilityContext.startAbilityForResultWithAccount 457 458startAbilityForResultWithAccount(want: Want, accountId: number, callback: AsyncCallback\<AbilityResult>): void; 459 460Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result when the ability is terminated. 461 462**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 463 464**System capability**: SystemCapability.Ability.AbilityRuntime.Core 465 466**System API**: This is a system API and cannot be called by third-party applications. 467 468**Parameters** 469 470| Name| Type| Mandatory| Description| 471| -------- | -------- | -------- | -------- | 472| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 473| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 474| callback | AsyncCallback\<AbilityResult\> | Yes| Callback used to return the result.| 475 476**Error codes** 477 478| ID| Error Message| 479| ------- | -------------------------------- | 480| 16000001 | The specified ability does not exist. | 481| 16000002 | Incorrect ability type. | 482| 16000004 | Can not start invisible component. | 483| 16000005 | The specified process does not have the permission. | 484| 16000006 | Cross-user operations are not allowed. | 485| 16000008 | The crowdtesting application expires. | 486| 16000009 | An ability cannot be started or stopped in Wukong mode. | 487| 16000010 | The call with the continuation flag is forbidden. | 488| 16000011 | The context does not exist. | 489| 16000017 | The previous ability is starting, wait start later. | 490| 16000050 | Internal error. | 491| 16000053 | The ability is not on the top of the UI. | 492| 16000055 | Installation-free timed out. | 493| 16200001 | The caller has been released. | 494 495**Example** 496 497 ```ts 498 let want = { 499 deviceId: '', 500 bundleName: 'com.extreme.test', 501 abilityName: 'MainAbility' 502 }; 503 let accountId = 100; 504 505 try { 506 this.context.startAbilityForResultWithAccount(want, accountId, (error, result) => { 507 if (error.code) { 508 // Process service logic errors. 509 console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) + 510 ' error.message: ' + JSON.stringify(error.message)); 511 return; 512 } 513 // Carry out normal service processing. 514 console.log('startAbilityForResultWithAccount succeed, result.resultCode = ' + 515 result.resultCode) 516 }); 517 } catch (paramError) { 518 // Process input parameter errors. 519 console.log('error.code: ' + JSON.stringify(paramError.code) + 520 ' error.message: ' + JSON.stringify(paramError.message)); 521 } 522 ``` 523 524 525## UIAbilityContext.startAbilityForResultWithAccount 526 527startAbilityForResultWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void; 528 529Starts an ability with the start options and account ID specified. This API uses an asynchronous callback to return the result when the ability is terminated. 530 531**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 532 533**System capability**: SystemCapability.Ability.AbilityRuntime.Core 534 535**System API**: This is a system API and cannot be called by third-party applications. 536 537**Parameters** 538 539| Name| Type| Mandatory| Description| 540| -------- | -------- | -------- | -------- | 541| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 542| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 543| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 544| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 545 546**Error codes** 547 548| ID| Error Message| 549| ------- | -------------------------------- | 550| 16000001 | The specified ability does not exist. | 551| 16000002 | Incorrect ability type. | 552| 16000004 | Can not start invisible component. | 553| 16000005 | The specified process does not have the permission. | 554| 16000006 | Cross-user operations are not allowed. | 555| 16000008 | The crowdtesting application expires. | 556| 16000009 | An ability cannot be started or stopped in Wukong mode. | 557| 16000010 | The call with the continuation flag is forbidden. | 558| 16000011 | The context does not exist. | 559| 16000017 | The previous ability is starting, wait start later. | 560| 16000050 | Internal error. | 561| 16000053 | The ability is not on the top of the UI. | 562| 16000055 | Installation-free timed out. | 563| 16200001 | The caller has been released. | 564 565**Example** 566 567 ```ts 568 let want = { 569 deviceId: '', 570 bundleName: 'com.extreme.test', 571 abilityName: 'MainAbility' 572 }; 573 let accountId = 100; 574 let options = { 575 windowMode: 0 576 }; 577 578 try { 579 this.context.startAbilityForResultWithAccount(want, accountId, options, (error, result) => { 580 if (error.code) { 581 // Process service logic errors. 582 console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) + 583 ' error.message: ' + JSON.stringify(error.message)); 584 return; 585 } 586 // Carry out normal service processing. 587 console.log('startAbilityForResultWithAccount succeed, result.resultCode = ' + 588 result.resultCode) 589 }); 590 } catch (paramError) { 591 // Process input parameter errors. 592 console.log('error.code: ' + JSON.stringify(paramError.code) + 593 ' error.message: ' + JSON.stringify(paramError.message)); 594 } 595 ``` 596 597 598## UIAbilityContext.startAbilityForResultWithAccount 599 600startAbilityForResultWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<AbilityResult\>; 601 602Starts an ability with the account ID specified. This API uses a promise to return the result when the ability is terminated. 603 604**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 605 606**System capability**: SystemCapability.Ability.AbilityRuntime.Core 607 608**System API**: This is a system API and cannot be called by third-party applications. 609 610**Parameters** 611 612| Name| Type| Mandatory| Description| 613| -------- | -------- | -------- | -------- | 614| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 615| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 616| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 617 618**Return value** 619 620| Type| Description| 621| -------- | -------- | 622| Promise<AbilityResult> | Promise used to return the result.| 623 624**Error codes** 625 626| ID| Error Message| 627| ------- | -------------------------------- | 628| 16000001 | The specified ability does not exist. | 629| 16000002 | Incorrect ability type. | 630| 16000004 | Can not start invisible component. | 631| 16000005 | The specified process does not have the permission. | 632| 16000006 | Cross-user operations are not allowed. | 633| 16000008 | The crowdtesting application expires. | 634| 16000009 | An ability cannot be started or stopped in Wukong mode. | 635| 16000010 | The call with the continuation flag is forbidden. | 636| 16000011 | The context does not exist. | 637| 16000017 | The previous ability is starting, wait start later. | 638| 16000050 | Internal error. | 639| 16000053 | The ability is not on the top of the UI. | 640| 16000055 | Installation-free timed out. | 641| 16200001 | The caller has been released. | 642 643**Example** 644 645 ```ts 646 let want = { 647 deviceId: '', 648 bundleName: 'com.extreme.test', 649 abilityName: 'MainAbility' 650 }; 651 let accountId = 100; 652 let options = { 653 windowMode: 0 654 }; 655 656 try { 657 this.context.startAbilityForResultWithAccount(want, accountId, options) 658 .then((result) => { 659 // Carry out normal service processing. 660 console.log('startAbilityForResultWithAccount succeed, result.resultCode = ' + 661 result.resultCode) 662 }) 663 .catch((error) => { 664 // Process service logic errors. 665 console.log('startAbilityForResultWithAccount failed, error.code: ' + JSON.stringify(error.code) + 666 ' error.message: ' + JSON.stringify(error.message)); 667 }); 668 } catch (paramError) { 669 // Process input parameter errors. 670 console.log('error.code: ' + JSON.stringify(paramError.code) + 671 ' error.message: ' + JSON.stringify(paramError.message)); 672 } 673 ``` 674## UIAbilityContext.startServiceExtensionAbility 675 676startServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void; 677 678Starts a ServiceExtensionAbility. This API uses an asynchronous callback to return the result. 679 680**System capability**: SystemCapability.Ability.AbilityRuntime.Core 681 682**System API**: This is a system API and cannot be called by third-party applications. 683 684**Parameters** 685 686| Name| Type| Mandatory| Description| 687| -------- | -------- | -------- | -------- | 688| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 689| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 690 691**Error codes** 692 693| ID| Error Message| 694| ------- | -------------------------------- | 695| 16000001 | The specified ability does not exist. | 696| 16000002 | Incorrect ability type. | 697| 16000005 | The specified process does not have the permission. | 698| 16000006 | Cross-user operations are not allowed. | 699| 16000008 | The crowdtesting application expires. | 700| 16000011 | The context does not exist. | 701| 16000050 | Internal error. | 702| 16200001 | The caller has been released. | 703 704**Example** 705 706 ```ts 707 let want = { 708 deviceId: '', 709 bundleName: 'com.extreme.test', 710 abilityName: 'MainAbility' 711 }; 712 713 try { 714 this.context.startServiceExtensionAbility(want, (error) => { 715 if (error.code) { 716 // Process service logic errors. 717 console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 718 ' error.message: ' + JSON.stringify(error.message)); 719 return; 720 } 721 // Carry out normal service processing. 722 console.log('startServiceExtensionAbility succeed'); 723 }); 724 } catch (paramError) { 725 // Process input parameter errors. 726 console.log('error.code: ' + JSON.stringify(paramError.code) + 727 ' error.message: ' + JSON.stringify(paramError.message)); 728 } 729 ``` 730 731## UIAbilityContext.startServiceExtensionAbility 732 733startServiceExtensionAbility(want: Want): Promise\<void>; 734 735Starts a ServiceExtensionAbility. This API uses a promise to return the result. 736 737**System capability**: SystemCapability.Ability.AbilityRuntime.Core 738 739**System API**: This is a system API and cannot be called by third-party applications. 740 741**Parameters** 742 743| Name| Type| Mandatory| Description| 744| -------- | -------- | -------- | -------- | 745| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 746 747**Error codes** 748 749| ID| Error Message| 750| ------- | -------------------------------- | 751| 16000001 | The specified ability does not exist. | 752| 16000002 | Incorrect ability type. | 753| 16000005 | The specified process does not have the permission. | 754| 16000006 | Cross-user operations are not allowed. | 755| 16000008 | The crowdtesting application expires. | 756| 16000011 | The context does not exist. | 757| 16000050 | Internal error. | 758| 16200001 | The caller has been released. | 759 760**Example** 761 762 ```ts 763 let want = { 764 deviceId: '', 765 bundleName: 'com.extreme.test', 766 abilityName: 'MainAbility' 767 }; 768 769 try { 770 this.context.startServiceExtensionAbility(want) 771 .then((data) => { 772 // Carry out normal service processing. 773 console.log('startServiceExtensionAbility succeed'); 774 }) 775 .catch((error) => { 776 // Process service logic errors. 777 console.log('startServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 778 ' error.message: ' + JSON.stringify(error.message)); 779 }); 780 } catch (paramError) { 781 // Process input parameter errors. 782 console.log('error.code: ' + JSON.stringify(paramError.code) + 783 ' error.message: ' + JSON.stringify(paramError.message)); 784 } 785 ``` 786 787## UIAbilityContext.startServiceExtensionAbilityWithAccount 788 789startServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 790 791Starts a ServiceExtensionAbility with the account ID specified. This API uses an asynchronous callback to return the result. 792 793**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 794 795**System capability**: SystemCapability.Ability.AbilityRuntime.Core 796 797**System API**: This is a system API and cannot be called by third-party applications. 798 799**Parameters** 800 801| Name| Type| Mandatory| Description| 802| -------- | -------- | -------- | -------- | 803| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 804| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 805| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 806 807**Error codes** 808 809| ID| Error Message| 810| ------- | -------------------------------- | 811| 16000001 | The specified ability does not exist. | 812| 16000002 | Incorrect ability type. | 813| 16000005 | The specified process does not have the permission. | 814| 16000006 | Cross-user operations are not allowed. | 815| 16000008 | The crowdtesting application expires. | 816| 16000011 | The context does not exist. | 817| 16000050 | Internal error. | 818| 16200001 | The caller has been released. | 819 820**Example** 821 822 ```ts 823 let want = { 824 deviceId: '', 825 bundleName: 'com.extreme.test', 826 abilityName: 'MainAbility' 827 }; 828 let accountId = 100; 829 830 try { 831 this.context.startServiceExtensionAbilityWithAccount(want, accountId, (error) => { 832 if (error.code) { 833 // Process service logic errors. 834 console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 835 ' error.message: ' + JSON.stringify(error.message)); 836 return; 837 } 838 // Carry out normal service processing. 839 console.log('startServiceExtensionAbilityWithAccount succeed'); 840 }); 841 } catch (paramError) { 842 // Process input parameter errors. 843 console.log('error.code: ' + JSON.stringify(paramError.code) + 844 ' error.message: ' + JSON.stringify(paramError.message)); 845 } 846 ``` 847 848## UIAbilityContext.startServiceExtensionAbilityWithAccount 849 850startServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>; 851 852Starts a ServiceExtensionAbility with the account ID specified. This API uses a promise to return the result. 853 854**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 855 856**System capability**: SystemCapability.Ability.AbilityRuntime.Core 857 858**System API**: This is a system API and cannot be called by third-party applications. 859 860**Parameters** 861 862| Name| Type| Mandatory| Description| 863| -------- | -------- | -------- | -------- | 864| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 865| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 866 867**Error codes** 868 869| ID| Error Message| 870| ------- | -------------------------------- | 871| 16000001 | The specified ability does not exist. | 872| 16000002 | Incorrect ability type. | 873| 16000005 | The specified process does not have the permission. | 874| 16000006 | Cross-user operations are not allowed. | 875| 16000008 | The crowdtesting application expires. | 876| 16000011 | The context does not exist. | 877| 16000050 | Internal error. | 878| 16200001 | The caller has been released. | 879 880**Example** 881 882 ```ts 883 let want = { 884 deviceId: '', 885 bundleName: 'com.extreme.test', 886 abilityName: 'MainAbility' 887 }; 888 let accountId = 100; 889 890 try { 891 this.context.startServiceExtensionAbilityWithAccount(want, accountId) 892 .then((data) => { 893 // Carry out normal service processing. 894 console.log('startServiceExtensionAbilityWithAccount succeed'); 895 }) 896 .catch((error) => { 897 // Process service logic errors. 898 console.log('startServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 899 ' error.message: ' + JSON.stringify(error.message)); 900 }); 901 } catch (paramError) { 902 // Process input parameter errors. 903 console.log('error.code: ' + JSON.stringify(paramError.code) + 904 ' error.message: ' + JSON.stringify(paramError.message)); 905 } 906 ``` 907## UIAbilityContext.stopServiceExtensionAbility 908 909stopServiceExtensionAbility(want: Want, callback: AsyncCallback\<void>): void; 910 911Stops a ServiceExtensionAbility in the same application. This API uses an asynchronous callback to return the result. 912 913**System capability**: SystemCapability.Ability.AbilityRuntime.Core 914 915**System API**: This is a system API and cannot be called by third-party applications. 916 917**Parameters** 918 919| Name| Type| Mandatory| Description| 920| -------- | -------- | -------- | -------- | 921| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 922| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 923 924**Error codes** 925 926| ID| Error Message| 927| ------- | -------------------------------- | 928| 16000001 | The specified ability does not exist. | 929| 16000002 | Incorrect ability type. | 930| 16000005 | The specified process does not have the permission. | 931| 16000006 | Cross-user operations are not allowed. | 932| 16000011 | The context does not exist. | 933| 16000050 | Internal error. | 934| 16200001 | The caller has been released. | 935 936**Example** 937 938 ```ts 939 let want = { 940 deviceId: '', 941 bundleName: 'com.extreme.test', 942 abilityName: 'MainAbility' 943 }; 944 945 try { 946 this.context.stopServiceExtensionAbility(want, (error) => { 947 if (error.code) { 948 // Process service logic errors. 949 console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 950 ' error.message: ' + JSON.stringify(error.message)); 951 return; 952 } 953 // Carry out normal service processing. 954 console.log('stopServiceExtensionAbility succeed'); 955 }); 956 } catch (paramError) { 957 // Process input parameter errors. 958 console.log('error.code: ' + JSON.stringify(paramError.code) + 959 ' error.message: ' + JSON.stringify(paramError.message)); 960 } 961 ``` 962 963## UIAbilityContext.stopServiceExtensionAbility 964 965stopServiceExtensionAbility(want: Want): Promise\<void>; 966 967Stops a ServiceExtensionAbility in the same application. This API uses a promise to return the result. 968 969**System capability**: SystemCapability.Ability.AbilityRuntime.Core 970 971**System API**: This is a system API and cannot be called by third-party applications. 972 973**Parameters** 974 975| Name| Type| Mandatory| Description| 976| -------- | -------- | -------- | -------- | 977| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 978 979**Error codes** 980 981| ID| Error Message| 982| ------- | -------------------------------- | 983| 16000001 | The specified ability does not exist. | 984| 16000002 | Incorrect ability type. | 985| 16000005 | The specified process does not have the permission. | 986| 16000006 | Cross-user operations are not allowed. | 987| 16000011 | The context does not exist. | 988| 16000050 | Internal error. | 989| 16200001 | The caller has been released. | 990 991**Example** 992 993 ```ts 994 let want = { 995 deviceId: '', 996 bundleName: 'com.extreme.test', 997 abilityName: 'MainAbility' 998 }; 999 1000 try { 1001 this.context.stopServiceExtensionAbility(want) 1002 .then((data) => { 1003 // Carry out normal service processing. 1004 console.log('stopServiceExtensionAbility succeed'); 1005 }) 1006 .catch((error) => { 1007 // Process service logic errors. 1008 console.log('stopServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 1009 ' error.message: ' + JSON.stringify(error.message)); 1010 }); 1011 } catch (paramError) { 1012 // Process input parameter errors. 1013 console.log('error.code: ' + JSON.stringify(paramError.code) + 1014 ' error.message: ' + JSON.stringify(paramError.message)); 1015 } 1016 ``` 1017 1018## UIAbilityContext.stopServiceExtensionAbilityWithAccount 1019 1020stopServiceExtensionAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void>): void; 1021 1022Stops a ServiceExtensionAbility with the account ID specified in the same application. This API uses an asynchronous callback to return the result. 1023 1024**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1025 1026**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1027 1028**System API**: This is a system API and cannot be called by third-party applications. 1029 1030**Parameters** 1031 1032| Name| Type| Mandatory| Description| 1033| -------- | -------- | -------- | -------- | 1034| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 1035| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 1036| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 1037 1038**Error codes** 1039 1040| ID| Error Message| 1041| ------- | -------------------------------- | 1042| 16000001 | The specified ability does not exist. | 1043| 16000002 | Incorrect ability type. | 1044| 16000005 | The specified process does not have the permission. | 1045| 16000006 | Cross-user operations are not allowed. | 1046| 16000011 | The context does not exist. | 1047| 16000050 | Internal error. | 1048| 16200001 | The caller has been released. | 1049 1050**Example** 1051 1052 ```ts 1053 let want = { 1054 deviceId: '', 1055 bundleName: 'com.extreme.test', 1056 abilityName: 'MainAbility' 1057 }; 1058 let accountId = 100; 1059 1060 try { 1061 this.context.stopServiceExtensionAbilityWithAccount(want, accountId, (error) => { 1062 if (error.code) { 1063 // Process service logic errors. 1064 console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 1065 ' error.message: ' + JSON.stringify(error.message)); 1066 return; 1067 } 1068 // Carry out normal service processing. 1069 console.log('stopServiceExtensionAbilityWithAccount succeed'); 1070 }); 1071 } catch (paramError) { 1072 // Process input parameter errors. 1073 console.log('error.code: ' + JSON.stringify(paramError.code) + 1074 ' error.message: ' + JSON.stringify(paramError.message)); 1075 } 1076 ``` 1077 1078## UIAbilityContext.stopServiceExtensionAbilityWithAccount 1079 1080stopServiceExtensionAbilityWithAccount(want: Want, accountId: number): Promise\<void>; 1081 1082Stops a ServiceExtensionAbility with the account ID specified in the same application. This API uses a promise to return the result. 1083 1084**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1085 1086**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1087 1088**System API**: This is a system API and cannot be called by third-party applications. 1089 1090**Parameters** 1091 1092| Name| Type| Mandatory| Description| 1093| -------- | -------- | -------- | -------- | 1094| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 1095| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 1096 1097**Error codes** 1098 1099| ID| Error Message| 1100| ------- | -------------------------------- | 1101| 16000001 | The specified ability does not exist. | 1102| 16000002 | Incorrect ability type. | 1103| 16000005 | The specified process does not have the permission. | 1104| 16000006 | Cross-user operations are not allowed. | 1105| 16000011 | The context does not exist. | 1106| 16000050 | Internal error. | 1107| 16200001 | The caller has been released. | 1108 1109**Example** 1110 1111 ```ts 1112 let want = { 1113 deviceId: '', 1114 bundleName: 'com.extreme.test', 1115 abilityName: 'MainAbility' 1116 }; 1117 let accountId = 100; 1118 1119 try { 1120 this.context.stopServiceExtensionAbilityWithAccount(want, accountId) 1121 .then((data) => { 1122 // Carry out normal service processing. 1123 console.log('stopServiceExtensionAbilityWithAccount succeed'); 1124 }) 1125 .catch((error) => { 1126 // Process service logic errors. 1127 console.log('stopServiceExtensionAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 1128 ' error.message: ' + JSON.stringify(error.message)); 1129 }); 1130 } catch (paramError) { 1131 // Process input parameter errors. 1132 console.log('error.code: ' + JSON.stringify(paramError.code) + 1133 ' error.message: ' + JSON.stringify(paramError.message)); 1134 } 1135 ``` 1136 1137## UIAbilityContext.terminateSelf 1138 1139terminateSelf(callback: AsyncCallback<void>): void; 1140 1141Terminates this ability. This API uses an asynchronous callback to return the result. 1142 1143**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1144 1145**Parameters** 1146 1147| Name| Type| Mandatory| Description| 1148| -------- | -------- | -------- | -------- | 1149| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1150 1151**Error codes** 1152 1153| ID| Error Message| 1154| ------- | -------------------------------- | 1155| 16000001 | The specified ability does not exist. | 1156| 16000004 | Can not start invisible component. | 1157| 16000005 | The specified process does not have the permission. | 1158| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1159| 16000011 | The context does not exist. | 1160| 16000050 | Internal error. | 1161 1162**Example** 1163 1164 ```ts 1165 this.context.terminateSelf((error) => { 1166 if (error.code) { 1167 // Process service logic errors. 1168 console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) + 1169 ' error.message: ' + JSON.stringify(error.message)); 1170 return; 1171 } 1172 // Carry out normal service processing. 1173 console.log('terminateSelf succeed'); 1174 }); 1175 ``` 1176 1177 1178## UIAbilityContext.terminateSelf 1179 1180terminateSelf(): Promise<void>; 1181 1182Terminates this ability. This API uses a promise to return the result. 1183 1184**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1185 1186**Return value** 1187 1188| Type| Description| 1189| -------- | -------- | 1190| Promise<void> | Promise used to return the result.| 1191 1192**Error codes** 1193 1194| ID| Error Message| 1195| ------- | -------------------------------- | 1196| 16000001 | The specified ability does not exist. | 1197| 16000004 | Can not start invisible component. | 1198| 16000005 | The specified process does not have the permission. | 1199| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1200| 16000011 | The context does not exist. | 1201| 16000050 | Internal error. | 1202 1203**Example** 1204 1205 ```ts 1206 this.context.terminateSelf().then((data) => { 1207 // Carry out normal service processing. 1208 console.log('terminateSelf succeed'); 1209 }).catch((error) => { 1210 // Process service logic errors. 1211 console.log('terminateSelf failed, error.code: ' + JSON.stringify(error.code) + 1212 ' error.message: ' + JSON.stringify(error.message)); 1213 }); 1214 ``` 1215 1216 1217## UIAbilityContext.terminateSelfWithResult 1218 1219terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback<void>): void; 1220 1221Terminates this ability. If the ability is started by calling [startAbilityForResult](#uiabilitycontextstartabilityforresult), the result is returned to the caller in the form of an asynchronous callback when **terminateSelfWithResult** is called. Otherwise, no result is returned to the caller when **terminateSelfWithResult** is called. 1222 1223**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1224 1225**Parameters** 1226 1227| Name| Type| Mandatory| Description| 1228| -------- | -------- | -------- | -------- | 1229| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the caller.| 1230| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1231 1232**Error codes** 1233 1234| ID| Error Message| 1235| ------- | -------------------------------- | 1236| 16000001 | The specified ability does not exist. | 1237| 16000004 | Can not start invisible component. | 1238| 16000005 | The specified process does not have the permission. | 1239| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1240| 16000011 | The context does not exist. | 1241| 16000050 | Internal error. | 1242 1243**Example** 1244 1245 ```ts 1246 let want = { 1247 bundleName: 'com.extreme.myapplication', 1248 abilityName: 'SecondAbility' 1249 } 1250 let resultCode = 100; 1251 // AbilityResult information returned to the caller. 1252 let abilityResult = { 1253 want, 1254 resultCode 1255 } 1256 1257 try { 1258 this.context.terminateSelfWithResult(abilityResult, (error) => { 1259 if (error.code) { 1260 // Process service logic errors. 1261 console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(error.code) + 1262 ' error.message: ' + JSON.stringify(error.message)); 1263 return; 1264 } 1265 // Carry out normal service processing. 1266 console.log('terminateSelfWithResult succeed'); 1267 }); 1268 } catch (paramError) { 1269 // Process input parameter errors. 1270 console.log('error.code: ' + JSON.stringify(paramError.code) + 1271 ' error.message: ' + JSON.stringify(paramError.message)); 1272 } 1273 ``` 1274 1275 1276## UIAbilityContext.terminateSelfWithResult 1277 1278terminateSelfWithResult(parameter: AbilityResult): Promise<void>; 1279 1280Terminates this ability. If the ability is started by calling [startAbilityForResult](#uiabilitycontextstartabilityforresult), the result is returned to the caller in the form of a promise when **terminateSelfWithResult** is called. Otherwise, no result is returned to the caller when **terminateSelfWithResult** is called. 1281 1282**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1283 1284**Parameters** 1285 1286| Name| Type| Mandatory| Description| 1287| -------- | -------- | -------- | -------- | 1288| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the caller.| 1289 1290**Return value** 1291 1292| Type| Description| 1293| -------- | -------- | 1294| Promise<void> | Promise used to return the result.| 1295 1296**Error codes** 1297 1298| ID| Error Message| 1299| ------- | -------------------------------- | 1300| 16000001 | The specified ability does not exist. | 1301| 16000004 | Can not start invisible component. | 1302| 16000005 | The specified process does not have the permission. | 1303| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1304| 16000011 | The context does not exist. | 1305| 16000050 | Internal error. | 1306 1307**Example** 1308 1309 ```ts 1310 let want = { 1311 bundleName: 'com.extreme.myapplication', 1312 abilityName: 'SecondAbility' 1313 } 1314 let resultCode = 100; 1315 // AbilityResult information returned to the caller. 1316 let abilityResult = { 1317 want, 1318 resultCode 1319 } 1320 1321 try { 1322 this.context.terminateSelfWithResult(abilityResult) 1323 .then((data) => { 1324 // Carry out normal service processing. 1325 console.log('terminateSelfWithResult succeed'); 1326 }) 1327 .catch((error) => { 1328 // Process service logic errors. 1329 console.log('terminateSelfWithResult failed, error.code: ' + JSON.stringify(error.code) + 1330 ' error.message: ' + JSON.stringify(error.message)); 1331 }); 1332 } catch (paramError) { 1333 // Process input parameter errors. 1334 console.log('error.code: ' + JSON.stringify(paramError.code) + 1335 ' error.message: ' + JSON.stringify(paramError.message)); 1336 } 1337 ``` 1338 1339## UIAbilityContext.connectServiceExtensionAbility 1340 1341connectServiceExtensionAbility(want: Want, options: ConnectOptions): number; 1342 1343Connects this ability to an ability that uses the **AbilityInfo.AbilityType.SERVICE** template. 1344 1345**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1346 1347**Parameters** 1348 1349| Name| Type| Mandatory| Description| 1350| -------- | -------- | -------- | -------- | 1351| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 1352| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | No| Parameters for the connection.| 1353 1354**Return value** 1355 1356| Type| Description| 1357| -------- | -------- | 1358| number | Result code of the ability connection.| 1359 1360**Error codes** 1361 1362| ID| Error Message| 1363| ------- | -------------------------------- | 1364| 16000001 | The specified ability does not exist. | 1365| 16000005 | The specified process does not have the permission. | 1366| 16000011 | The context does not exist. | 1367| 16000050 | Internal error. | 1368 1369**Example** 1370 1371 ```ts 1372 let want = { 1373 deviceId: '', 1374 bundleName: 'com.extreme.test', 1375 abilityName: 'MainAbility' 1376 }; 1377 let options = { 1378 onConnect(elementName, remote) { console.log('----------- onConnect -----------') }, 1379 onDisconnect(elementName) { console.log('----------- onDisconnect -----------') }, 1380 onFailed(code) { console.log('----------- onFailed -----------') } 1381 } 1382 1383 let connection = null; 1384 try { 1385 connection = this.context.connectServiceExtensionAbility(want, options); 1386 } catch (paramError) { 1387 // Process input parameter errors. 1388 console.log('error.code: ' + JSON.stringify(paramError.code) + 1389 ' error.message: ' + JSON.stringify(paramError.message)); 1390 } 1391 ``` 1392 1393 1394## UIAbilityContext.connectServiceExtensionAbilityWithAccount 1395 1396connectServiceExtensionAbilityWithAccount(want: Want, accountId: number, options: ConnectOptions): number; 1397 1398Connects this ability to an ability that uses the **AbilityInfo.AbilityType.SERVICE** template, with the account ID specified. 1399 1400**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1401 1402**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1403 1404**System API**: This is a system API and cannot be called by third-party applications. 1405 1406**Parameters** 1407 1408| Name| Type| Mandatory| Description| 1409| -------- | -------- | -------- | -------- | 1410| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 1411| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 1412| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | No| Parameters for the connection.| 1413 1414**Return value** 1415 1416| Type| Description| 1417| -------- | -------- | 1418| number | Result code of the ability connection.| 1419 1420**Error codes** 1421 1422| ID| Error Message| 1423| ------- | -------------------------------- | 1424| 16000001 | The specified ability does not exist. | 1425| 16000005 | The specified process does not have the permission. | 1426| 16000011 | The context does not exist. | 1427| 16000050 | Internal error. | 1428 1429**Example** 1430 1431 ```ts 1432 let want = { 1433 deviceId: '', 1434 bundleName: 'com.extreme.test', 1435 abilityName: 'MainAbility' 1436 }; 1437 let accountId = 100; 1438 let options = { 1439 onConnect(elementName, remote) { console.log('----------- onConnect -----------') }, 1440 onDisconnect(elementName) { console.log('----------- onDisconnect -----------') }, 1441 onFailed(code) { console.log('----------- onFailed -----------') } 1442 } 1443 1444 let connection = null; 1445 try { 1446 connection = this.context.connectServiceExtensionAbilityWithAccount(want, accountId, options); 1447 } catch (paramError) { 1448 // Process input parameter errors. 1449 console.log('error.code: ' + JSON.stringify(paramError.code) + 1450 ' error.message: ' + JSON.stringify(paramError.message)); 1451 } 1452 ``` 1453 1454## UIAbilityContext.disconnectServiceExtensionAbility 1455 1456disconnectServiceExtensionAbility(connection: number): Promise\<void>; 1457 1458Disconnects a connection. This API uses a promise to return the result. 1459 1460**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1461 1462**Parameters** 1463 1464| Name| Type| Mandatory| Description| 1465| -------- | -------- | -------- | -------- | 1466| connection | number | Yes| Result code of the ability connection.| 1467 1468**Return value** 1469 1470| Type| Description| 1471| -------- | -------- | 1472| Promise\<void> | Promise used to return the result.| 1473 1474**Error codes** 1475 1476| ID| Error Message| 1477| ------- | -------------------------------- | 1478| 16000001 | The specified ability does not exist. | 1479| 16000005 | The specified process does not have the permission. | 1480| 16000011 | The context does not exist. | 1481| 16000050 | Internal error. | 1482 1483**Example** 1484 1485 ```ts 1486 // connection is the return value of connectServiceExtensionAbility. 1487 let connection = 1; 1488 1489 try { 1490 this.context.disconnectServiceExtensionAbility(connection) 1491 .then((data) => { 1492 // Carry out normal service processing. 1493 console.log('disconnectServiceExtensionAbility succeed'); 1494 }) 1495 .catch((error) => { 1496 // Process service logic errors. 1497 console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 1498 ' error.message: ' + JSON.stringify(error.message)); 1499 }); 1500 } catch (paramError) { 1501 // Process input parameter errors. 1502 console.log('error.code: ' + JSON.stringify(paramError.code) + 1503 ' error.message: ' + JSON.stringify(paramError.message)); 1504 } 1505 ``` 1506 1507## UIAbilityContext.disconnectServiceExtensionAbility 1508 1509disconnectServiceExtensionAbility(connection: number, callback:AsyncCallback\<void>): void; 1510 1511Disconnects a connection. This API uses an asynchronous callback to return the result. 1512 1513**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1514 1515**Parameters** 1516 1517| Name| Type| Mandatory| Description| 1518| -------- | -------- | -------- | -------- | 1519| connection | number | Yes| Result code of the ability connection.| 1520| callback | AsyncCallback\<void> | Yes| Callback used to return the result.| 1521 1522**Error codes** 1523 1524| ID| Error Message| 1525| ------- | -------------------------------- | 1526| 16000001 | The specified ability does not exist. | 1527| 16000005 | The specified process does not have the permission. | 1528| 16000011 | The context does not exist. | 1529| 16000050 | Internal error. | 1530 1531**Example** 1532 1533 ```ts 1534 // connection is the return value of connectServiceExtensionAbility. 1535 let connection = 1; 1536 1537 try { 1538 this.context.disconnectServiceExtensionAbility(connection, (error) => { 1539 if (error.code) { 1540 // Process service logic errors. 1541 console.log('disconnectServiceExtensionAbility failed, error.code: ' + JSON.stringify(error.code) + 1542 ' error.message: ' + JSON.stringify(error.message)); 1543 return; 1544 } 1545 // Carry out normal service processing. 1546 console.log('disconnectServiceExtensionAbility succeed'); 1547 }); 1548 } catch (paramError) { 1549 // Process input parameter errors. 1550 console.log('error.code: ' + JSON.stringify(paramError.code) + 1551 ' error.message: ' + JSON.stringify(paramError.message)); 1552 } 1553 ``` 1554 1555## UIAbilityContext.startAbilityByCall 1556 1557startAbilityByCall(want: Want): Promise<Caller>; 1558 1559Starts an ability in the foreground or background and obtains the caller object for communicating with the ability. 1560 1561Observe the following when using this API: 1562 - If an application running in the background needs to call this API to start an ability, it must have the **ohos.permission.START_ABILITIES_FROM_BACKGROUND** permission. 1563 - If **visible** of the target ability is **false**, the caller must have the **ohos.permission.START_INVISIBLE_ABILITY** permission. 1564 - For details about the startup rules for the components in the stage model, see [Component Startup Rules (Stage Model)](../../application-models/component-startup-rules.md). 1565 1566**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1567 1568**System API**: This is a system API and cannot be called by third-party applications. 1569 1570**Parameters** 1571 1572| Name| Type| Mandatory| Description| 1573| -------- | -------- | -------- | -------- | 1574| want | [Want](js-apis-application-want.md) | Yes| Information about the ability to start, including **abilityName**, **moduleName**, **bundleName**, **deviceId** (optional), and **parameters** (optional). If **deviceId** is left blank or null, the local ability is started. If **parameters** is left blank or null, the ability is started in the background.| 1575 1576**Return value** 1577 1578| Type| Description| 1579| -------- | -------- | 1580| Promise<Caller> | Promise used to return the caller object to communicate with.| 1581 1582**Error codes** 1583 1584| ID| Error Message| 1585| ------- | -------------------------------- | 1586| 16000001 | The specified ability does not exist. | 1587| 16000002 | Incorrect ability type. | 1588| 16000004 | Can not start invisible component. | 1589| 16000005 | The specified process does not have the permission. | 1590| 16000006 | Cross-user operations are not allowed. | 1591| 16000008 | The crowdtesting application expires. | 1592| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1593| 16000010 | The call with the continuation flag is forbidden. | 1594| 16000011 | The context does not exist. | 1595| 16000017 | The previous ability is starting, wait start later. | 1596| 16000050 | Internal error. | 1597| 16000053 | The ability is not on the top of the UI. | 1598| 16000055 | Installation-free timed out. | 1599| 16200001 | The caller has been released. | 1600 1601**Example** 1602 1603 Start an ability in the background. 1604 1605 ```ts 1606 let caller = undefined; 1607 1608 // Start an ability in the background by not passing parameters. 1609 let wantBackground = { 1610 bundleName: 'com.example.myservice', 1611 moduleName: 'entry', 1612 abilityName: 'MainAbility', 1613 deviceId: '' 1614 }; 1615 1616 try { 1617 this.context.startAbilityByCall(wantBackground) 1618 .then((obj) => { 1619 // Carry out normal service processing. 1620 caller = obj; 1621 console.log('startAbilityByCall succeed'); 1622 }).catch((error) => { 1623 // Process service logic errors. 1624 console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) + 1625 ' error.message: ' + JSON.stringify(error.message)); 1626 }); 1627 } catch (paramError) { 1628 // Process input parameter errors. 1629 console.log('error.code: ' + JSON.stringify(paramError.code) + 1630 ' error.message: ' + JSON.stringify(paramError.message)); 1631 } 1632 ``` 1633 1634 Start an ability in the foreground. 1635 1636 ```ts 1637 let caller = undefined; 1638 1639 // Start an ability in the foreground with 'ohos.aafwk.param.callAbilityToForeground' in parameters set to true. 1640 let wantForeground = { 1641 bundleName: 'com.example.myservice', 1642 moduleName: 'entry', 1643 abilityName: 'MainAbility', 1644 deviceId: '', 1645 parameters: { 1646 'ohos.aafwk.param.callAbilityToForeground': true 1647 } 1648 }; 1649 1650 try { 1651 this.context.startAbilityByCall(wantForeground) 1652 .then((obj) => { 1653 // Carry out normal service processing. 1654 caller = obj; 1655 console.log('startAbilityByCall succeed'); 1656 }).catch((error) => { 1657 // Process service logic errors. 1658 console.log('startAbilityByCall failed, error.code: ' + JSON.stringify(error.code) + 1659 ' error.message: ' + JSON.stringify(error.message)); 1660 }); 1661 } catch (paramError) { 1662 // Process input parameter errors. 1663 console.log('error.code: ' + JSON.stringify(paramError.code) + 1664 ' error.message: ' + JSON.stringify(paramError.message)); 1665 } 1666 ``` 1667 1668## UIAbilityContext.startAbilityWithAccount 1669 1670startAbilityWithAccount(want: Want, accountId: number, callback: AsyncCallback\<void\>): void; 1671 1672Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result. 1673 1674**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1675 1676**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1677 1678**System API**: This is a system API and cannot be called by third-party applications. 1679 1680**Parameters** 1681 1682| Name| Type| Mandatory| Description| 1683| -------- | -------- | -------- | -------- | 1684| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 1685| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 1686| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 1687 1688**Error codes** 1689 1690| ID| Error Message| 1691| ------- | -------------------------------- | 1692| 16000001 | The specified ability does not exist. | 1693| 16000002 | Incorrect ability type. | 1694| 16000004 | Can not start invisible component. | 1695| 16000005 | The specified process does not have the permission. | 1696| 16000006 | Cross-user operations are not allowed. | 1697| 16000008 | The crowdtesting application expires. | 1698| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1699| 16000010 | The call with the continuation flag is forbidden. | 1700| 16000011 | The context does not exist. | 1701| 16000017 | The previous ability is starting, wait start later. | 1702| 16000050 | Internal error. | 1703| 16000053 | The ability is not on the top of the UI. | 1704| 16000055 | Installation-free timed out. | 1705| 16200001 | The caller has been released. | 1706 1707**Example** 1708 1709 ```ts 1710 let want = { 1711 deviceId: '', 1712 bundleName: 'com.extreme.test', 1713 abilityName: 'MainAbility' 1714 }; 1715 let accountId = 100; 1716 1717 try { 1718 this.context.startAbilityWithAccount(want, accountId, (error) => { 1719 if (error.code) { 1720 // Process service logic errors. 1721 console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 1722 ' error.message: ' + JSON.stringify(error.message)); 1723 return; 1724 } 1725 // Carry out normal service processing. 1726 console.log('startAbilityWithAccount succeed'); 1727 }); 1728 } catch (paramError) { 1729 // Process input parameter errors. 1730 console.log('error.code: ' + JSON.stringify(paramError.code) + 1731 ' error.message: ' + JSON.stringify(paramError.message)); 1732 } 1733 ``` 1734 1735 1736## UIAbilityContext.startAbilityWithAccount 1737 1738startAbilityWithAccount(want: Want, accountId: number, options: StartOptions, callback: AsyncCallback\<void\>): void; 1739 1740Starts an ability with the account ID specified. This API uses an asynchronous callback to return the result. 1741 1742**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1743 1744**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1745 1746**System API**: This is a system API and cannot be called by third-party applications. 1747 1748**Parameters** 1749 1750| Name| Type| Mandatory| Description| 1751| -------- | -------- | -------- | -------- | 1752| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 1753| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 1754| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 1755| callback | AsyncCallback\<void\> | Yes| Callback used to return the result.| 1756 1757**Error codes** 1758 1759| ID| Error Message| 1760| ------- | -------------------------------- | 1761| 16000001 | The specified ability does not exist. | 1762| 16000002 | Incorrect ability type. | 1763| 16000004 | Can not start invisible component. | 1764| 16000005 | The specified process does not have the permission. | 1765| 16000006 | Cross-user operations are not allowed. | 1766| 16000008 | The crowdtesting application expires. | 1767| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1768| 16000010 | The call with the continuation flag is forbidden. | 1769| 16000011 | The context does not exist. | 1770| 16000017 | The previous ability is starting, wait start later. | 1771| 16000050 | Internal error. | 1772| 16000053 | The ability is not on the top of the UI. | 1773| 16000055 | Installation-free timed out. | 1774| 16200001 | The caller has been released. | 1775 1776**Example** 1777 1778 ```ts 1779 let want = { 1780 deviceId: '', 1781 bundleName: 'com.extreme.test', 1782 abilityName: 'MainAbility' 1783 }; 1784 let accountId = 100; 1785 let options = { 1786 windowMode: 0 1787 }; 1788 1789 try { 1790 this.context.startAbilityWithAccount(want, accountId, options, (error) => { 1791 if (error.code) { 1792 // Process service logic errors. 1793 console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 1794 ' error.message: ' + JSON.stringify(error.message)); 1795 return; 1796 } 1797 // Carry out normal service processing. 1798 console.log('startAbilityWithAccount succeed'); 1799 }); 1800 } catch (paramError) { 1801 // Process input parameter errors. 1802 console.log('error.code: ' + JSON.stringify(paramError.code) + 1803 ' error.message: ' + JSON.stringify(paramError.message)); 1804 } 1805 ``` 1806 1807 1808## UIAbilityContext.startAbilityWithAccount 1809 1810startAbilityWithAccount(want: Want, accountId: number, options?: StartOptions): Promise\<void\>; 1811 1812Starts an ability with the account ID specified. This API uses a promise to return the result. 1813 1814**Required permissions**: ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS 1815 1816**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1817 1818**System API**: This is a system API and cannot be called by third-party applications. 1819 1820**Parameters** 1821 1822| Name| Type| Mandatory| Description| 1823| -------- | -------- | -------- | -------- | 1824| want | [Want](js-apis-application-want.md) | Yes| Want information about the target ability.| 1825| accountId | number | Yes| ID of a system account. For details, see [getCreatedOsAccountsCount](js-apis-osAccount.md#getosaccountlocalidfromprocess).| 1826| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 1827 1828**Error codes** 1829 1830| ID| Error Message| 1831| ------- | -------------------------------- | 1832| 16000001 | The specified ability does not exist. | 1833| 16000002 | Incorrect ability type. | 1834| 16000004 | Can not start invisible component. | 1835| 16000005 | The specified process does not have the permission. | 1836| 16000006 | Cross-user operations are not allowed. | 1837| 16000008 | The crowdtesting application expires. | 1838| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1839| 16000010 | The call with the continuation flag is forbidden. | 1840| 16000011 | The context does not exist. | 1841| 16000017 | The previous ability is starting, wait start later. | 1842| 16000050 | Internal error. | 1843| 16000053 | The ability is not on the top of the UI. | 1844| 16000055 | Installation-free timed out. | 1845| 16200001 | The caller has been released. | 1846 1847**Example** 1848 1849 ```ts 1850 let want = { 1851 deviceId: '', 1852 bundleName: 'com.extreme.test', 1853 abilityName: 'MainAbility' 1854 }; 1855 let accountId = 100; 1856 let options = { 1857 windowMode: 0 1858 }; 1859 1860 try { 1861 this.context.startAbilityWithAccount(want, accountId, options) 1862 .then((data) => { 1863 // Carry out normal service processing. 1864 console.log('startAbilityWithAccount succeed'); 1865 }) 1866 .catch((error) => { 1867 // Process service logic errors. 1868 console.log('startAbilityWithAccount failed, error.code: ' + JSON.stringify(error.code) + 1869 ' error.message: ' + JSON.stringify(error.message)); 1870 }); 1871 } catch (paramError) { 1872 // Process input parameter errors. 1873 console.log('error.code: ' + JSON.stringify(paramError.code) + 1874 ' error.message: ' + JSON.stringify(paramError.message)); 1875 } 1876 ``` 1877 1878## UIAbilityContext.requestPermissionsFromUser 1879 1880requestPermissionsFromUser(permissions: Array<string>, requestCallback: AsyncCallback<PermissionRequestResult>) : void; 1881 1882Requests permissions from the user by displaying a dialog box. This API uses an asynchronous callback to return the result. 1883 1884**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1885 1886**Parameters** 1887 1888| Name| Type| Mandatory| Description| 1889| -------- | -------- | -------- | -------- | 1890| permissions | Array<string> | Yes| Permissions to request.| 1891| callback | AsyncCallback<[PermissionRequestResult](js-apis-permissionRequestResult.md)> | Yes| Callback used to return the result.| 1892 1893**Example** 1894 1895 ```ts 1896 let permissions=['com.example.permission'] 1897 this.context.requestPermissionsFromUser(permissions,(result) => { 1898 console.log('requestPermissionsFromUserresult:' + JSON.stringify(result)); 1899 }); 1900 1901 ``` 1902 1903 1904## UIAbilityContext.requestPermissionsFromUser 1905 1906requestPermissionsFromUser(permissions: Array<string>) : Promise<PermissionRequestResult>; 1907 1908Requests permissions from the user by displaying a dialog box. This API uses a promise to return the result. 1909 1910**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1911 1912**Parameters** 1913 1914| Name| Type| Mandatory| Description| 1915| -------- | -------- | -------- | -------- | 1916| permissions | Array<string> | Yes| Permissions to request.| 1917 1918**Return value** 1919 1920| Type| Description| 1921| -------- | -------- | 1922| Promise<[PermissionRequestResult](js-apis-permissionRequestResult.md)> | Promise used to return the result.| 1923 1924**Example** 1925 1926 ```ts 1927 let permissions=['com.example.permission'] 1928 this.context.requestPermissionsFromUser(permissions).then((data) => { 1929 console.log('success:' + JSON.stringify(data)); 1930 }).catch((error) => { 1931 console.log('failed:' + JSON.stringify(error)); 1932 }); 1933 1934 ``` 1935 1936 1937## UIAbilityContext.setMissionLabel 1938 1939setMissionLabel(label: string, callback:AsyncCallback<void>): void; 1940 1941Sets a label for this ability in the mission. This API uses an asynchronous callback to return the result. 1942 1943**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1944 1945**Parameters** 1946 1947| Name| Type| Mandatory| Description| 1948| -------- | -------- | -------- | -------- | 1949| label | string | Yes| Label of the ability to set.| 1950| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1951 1952**Example** 1953 1954 ```ts 1955 this.context.setMissionLabel('test',(result) => { 1956 console.log('requestPermissionsFromUserresult:' + JSON.stringify(result)); 1957 }); 1958 ``` 1959 1960 1961## UIAbilityContext.setMissionLabel 1962 1963setMissionLabel(label: string): Promise<void>; 1964 1965Sets a label for this ability in the mission. This API uses a promise to return the result. 1966 1967**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1968 1969**Parameters** 1970 1971| Name| Type| Mandatory| Description| 1972| -------- | -------- | -------- | -------- | 1973| label | string | Yes| Label of the ability to set.| 1974 1975**Return value** 1976 1977| Type| Description| 1978| -------- | -------- | 1979| Promise<void> | Promise used to return the result.| 1980 1981**Error codes** 1982 1983| ID| Error Message| 1984| ------- | -------------------------------- | 1985| 16000011 | The context does not exist. | 1986| 16000050 | Internal error. | 1987 1988**Example** 1989 1990 ```ts 1991 this.context.setMissionLabel('test').then(() => { 1992 console.log('success'); 1993 }).catch((error) => { 1994 console.log('failed:' + JSON.stringify(error)); 1995 }); 1996 ``` 1997## UIAbilityContext.setMissionIcon 1998 1999setMissionIcon(icon: image.PixelMap, callback:AsyncCallback\<void>): void; 2000 2001Sets an icon for this ability in the mission. This API uses an asynchronous callback to return the result. 2002 2003**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2004 2005**System API**: This is a system API and cannot be called by third-party applications. 2006 2007**Parameters** 2008 2009| Name| Type| Mandatory| Description| 2010| -------- | -------- | -------- | -------- | 2011| icon | image.PixelMap | Yes| Icon of the ability to set.| 2012| callback | AsyncCallback\<void> | Yes| Callback used to return the result.| 2013 2014**Error codes** 2015 2016| ID| Error Message| 2017| ------- | -------------------------------- | 2018| 16000011 | The context does not exist. | 2019| 16000050 | Internal error. | 2020 2021**Example** 2022 2023 ```ts 2024 import image from '@ohos.multimedia.image'; 2025 let imagePixelMap; 2026 let color = new ArrayBuffer(0); 2027 let initializationOptions = { 2028 size: { 2029 height: 100, 2030 width: 100 2031 } 2032 }; 2033 image.createPixelMap(color, initializationOptions) 2034 .then((data) => { 2035 imagePixelMap = data; 2036 }) 2037 .catch((err) => { 2038 console.log('--------- createPixelMap fail, err: ---------', err) 2039 }); 2040 this.context.setMissionIcon(imagePixelMap, (err) => { 2041 console.log('---------- setMissionIcon fail, err: -----------', err); 2042 }) 2043 ``` 2044 2045 2046## UIAbilityContext.setMissionIcon 2047 2048setMissionIcon(icon: image.PixelMap): Promise\<void>; 2049 2050Sets an icon for this ability in the mission. This API uses a promise to return the result. 2051 2052**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2053 2054**System API**: This is a system API and cannot be called by third-party applications. 2055 2056**Parameters** 2057 2058| Name| Type| Mandatory| Description| 2059| -------- | -------- | -------- | -------- | 2060| icon | image.PixelMap | Yes| Icon of the ability to set.| 2061 2062**Return value** 2063 2064| Type| Description| 2065| -------- | -------- | 2066| Promise<void> | Promise used to return the result.| 2067 2068**Error codes** 2069 2070| ID| Error Message| 2071| ------- | -------------------------------- | 2072| 16000011 | The context does not exist. | 2073| 16000050 | Internal error. | 2074 2075**Example** 2076 2077 ```ts 2078 import image from '@ohos.multimedia.image'; 2079 let imagePixelMap; 2080 let color = new ArrayBuffer(0); 2081 let initializationOptions = { 2082 size: { 2083 height: 100, 2084 width: 100 2085 } 2086 }; 2087 image.createPixelMap(color, initializationOptions) 2088 .then((data) => { 2089 imagePixelMap = data; 2090 }) 2091 .catch((err) => { 2092 console.log('--------- createPixelMap fail, err: ---------', err) 2093 }); 2094 this.context.setMissionIcon(imagePixelMap) 2095 .then(() => { 2096 console.log('-------------- setMissionIcon success -------------'); 2097 }) 2098 .catch((err) => { 2099 console.log('-------------- setMissionIcon fail, err: -------------', err); 2100 }); 2101 ``` 2102## UIAbilityContext.restoreWindowStage 2103 2104restoreWindowStage(localStorage: LocalStorage) : void; 2105 2106Restores the window stage data for this ability. 2107 2108**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2109 2110**Parameters** 2111 2112| Name| Type| Mandatory| Description| 2113| -------- | -------- | -------- | -------- | 2114| localStorage | image.LocalStorage | Yes| Storage used to store the restored window stage.| 2115 2116**Error codes** 2117 2118| ID| Error Message| 2119| ------- | -------------------------------- | 2120| 16000011 | The context does not exist. | 2121| 16000050 | Internal error. | 2122 2123**Example** 2124 2125 ```ts 2126 let storage = new LocalStorage(); 2127 this.context.restoreWindowStage(storage); 2128 ``` 2129 2130## UIAbilityContext.isTerminating 2131 2132isTerminating(): boolean; 2133 2134Checks whether this ability is in the terminating state. 2135 2136**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2137 2138**Return value** 2139 2140| Type| Description| 2141| -------- | -------- | 2142| boolean| The value **true** means that the UIAbility is in the terminating state, and **false** means the opposite.| 2143 2144**Error codes** 2145 2146| ID| Error Message| 2147| ------- | -------------------------------- | 2148| 16000011 | The context does not exist. | 2149| 16000050 | Internal error. | 2150 2151**Example** 2152 2153 ```ts 2154 let isTerminating = this.context.isTerminating(); 2155 console.log('ability state :' + isTerminating); 2156 ``` 2157