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