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