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 | Failed to start the invisible ability. | 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 and prepare 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 | Redirection to a third-party application is not allowed in API version 11 or later. | 72| 16000019 | No matching ability is found. | 73| 16000050 | Internal error. | 74| 16000053 | The ability is not on the top of the UI. | 75| 16000055 | Installation-free timed out. | 76| 16000071 | App clone is not supported. | 77| 16000072 | App clone or multi-instance is not supported. | 78| 16000073 | The app clone index is invalid. | 79| 16000076 | The app instance key is invalid. | 80| 16000077 | The number of app instances reaches the limit. | 81| 16000078 | The multi-instance is not supported. | 82| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 83| 16000080 | Creating a new instance is not supported. | 84| 16200001 | The caller has been released. | 85 86**Example** 87 88```ts 89import { UIAbility, Want } from '@kit.AbilityKit'; 90import { BusinessError } from '@kit.BasicServicesKit'; 91 92export default class EntryAbility extends UIAbility { 93 onForeground() { 94 let want: Want = { 95 bundleName: 'com.example.myapplication', 96 abilityName: 'EntryAbility' 97 }; 98 99 try { 100 this.context.startAbility(want, (err: BusinessError) => { 101 if (err.code) { 102 // Process service logic errors. 103 console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); 104 return; 105 } 106 // Carry out normal service processing. 107 console.info('startAbility succeed'); 108 }); 109 } catch (err) { 110 // Process input parameter errors. 111 let code = (err as BusinessError).code; 112 let message = (err as BusinessError).message; 113 console.error(`startAbility failed, code is ${code}, message is ${message}`); 114 } 115 } 116} 117``` 118 119## UIAbilityContext.startAbility 120 121startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 122 123Starts an ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 124 125> **NOTE** 126> 127> 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). 128 129**Atomic service API**: This API can be used in atomic services since API version 11. 130 131**System capability**: SystemCapability.Ability.AbilityRuntime.Core 132 133**Parameters** 134 135| Name| Type| Mandatory| Description| 136| -------- | -------- | -------- | -------- | 137| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 138| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 139| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 140 141**Error codes** 142 143For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 144 145| ID| Error Message| 146| ------- | -------------------------------- | 147| 201 | The application does not have permission to call the interface. | 148| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 149| 801 | Capability not support. | 150| 16000001 | The specified ability does not exist. | 151| 16000004 | Failed to start the invisible ability. | 152| 16000005 | The specified process does not have the permission. | 153| 16000006 | Cross-user operations are not allowed. | 154| 16000008 | The crowdtesting application expires. | 155| 16000009 | An ability cannot be started or stopped in Wukong mode. | 156| 16000011 | The context does not exist. | 157| 16000012 | The application is controlled. | 158| 16000013 | The application is controlled by EDM. | 159| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 160| 16000019 | No matching ability is found. | 161| 16000050 | Internal error. | 162| 16000053 | The ability is not on the top of the UI. | 163| 16000055 | Installation-free timed out. | 164| 16000067 | The StartOptions check failed. | 165| 16000068 | The ability is already running. | 166| 16300003 | The target application is not self application. | 167| 16000071 | App clone is not supported. | 168| 16000072 | App clone or multi-instance is not supported. | 169| 16000073 | The app clone index is invalid. | 170| 16000076 | The app instance key is invalid. | 171| 16000077 | The number of app instances reaches the limit. | 172| 16000078 | The multi-instance is not supported. | 173| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 174| 16000080 | Creating a new instance is not supported. | 175| 16200001 | The caller has been released. | 176 177**Example** 178 179```ts 180import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; 181import { BusinessError } from '@kit.BasicServicesKit'; 182 183export default class EntryAbility extends UIAbility { 184 onForeground() { 185 let want: Want = { 186 deviceId: '', 187 bundleName: 'com.example.myapplication', 188 abilityName: 'EntryAbility' 189 }; 190 let options: StartOptions = { 191 displayId: 0 192 }; 193 194 try { 195 this.context.startAbility(want, options, (err: BusinessError) => { 196 if (err.code) { 197 // Process service logic errors. 198 console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); 199 return; 200 } 201 // Carry out normal service processing. 202 console.info('startAbility succeed'); 203 }); 204 } catch (err) { 205 // Process input parameter errors. 206 let code = (err as BusinessError).code; 207 let message = (err as BusinessError).message; 208 console.error(`startAbility failed, code is ${code}, message is ${message}`); 209 } 210 } 211} 212``` 213 214## UIAbilityContext.startAbility 215 216startAbility(want: Want, options?: StartOptions): Promise<void> 217 218Starts an ability. This API uses a promise to return the result. It can be called only by the main thread. 219 220> **NOTE** 221> 222> 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). 223 224**Atomic service API**: This API can be used in atomic services since API version 11. 225 226**System capability**: SystemCapability.Ability.AbilityRuntime.Core 227 228**Parameters** 229 230| Name| Type| Mandatory| Description| 231| -------- | -------- | -------- | -------- | 232| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 233| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 234 235**Return value** 236 237| Type| Description| 238| -------- | -------- | 239| Promise<void> | Promise used to return the result.| 240 241**Error codes** 242 243For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 244 245| ID| Error Message| 246| ------- | -------------------------------- | 247| 201 | The application does not have permission to call the interface. | 248| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 249| 801 | Capability not support. | 250| 16000001 | The specified ability does not exist. | 251| 16000002 | Incorrect ability type. | 252| 16000004 | Failed to start the invisible ability. | 253| 16000005 | The specified process does not have the permission. | 254| 16000006 | Cross-user operations are not allowed. | 255| 16000008 | The crowdtesting application expires. | 256| 16000009 | An ability cannot be started or stopped in Wukong mode. | 257| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 258| 16000011 | The context does not exist. | 259| 16000012 | The application is controlled. | 260| 16000013 | The application is controlled by EDM. | 261| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 262| 16000019 | No matching ability is found. | 263| 16000050 | Internal error. | 264| 16000053 | The ability is not on the top of the UI. | 265| 16000055 | Installation-free timed out. | 266| 16000067 | The StartOptions check failed. | 267| 16000068 | The ability is already running. | 268| 16300003 | The target application is not self application. | 269| 16000071 | App clone is not supported. | 270| 16000072 | App clone or multi-instance is not supported. | 271| 16000073 | The app clone index is invalid. | 272| 16000076 | The app instance key is invalid. | 273| 16000077 | The number of app instances reaches the limit. | 274| 16000078 | The multi-instance is not supported. | 275| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 276| 16000080 | Creating a new instance is not supported. | 277| 16200001 | The caller has been released. | 278 279**Example** 280 281```ts 282import { UIAbility, Want, StartOptions } from '@kit.AbilityKit'; 283import { BusinessError } from '@kit.BasicServicesKit'; 284 285export default class EntryAbility extends UIAbility { 286 onForeground() { 287 let want: Want = { 288 bundleName: 'com.example.myapplication', 289 abilityName: 'EntryAbility' 290 }; 291 let options: StartOptions = { 292 displayId: 0 293 }; 294 295 try { 296 this.context.startAbility(want, options) 297 .then(() => { 298 // Carry out normal service processing. 299 console.info('startAbility succeed'); 300 }) 301 .catch((err: BusinessError) => { 302 // Process service logic errors. 303 console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); 304 }); 305 } catch (err) { 306 // Process input parameter errors. 307 let code = (err as BusinessError).code; 308 let message = (err as BusinessError).message; 309 console.error(`startAbility failed, code is ${code}, message is ${message}`); 310 } 311 } 312} 313``` 314 315## UIAbilityContext.startAbilityForResult 316 317startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void 318 319Starts an ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 320 321The following situations may be possible for a started ability: 322 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. 323 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 324 - 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. 325 326> **NOTE** 327> 328> 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). 329 330**Atomic service API**: This API can be used in atomic services since API version 11. 331 332**System capability**: SystemCapability.Ability.AbilityRuntime.Core 333 334**Parameters** 335 336| Name| Type| Mandatory| Description| 337| -------- | -------- | -------- | -------- | 338| want |[Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 339| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result.| 340 341**Error codes** 342 343For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 344 345| ID| Error Message| 346| ------- | -------------------------------- | 347| 201 | The application does not have permission to call the interface. | 348| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 349| 16000001 | The specified ability does not exist. | 350| 16000002 | Incorrect ability type. | 351| 16000004 | Failed to start the invisible ability. | 352| 16000005 | The specified process does not have the permission. | 353| 16000006 | Cross-user operations are not allowed. | 354| 16000008 | The crowdtesting application expires. | 355| 16000009 | An ability cannot be started or stopped in Wukong mode. | 356| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 357| 16000011 | The context does not exist. | 358| 16000012 | The application is controlled. | 359| 16000013 | The application is controlled by EDM. | 360| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 361| 16000019 | No matching ability is found. | 362| 16000050 | Internal error. | 363| 16000053 | The ability is not on the top of the UI. | 364| 16000055 | Installation-free timed out. | 365| 16000071 | App clone is not supported. | 366| 16000072 | App clone or multi-instance is not supported. | 367| 16000073 | The app clone index is invalid. | 368| 16000076 | The app instance key is invalid. | 369| 16000077 | The number of app instances reaches the limit. | 370| 16000078 | The multi-instance is not supported. | 371| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 372| 16000080 | Creating a new instance is not supported. | 373| 16200001 | The caller has been released. | 374 375**Example** 376 377```ts 378import { UIAbility, Want, common } from '@kit.AbilityKit'; 379import { BusinessError } from '@kit.BasicServicesKit'; 380 381export default class EntryAbility extends UIAbility { 382 onForeground() { 383 let want: Want = { 384 deviceId: '', 385 bundleName: 'com.example.myapplication', 386 abilityName: 'EntryAbility' 387 }; 388 389 try { 390 this.context.startAbilityForResult(want, (err: BusinessError, result: common.AbilityResult) => { 391 if (err.code) { 392 // Process service logic errors. 393 console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); 394 return; 395 } 396 // Carry out normal service processing. 397 console.info('startAbilityForResult succeed'); 398 }); 399 } catch (err) { 400 // Process input parameter errors. 401 let code = (err as BusinessError).code; 402 let message = (err as BusinessError).message; 403 console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`); 404 } 405 } 406} 407``` 408 409## UIAbilityContext.startAbilityForResult 410 411startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void 412 413Starts 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. 414 415The following situations may be possible for a started ability: 416 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. 417 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 418 - 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. 419 420> **NOTE** 421> 422> 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). 423 424**Atomic service API**: This API can be used in atomic services since API version 11. 425 426**System capability**: SystemCapability.Ability.AbilityRuntime.Core 427 428**Parameters** 429 430| Name| Type| Mandatory| Description| 431| -------- | -------- | -------- | -------- | 432| want |[Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 433| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 434| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result.| 435 436**Error codes** 437 438For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 439 440| ID| Error Message| 441| ------- | -------------------------------- | 442| 201 | The application does not have permission to call the interface. | 443| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 444| 16000001 | The specified ability does not exist. | 445| 16000004 | Failed to start the invisible ability. | 446| 16000005 | The specified process does not have the permission. | 447| 16000006 | Cross-user operations are not allowed. | 448| 16000008 | The crowdtesting application expires. | 449| 16000009 | An ability cannot be started or stopped in Wukong mode. | 450| 16000011 | The context does not exist. | 451| 16000012 | The application is controlled. | 452| 16000013 | The application is controlled by EDM. | 453| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 454| 16000019 | No matching ability is found. | 455| 16000050 | Internal error. | 456| 16000053 | The ability is not on the top of the UI. | 457| 16000055 | Installation-free timed out. | 458| 16000071 | App clone is not supported. | 459| 16000072 | App clone or multi-instance is not supported. | 460| 16000073 | The app clone index is invalid. | 461| 16000076 | The app instance key is invalid. | 462| 16000077 | The number of app instances reaches the limit. | 463| 16000078 | The multi-instance is not supported. | 464| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 465| 16000080 | Creating a new instance is not supported. | 466| 16200001 | The caller has been released. | 467 468**Example** 469 470```ts 471import { UIAbility, Want, common, StartOptions } from '@kit.AbilityKit'; 472import { BusinessError } from '@kit.BasicServicesKit'; 473 474export default class EntryAbility extends UIAbility { 475 onForeground() { 476 let want: Want = { 477 deviceId: '', 478 bundleName: 'com.example.myapplication', 479 abilityName: 'EntryAbility' 480 }; 481 let options: StartOptions = { 482 displayId: 0 483 }; 484 485 try { 486 this.context.startAbilityForResult(want, options, (err: BusinessError, result: common.AbilityResult) => { 487 if (err.code) { 488 // Process service logic errors. 489 console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); 490 return; 491 } 492 // Carry out normal service processing. 493 console.info('startAbilityForResult succeed'); 494 }); 495 } catch (err) { 496 // Process input parameter errors. 497 let code = (err as BusinessError).code; 498 let message = (err as BusinessError).message; 499 console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`); 500 } 501 } 502} 503``` 504 505 506## UIAbilityContext.startAbilityForResult 507 508startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult> 509 510Starts an ability. This API uses a promise to return the result. It can be called only by the main thread. 511 512The following situations may be possible for a started ability: 513 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. 514 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 515 - 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. 516 517> **NOTE** 518> 519> 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). 520 521**Atomic service API**: This API can be used in atomic services since API version 11. 522 523**System capability**: SystemCapability.Ability.AbilityRuntime.Core 524 525**Parameters** 526 527| Name| Type| Mandatory| Description| 528| -------- | -------- | -------- | -------- | 529| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 530| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 531 532 533**Return value** 534 535| Type| Description| 536| -------- | -------- | 537| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise used to return the result.| 538 539**Error codes** 540 541For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 542 543| ID| Error Message| 544| ------- | -------------------------------- | 545| 201 | The application does not have permission to call the interface. | 546| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 547| 16000001 | The specified ability does not exist. | 548| 16000002 | Incorrect ability type. | 549| 16000004 | Failed to start the invisible ability. | 550| 16000005 | The specified process does not have the permission. | 551| 16000006 | Cross-user operations are not allowed. | 552| 16000008 | The crowdtesting application expires. | 553| 16000009 | An ability cannot be started or stopped in Wukong mode. | 554| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 555| 16000011 | The context does not exist. | 556| 16000012 | The application is controlled. | 557| 16000013 | The application is controlled by EDM. | 558| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 559| 16000019 | No matching ability is found. | 560| 16000050 | Internal error. | 561| 16000053 | The ability is not on the top of the UI. | 562| 16000055 | Installation-free timed out. | 563| 16000071 | App clone is not supported. | 564| 16000072 | App clone or multi-instance is not supported. | 565| 16000073 | The app clone index is invalid. | 566| 16000076 | The app instance key is invalid. | 567| 16000077 | The number of app instances reaches the limit. | 568| 16000078 | The multi-instance is not supported. | 569| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 570| 16000080 | Creating a new instance is not supported. | 571| 16200001 | The caller has been released. | 572 573**Example** 574 575```ts 576import { UIAbility, Want, common, StartOptions } from '@kit.AbilityKit'; 577import { BusinessError } from '@kit.BasicServicesKit'; 578 579export default class EntryAbility extends UIAbility { 580 onForeground() { 581 let want: Want = { 582 bundleName: 'com.example.myapplication', 583 abilityName: 'EntryAbility' 584 }; 585 let options: StartOptions = { 586 displayId: 0 587 }; 588 589 try { 590 this.context.startAbilityForResult(want, options) 591 .then((result: common.AbilityResult) => { 592 // Carry out normal service processing. 593 console.info('startAbilityForResult succeed'); 594 }) 595 .catch((err: BusinessError) => { 596 // Process service logic errors. 597 console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); 598 }); 599 } catch (err) { 600 // Process input parameter errors. 601 let code = (err as BusinessError).code; 602 let message = (err as BusinessError).message; 603 console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`); 604 } 605 } 606} 607``` 608 609## UIAbilityContext.terminateSelf 610 611terminateSelf(callback: AsyncCallback<void>): void 612 613Terminates this ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 614 615> **NOTE** 616> 617> 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**. 618 619**Atomic service API**: This API can be used in atomic services since API version 11. 620 621**System capability**: SystemCapability.Ability.AbilityRuntime.Core 622 623**Parameters** 624 625| Name| Type| Mandatory| Description| 626| -------- | -------- | -------- | -------- | 627| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 628 629**Error codes** 630 631For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 632 633| ID| Error Message| 634| ------- | -------------------------------- | 635| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 636| 16000009 | An ability cannot be started or stopped in Wukong mode. | 637| 16000011 | The context does not exist. | 638| 16000050 | Internal error. | 639 640**Example** 641 642```ts 643import { UIAbility } from '@kit.AbilityKit'; 644import { BusinessError } from '@kit.BasicServicesKit'; 645 646export default class EntryAbility extends UIAbility { 647 onForeground() { 648 try { 649 this.context.terminateSelf((err: BusinessError) => { 650 if (err.code) { 651 // Process service logic errors. 652 console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`); 653 return; 654 } 655 // Carry out normal service processing. 656 console.info('terminateSelf succeed'); 657 }); 658 } catch (err) { 659 // Capture the synchronization parameter error. 660 let code = (err as BusinessError).code; 661 let message = (err as BusinessError).message; 662 console.error(`terminateSelf failed, code is ${code}, message is ${message}`); 663 } 664 } 665} 666``` 667 668 669## UIAbilityContext.terminateSelf 670 671terminateSelf(): Promise<void> 672 673Terminates this ability. This API uses a promise to return the result. It can be called only by the main thread. 674 675> **NOTE** 676> 677> 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**. 678 679**Atomic service API**: This API can be used in atomic services since API version 11. 680 681**System capability**: SystemCapability.Ability.AbilityRuntime.Core 682 683**Return value** 684 685| Type| Description| 686| -------- | -------- | 687| Promise<void> | Promise used to return the result.| 688 689**Error codes** 690 691For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 692 693| ID| Error Message| 694| ------- | -------------------------------- | 695| 16000009 | An ability cannot be started or stopped in Wukong mode. | 696| 16000011 | The context does not exist. | 697| 16000050 | Internal error. | 698 699 700**Example** 701 702```ts 703import { UIAbility } from '@kit.AbilityKit'; 704import { BusinessError } from '@kit.BasicServicesKit'; 705 706export default class EntryAbility extends UIAbility { 707 onForeground() { 708 try { 709 this.context.terminateSelf() 710 .then(() => { 711 // Carry out normal service processing. 712 console.info('terminateSelf succeed'); 713 }) 714 .catch((err: BusinessError) => { 715 // Process service logic errors. 716 console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`); 717 }); 718 } catch (err) { 719 // Capture the synchronization parameter error. 720 let code = (err as BusinessError).code; 721 let message = (err as BusinessError).message; 722 console.error(`terminateSelf failed, code is ${code}, message is ${message}`); 723 } 724 } 725} 726``` 727 728 729## UIAbilityContext.terminateSelfWithResult 730 731terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback<void>): void 732 733Terminates this ability. This API uses an asynchronous callback to return the result. It can be called only by the main thread. 734 735If 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. 736 737> **NOTE** 738> 739> 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**. 740 741**Atomic service API**: This API can be used in atomic services since API version 11. 742 743**System capability**: SystemCapability.Ability.AbilityRuntime.Core 744 745**Parameters** 746 747| Name| Type| Mandatory| Description| 748| -------- | -------- | -------- | -------- | 749| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the caller.| 750| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 751 752**Error codes** 753 754For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 755 756| ID| Error Message| 757| ------- | -------------------------------- | 758| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 759| 16000009 | An ability cannot be started or stopped in Wukong mode. | 760| 16000011 | The context does not exist. | 761| 16000050 | Internal error. | 762 763 764**Example** 765 766```ts 767import { UIAbility, Want, common } from '@kit.AbilityKit'; 768import { BusinessError } from '@kit.BasicServicesKit'; 769 770export default class EntryAbility extends UIAbility { 771 onForeground() { 772 let want: Want = { 773 bundleName: 'com.example.myapplication', 774 abilityName: 'EntryAbility' 775 }; 776 let resultCode = 100; 777 // AbilityResult information returned to the caller. 778 let abilityResult: common.AbilityResult = { 779 want, 780 resultCode 781 }; 782 783 try { 784 this.context.terminateSelfWithResult(abilityResult, (err: BusinessError) => { 785 if (err.code) { 786 // Process service logic errors. 787 console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`); 788 return; 789 } 790 // Carry out normal service processing. 791 console.info('terminateSelfWithResult succeed'); 792 }); 793 } catch (err) { 794 // Process input parameter errors. 795 let code = (err as BusinessError).code; 796 let message = (err as BusinessError).message; 797 console.error(`terminateSelfWithResult failed, code is ${code}, message is ${message}`); 798 } 799 } 800} 801``` 802 803 804## UIAbilityContext.terminateSelfWithResult 805 806terminateSelfWithResult(parameter: AbilityResult): Promise<void> 807 808Terminates this ability. This API uses a promise to return the result. It can be called only by the main thread. 809 810If 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. 811 812> **NOTE** 813> 814> 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**. 815 816**Atomic service API**: This API can be used in atomic services since API version 11. 817 818**System capability**: SystemCapability.Ability.AbilityRuntime.Core 819 820**Parameters** 821 822| Name| Type| Mandatory| Description| 823| -------- | -------- | -------- | -------- | 824| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Information returned to the caller.| 825 826**Return value** 827 828| Type| Description| 829| -------- | -------- | 830| Promise<void> | Promise used to return the result.| 831 832**Error codes** 833 834For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 835 836| ID| Error Message| 837| ------- | -------------------------------- | 838| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 839| 16000009 | An ability cannot be started or stopped in Wukong mode. | 840| 16000011 | The context does not exist. | 841| 16000050 | Internal error. | 842 843 844**Example** 845 846```ts 847import { UIAbility, Want, common } from '@kit.AbilityKit'; 848import { BusinessError } from '@kit.BasicServicesKit'; 849 850export default class EntryAbility extends UIAbility { 851 onForeground() { 852 let want: Want = { 853 bundleName: 'com.example.myapplication', 854 abilityName: 'EntryAbility' 855 }; 856 let resultCode = 100; 857 // AbilityResult information returned to the caller. 858 let abilityResult: common.AbilityResult = { 859 want, 860 resultCode 861 }; 862 863 try { 864 this.context.terminateSelfWithResult(abilityResult) 865 .then(() => { 866 // Carry out normal service processing. 867 console.info('terminateSelfWithResult succeed'); 868 }) 869 .catch((err: BusinessError) => { 870 // Process service logic errors. 871 console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`); 872 }); 873 } catch (err) { 874 // Process input parameter errors. 875 let code = (err as BusinessError).code; 876 let message = (err as BusinessError).message; 877 console.error(`terminateSelfWithResult failed, code is ${code}, message is ${message}`); 878 } 879 } 880} 881``` 882 883## UIAbilityContext.connectServiceExtensionAbility 884 885connectServiceExtensionAbility(want: Want, options: ConnectOptions): number 886 887Connects this ability to a ServiceExtensionAbility. This API can be called only by the main thread. 888 889> **NOTE** 890> 891> 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). 892 893**System capability**: SystemCapability.Ability.AbilityRuntime.Core 894 895**Parameters** 896 897| Name| Type| Mandatory| Description| 898| -------- | -------- | -------- | -------- | 899| want | [Want](js-apis-app-ability-want.md) | Yes| Want information for connecting to the ServiceExtensionAbility.| 900| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Instance of the callback function after the connection to the ServiceExtensionAbility is set up.| 901 902**Return value** 903 904| Type| Description| 905| -------- | -------- | 906| number | Result code of the connection.| 907 908**Error codes** 909 910For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 911 912| ID| Error Message| 913| ------- | -------------------------------- | 914| 201 | The application does not have permission to call the interface. | 915| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 916| 16000001 | The specified ability does not exist. | 917| 16000002 | Incorrect ability type. | 918| 16000004 | Failed to start the invisible ability. | 919| 16000005 | The specified process does not have the permission. | 920| 16000006 | Cross-user operations are not allowed. | 921| 16000008 | The crowdtesting application expires. | 922| 16000011 | The context does not exist. | 923| 16000050 | Internal error. | 924| 16000053 | The ability is not on the top of the UI. | 925| 16000055 | Installation-free timed out. | 926 927**Example** 928 929```ts 930import { UIAbility, Want, common } from '@kit.AbilityKit'; 931import { rpc } from '@kit.IPCKit'; 932import { BusinessError } from '@kit.BasicServicesKit'; 933 934export default class EntryAbility extends UIAbility { 935 onForeground() { 936 let want: Want = { 937 deviceId: '', 938 bundleName: 'com.example.myapplication', 939 abilityName: 'ServiceExtensionAbility' 940 }; 941 let commRemote: rpc.IRemoteObject; 942 let options: common.ConnectOptions = { 943 onConnect(elementName, remote) { 944 commRemote = remote; 945 console.info('onConnect...'); 946 }, 947 onDisconnect(elementName) { 948 console.info('onDisconnect...'); 949 }, 950 onFailed(code) { 951 console.info('onFailed...'); 952 } 953 }; 954 let connection: number; 955 956 try { 957 connection = this.context.connectServiceExtensionAbility(want, options); 958 } catch (err) { 959 // Process input parameter errors. 960 let code = (err as BusinessError).code; 961 let message = (err as BusinessError).message; 962 console.error(`connectServiceExtensionAbility failed, code is ${code}, message is ${message}`); 963 } 964 } 965} 966``` 967 968## UIAbilityContext.disconnectServiceExtensionAbility 969 970disconnectServiceExtensionAbility(connection: number): Promise\<void> 971 972Disconnects 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. 973 974**System capability**: SystemCapability.Ability.AbilityRuntime.Core 975 976**Parameters** 977 978| Name| Type| Mandatory| Description| 979| -------- | -------- | -------- | -------- | 980| connection | number | Yes| Digital code of the connected ServiceExtensionAbility, that is, connectionId returned by **connectServiceExtensionAbility**.| 981 982**Return value** 983 984| Type| Description| 985| -------- | -------- | 986| Promise\<void> | Promise used to return the result.| 987 988**Error codes** 989 990For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 991 992| ID| Error Message| 993| ------- | -------------------------------- | 994| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 995| 16000011 | The context does not exist. | 996| 16000050 | Internal error. | 997 998**Example** 999 1000```ts 1001import { UIAbility } from '@kit.AbilityKit'; 1002import { rpc } from '@kit.IPCKit'; 1003import { BusinessError } from '@kit.BasicServicesKit'; 1004 1005export default class EntryAbility extends UIAbility { 1006 onForeground() { 1007 // connection is the return value of connectServiceExtensionAbility. 1008 let connection = 1; 1009 let commRemote: rpc.IRemoteObject | null; 1010 1011 try { 1012 this.context.disconnectServiceExtensionAbility(connection).then(() => { 1013 commRemote = null; 1014 // Carry out normal service processing. 1015 console.info('disconnectServiceExtensionAbility succeed'); 1016 }).catch((err: BusinessError) => { 1017 // Process service logic errors. 1018 console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); 1019 }); 1020 } catch (err) { 1021 commRemote = null; 1022 // Process input parameter errors. 1023 let code = (err as BusinessError).code; 1024 let message = (err as BusinessError).message; 1025 console.error(`disconnectServiceExtensionAbility failed, code is ${code}, message is ${message}`); 1026 } 1027 } 1028} 1029``` 1030 1031## UIAbilityContext.disconnectServiceExtensionAbility 1032 1033disconnectServiceExtensionAbility(connection: number, callback: AsyncCallback\<void>): void 1034 1035Disconnects 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. 1036 1037**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1038 1039**Parameters** 1040 1041| Name| Type| Mandatory| Description| 1042| -------- | -------- | -------- | -------- | 1043| connection | number | Yes| Digital code of the connected ServiceExtensionAbility, that is, connectionId returned by **connectServiceExtensionAbility**.| 1044| callback | AsyncCallback\<void> | Yes| Callback used to return the result.| 1045 1046**Error codes** 1047 1048For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1049 1050| ID| Error Message| 1051| ------- | -------------------------------- | 1052| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1053| 16000011 | The context does not exist. | 1054| 16000050 | Internal error. | 1055 1056**Example** 1057 1058```ts 1059import { UIAbility } from '@kit.AbilityKit'; 1060import { rpc } from '@kit.IPCKit'; 1061import { BusinessError } from '@kit.BasicServicesKit'; 1062 1063export default class EntryAbility extends UIAbility { 1064 onForeground() { 1065 // connection is the return value of connectServiceExtensionAbility. 1066 let connection = 1; 1067 let commRemote: rpc.IRemoteObject | null; 1068 1069 try { 1070 this.context.disconnectServiceExtensionAbility(connection, (err: BusinessError) => { 1071 commRemote = null; 1072 if (err.code) { 1073 // Process service logic errors. 1074 console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); 1075 return; 1076 } 1077 // Carry out normal service processing. 1078 console.info('disconnectServiceExtensionAbility succeed'); 1079 }); 1080 } catch (err) { 1081 commRemote = null; 1082 // Process input parameter errors. 1083 let code = (err as BusinessError).code; 1084 let message = (err as BusinessError).message; 1085 console.error(`disconnectServiceExtensionAbility failed, code is ${code}, message is ${message}`); 1086 } 1087 } 1088} 1089``` 1090 1091## UIAbilityContext.startAbilityByCall 1092 1093startAbilityByCall(want: Want): Promise<Caller> 1094 1095Starts 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. 1096 1097This API cannot be used to start the UIAbility with the launch type set to [specified](../../application-models/uiability-launch-type.md#specified). 1098 1099> **NOTE** 1100> 1101> 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). 1102 1103**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 1104 1105> **NOTE** 1106> 1107> 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. 1108 1109**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1110 1111**Parameters** 1112 1113| Name| Type| Mandatory| Description| 1114| -------- | -------- | -------- | -------- | 1115| 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.| 1116 1117**Return value** 1118 1119| Type| Description| 1120| -------- | -------- | 1121| Promise<[Caller](js-apis-app-ability-uiAbility.md#caller)> | Promise used to return the caller object to communicate with.| 1122 1123**Error codes** 1124 1125For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1126 1127| ID| Error Message| 1128| ------- | -------------------------------- | 1129| 201 | The application does not have permission to call the interface. | 1130| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1131| 16000001 | The specified ability does not exist. | 1132| 16000002 | Incorrect ability type. | 1133| 16000004 | Failed to start the invisible ability. | 1134| 16000006 | Cross-user operations are not allowed. | 1135| 16000008 | The crowdtesting application expires. | 1136| 16000011 | The context does not exist. | 1137| 16000012 | The application is controlled. | 1138| 16000013 | The application is controlled by EDM. | 1139| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 1140| 16000050 | Internal error. | 1141| 16000071 | App clone is not supported. | 1142| 16000072 | App clone or multi-instance is not supported. | 1143| 16000073 | The app clone index is invalid. | 1144| 16000076 | The app instance key is invalid. | 1145| 16000077 | The number of app instances reaches the limit. | 1146| 16000078 | The multi-instance is not supported. | 1147| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 1148| 16000080 | Creating a new instance is not supported. | 1149 1150**Example** 1151 1152Start an ability in the background. 1153 1154```ts 1155import { UIAbility, Caller, Want } from '@kit.AbilityKit'; 1156import { BusinessError } from '@kit.BasicServicesKit'; 1157 1158export default class EntryAbility extends UIAbility { 1159 onForeground() { 1160 let caller: Caller; 1161 // Start an ability in the background by not passing parameters. 1162 let wantBackground: Want = { 1163 bundleName: 'com.example.myapplication', 1164 moduleName: 'entry', 1165 abilityName: 'EntryAbility', 1166 deviceId: '' 1167 }; 1168 1169 try { 1170 this.context.startAbilityByCall(wantBackground) 1171 .then((obj: Caller) => { 1172 // Carry out normal service processing. 1173 caller = obj; 1174 console.info('startAbilityByCall succeed'); 1175 }).catch((err: BusinessError) => { 1176 // Process service logic errors. 1177 console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`); 1178 }); 1179 } catch (err) { 1180 // Process input parameter errors. 1181 let code = (err as BusinessError).code; 1182 let message = (err as BusinessError).message; 1183 console.error(`startAbilityByCall failed, code is ${code}, message is ${message}`); 1184 } 1185 } 1186} 1187``` 1188 1189Start an ability in the foreground. 1190 1191```ts 1192import { UIAbility, Caller, Want } from '@kit.AbilityKit'; 1193import { BusinessError } from '@kit.BasicServicesKit'; 1194 1195export default class EntryAbility extends UIAbility { 1196 onForeground() { 1197 let caller: Caller; 1198 // Start an ability in the foreground with 'ohos.aafwk.param.callAbilityToForeground' in parameters set to true. 1199 let wantForeground: Want = { 1200 bundleName: 'com.example.myapplication', 1201 moduleName: 'entry', 1202 abilityName: 'EntryAbility', 1203 deviceId: '', 1204 parameters: { 1205 'ohos.aafwk.param.callAbilityToForeground': true 1206 } 1207 }; 1208 1209 try { 1210 this.context.startAbilityByCall(wantForeground) 1211 .then((obj: Caller) => { 1212 // Carry out normal service processing. 1213 caller = obj; 1214 console.info('startAbilityByCall succeed'); 1215 }).catch((err: BusinessError) => { 1216 // Process service logic errors. 1217 console.error(`startAbilityByCall failed, code is ${err.code}, message is ${err.message}`); 1218 }); 1219 } catch (err) { 1220 // Process input parameter errors. 1221 let code = (err as BusinessError).code; 1222 let message = (err as BusinessError).message; 1223 console.error(`startAbilityByCall failed, code is ${code}, message is ${message}`); 1224 } 1225 } 1226} 1227``` 1228 1229## UIAbilityContext.setMissionLabel 1230 1231setMissionLabel(label: string, callback: AsyncCallback<void>): void 1232 1233Sets a label for this UIAbility in the mission. This API uses an asynchronous callback to return the result. 1234 1235**Atomic service API**: This API can be used in atomic services since API version 11. 1236 1237**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1238 1239**Parameters** 1240 1241| Name| Type| Mandatory| Description| 1242| -------- | -------- | -------- | -------- | 1243| label | string | Yes| Label of the ability to set.| 1244| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1245 1246**Error codes** 1247 1248For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1249 1250| ID| Error Message| 1251| ------- | -------------------------------- | 1252| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1253| 16000011 | The context does not exist. | 1254| 16000050 | Internal error. | 1255 1256**Example** 1257 1258```ts 1259import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; 1260import { BusinessError } from '@kit.BasicServicesKit'; 1261 1262export default class EntryAbility extends UIAbility { 1263 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 1264 this.context.setMissionLabel('test', (result: BusinessError) => { 1265 console.info(`setMissionLabel: ${JSON.stringify(result)}`); 1266 }); 1267 } 1268} 1269``` 1270 1271## UIAbilityContext.setMissionLabel 1272 1273setMissionLabel(label: string): Promise<void> 1274 1275Sets a label for this UIAbility in the mission. This API uses a promise 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| label | string | Yes| Label of the ability to set.| 1286 1287**Return value** 1288 1289| Type| Description| 1290| -------- | -------- | 1291| Promise<void> | Promise used to return the result.| 1292 1293**Error codes** 1294 1295For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1296 1297| ID| Error Message| 1298| ------- | -------------------------------- | 1299| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1300| 16000011 | The context does not exist. | 1301| 16000050 | Internal error. | 1302 1303**Example** 1304 1305```ts 1306import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; 1307import { BusinessError } from '@kit.BasicServicesKit'; 1308 1309export default class EntryAbility extends UIAbility { 1310 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 1311 this.context.setMissionLabel('test').then(() => { 1312 console.info('success'); 1313 }).catch((err: BusinessError) => { 1314 let code = (err as BusinessError).code; 1315 let message = (err as BusinessError).message; 1316 console.error(`setMissionLabel failed, code is ${code}, message is ${message}`); 1317 }); 1318 } 1319} 1320``` 1321 1322## UIAbilityContext.setMissionContinueState<sup>10+</sup> 1323 1324setMissionContinueState(state: AbilityConstant.ContinueState, callback: AsyncCallback<void>): void 1325 1326Sets the mission continuation state of this UIAbility. This API uses an asynchronous callback to return the result. 1327 1328**Atomic service API**: This API can be used in atomic services since API version 11. 1329 1330**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1331 1332**Parameters** 1333 1334| Name| Type| Mandatory| Description| 1335| -------- | -------- | -------- | -------- | 1336| state | [AbilityConstant.ContinueState](js-apis-app-ability-abilityConstant.md#continuestate10) | Yes| Mission continuation state.| 1337| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1338 1339**Error codes** 1340 1341For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1342 1343| ID| Error Message| 1344| ------- | -------------------------------- | 1345| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1346| 16000011 | The context does not exist. | 1347| 16000050 | Internal error. | 1348 1349**Example** 1350 1351```ts 1352import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; 1353import { BusinessError } from '@kit.BasicServicesKit'; 1354 1355export default class EntryAbility extends UIAbility { 1356 onForeground() { 1357 this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE, (result: BusinessError) => { 1358 console.info(`setMissionContinueState: ${JSON.stringify(result)}`); 1359 }); 1360 } 1361} 1362``` 1363 1364## UIAbilityContext.setMissionContinueState<sup>10+</sup> 1365 1366setMissionContinueState(state: AbilityConstant.ContinueState): Promise<void> 1367 1368Sets the mission continuation state of this UIAbility. This API uses a promise to return the result. 1369 1370**Atomic service API**: This API can be used in atomic services since API version 11. 1371 1372**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1373 1374**Parameters** 1375 1376| Name| Type| Mandatory| Description| 1377| -------- | -------- | -------- | -------- | 1378| state | [AbilityConstant.ContinueState](js-apis-app-ability-abilityConstant.md#continuestate10) | Yes| Mission continuation state.| 1379 1380**Return value** 1381 1382| Type| Description| 1383| -------- | -------- | 1384| Promise<void> | Promise used to return the result.| 1385 1386**Error codes** 1387 1388For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1389 1390| ID| Error Message| 1391| ------- | -------------------------------- | 1392| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1393| 16000011 | The context does not exist. | 1394| 16000050 | Internal error. | 1395 1396**Example** 1397 1398```ts 1399import { UIAbility, AbilityConstant } from '@kit.AbilityKit'; 1400import { BusinessError } from '@kit.BasicServicesKit'; 1401 1402export default class EntryAbility extends UIAbility { 1403 onForeground() { 1404 this.context.setMissionContinueState(AbilityConstant.ContinueState.INACTIVE).then(() => { 1405 console.info('success'); 1406 }).catch((err: BusinessError) => { 1407 console.error(`setMissionContinueState failed, code is ${err.code}, message is ${err.message}`); 1408 }); 1409 } 1410} 1411``` 1412 1413## UIAbilityContext.restoreWindowStage 1414 1415restoreWindowStage(localStorage: LocalStorage): void 1416 1417Restores the WindowStage data in the ability. It can be called only by the main thread. 1418 1419**Atomic service API**: This API can be used in atomic services since API version 11. 1420 1421**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1422 1423**Parameters** 1424 1425| Name| Type| Mandatory| Description| 1426| -------- | -------- | -------- | -------- | 1427| localStorage | LocalStorage | Yes| Storage used to store the restored window stage.| 1428 1429**Error codes** 1430 1431For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1432 1433| ID| Error Message| 1434| ------- | -------------------------------- | 1435| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1436| 16000011 | The context does not exist. | 1437| 16000050 | Internal error. | 1438 1439**Example** 1440 1441```ts 1442import { UIAbility } from '@kit.AbilityKit'; 1443 1444export default class EntryAbility extends UIAbility { 1445 onForeground() { 1446 let storage = new LocalStorage(); 1447 this.context.restoreWindowStage(storage); 1448 } 1449} 1450``` 1451 1452## UIAbilityContext.isTerminating 1453 1454isTerminating(): boolean 1455 1456Checks whether this ability is in the terminating state. 1457 1458**Atomic service API**: This API can be used in atomic services since API version 11. 1459 1460**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1461 1462**Return value** 1463 1464| Type| Description| 1465| -------- | -------- | 1466| boolean | The value **true** means that the ability is in the terminating state, and **false** means the opposite.| 1467 1468**Error codes** 1469 1470For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1471 1472| ID| Error Message| 1473| ------- | -------------------------------- | 1474| 16000011 | The context does not exist. | 1475 1476**Example** 1477 1478```ts 1479import { UIAbility } from '@kit.AbilityKit'; 1480 1481export default class EntryAbility extends UIAbility { 1482 onForeground() { 1483 let isTerminating: boolean = this.context.isTerminating(); 1484 console.info(`ability state is ${isTerminating}`); 1485 } 1486} 1487``` 1488 1489## UIAbilityContext.requestDialogService 1490 1491requestDialogService(want: Want, result: AsyncCallback<dialogRequest.RequestResult>): void 1492 1493Starts 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. 1494 1495> **NOTE** 1496> 1497> 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). 1498 1499**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1500 1501**Parameters** 1502 1503| Name| Type| Mandatory| Description| 1504| -------- | -------- | -------- | -------- | 1505| want |[Want](js-apis-app-ability-want.md) | Yes| Want information for starting the ServiceExtensionAbility.| 1506| result | AsyncCallback<[dialogRequest.RequestResult](js-apis-app-ability-dialogRequest.md#requestresult)> | Yes| Callback used to return the result.| 1507 1508**Error codes** 1509 1510For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1511 1512| ID| Error Message| 1513| ------- | -------------------------------- | 1514| 201 | The application does not have permission to call the interface. | 1515| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1516| 16000001 | The specified ability does not exist. | 1517| 16000002 | Incorrect ability type. | 1518| 16000004 | Failed to start the invisible ability. | 1519| 16000005 | The specified process does not have the permission. | 1520| 16000006 | Cross-user operations are not allowed. | 1521| 16000008 | The crowdtesting application expires. | 1522| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1523| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 1524| 16000011 | The context does not exist. | 1525| 16000012 | The application is controlled. | 1526| 16000013 | The application is controlled by EDM. | 1527| 16000050 | Internal error. | 1528| 16000053 | The ability is not on the top of the UI. | 1529| 16000055 | Installation-free timed out. | 1530| 16200001 | The caller has been released. | 1531 1532**Example** 1533 1534```ts 1535import { UIAbility, Want, dialogRequest } from '@kit.AbilityKit'; 1536import { BusinessError } from '@kit.BasicServicesKit'; 1537 1538export default class EntryAbility extends UIAbility { 1539 onForeground() { 1540 let want: Want = { 1541 deviceId: '', 1542 bundleName: 'com.example.myapplication', 1543 abilityName: 'AuthAccountServiceExtension' 1544 }; 1545 1546 try { 1547 this.context.requestDialogService(want, (err: BusinessError, result: dialogRequest.RequestResult) => { 1548 if (err.code) { 1549 // Process service logic errors. 1550 console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`); 1551 return; 1552 } 1553 // Carry out normal service processing. 1554 console.info(`requestDialogService succeed, result = ${JSON.stringify(result)}`); 1555 }); 1556 } catch (err) { 1557 // Process input parameter errors. 1558 let code = (err as BusinessError).code; 1559 let message = (err as BusinessError).message; 1560 console.error(`requestDialogService failed, code is ${code}, message is ${message}`); 1561 } 1562 } 1563} 1564``` 1565 1566 ## UIAbilityContext.requestDialogService 1567 1568requestDialogService(want: Want): Promise<dialogRequest.RequestResult> 1569 1570Starts 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. 1571 1572> **NOTE** 1573> 1574> 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). 1575 1576**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1577 1578**Parameters** 1579 1580| Name| Type| Mandatory| Description| 1581| -------- | -------- | -------- | -------- | 1582| want | [Want](js-apis-app-ability-want.md) | Yes| Want information for starting the ServiceExtensionAbility.| 1583 1584 1585**Return value** 1586 1587| Type| Description| 1588| -------- | -------- | 1589| Promise<[dialogRequest.RequestResult](js-apis-app-ability-dialogRequest.md)> | Promise used to return the result. 1590 1591**Error codes** 1592 1593For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1594 1595| ID| Error Message| 1596| ------- | -------------------------------- | 1597| 201 | The application does not have permission to call the interface. | 1598| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1599| 16000001 | The specified ability does not exist. | 1600| 16000002 | Incorrect ability type. | 1601| 16000004 | Failed to start the invisible ability. | 1602| 16000005 | The specified process does not have the permission. | 1603| 16000006 | Cross-user operations are not allowed. | 1604| 16000008 | The crowdtesting application expires. | 1605| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1606| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 1607| 16000011 | The context does not exist. | 1608| 16000012 | The application is controlled. | 1609| 16000013 | The application is controlled by EDM. | 1610| 16000050 | Internal error. | 1611| 16000053 | The ability is not on the top of the UI. | 1612| 16000055 | Installation-free timed out. | 1613| 16200001 | The caller has been released. | 1614 1615**Example** 1616 1617```ts 1618import { UIAbility, Want, dialogRequest } from '@kit.AbilityKit'; 1619import { BusinessError } from '@kit.BasicServicesKit'; 1620 1621export default class EntryAbility extends UIAbility { 1622 onForeground() { 1623 let want: Want = { 1624 bundleName: 'com.example.myapplication', 1625 abilityName: 'AuthAccountServiceExtension' 1626 }; 1627 1628 try { 1629 this.context.requestDialogService(want) 1630 .then((result: dialogRequest.RequestResult) => { 1631 // Carry out normal service processing. 1632 console.info(`requestDialogService succeed, result = ${JSON.stringify(result)}`); 1633 }) 1634 .catch((err: BusinessError) => { 1635 // Process service logic errors. 1636 console.error(`requestDialogService failed, code is ${err.code}, message is ${err.message}`); 1637 }); 1638 } catch (err) { 1639 // Process input parameter errors. 1640 let code = (err as BusinessError).code; 1641 let message = (err as BusinessError).message; 1642 console.error(`requestDialogService failed, code is ${code}, message is ${message}`); 1643 } 1644 } 1645} 1646``` 1647 1648## UIAbilityContext.reportDrawnCompleted<sup>10+</sup> 1649 1650reportDrawnCompleted(callback: AsyncCallback\<void>): void 1651 1652Reports an event indicating that page loading is complete (**loadContent** is successfully called). This API uses an asynchronous callback to return the result. 1653 1654**Atomic service API**: This API can be used in atomic services since API version 11. 1655 1656**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1657 1658**Parameters** 1659 1660| Name| Type| Mandatory| Description| 1661| -------- | -------- | -------- | -------- | 1662| callback | AsyncCallback<void> | Yes| Callback used to report that page loading is complete.| 1663 1664**Error codes** 1665 1666For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1667 1668| ID| Error Message| 1669| ------- | -------------------------------- | 1670| 16000011 | The context does not exist. | 1671| 16000050 | Internal error. | 1672 1673**Example** 1674 1675```ts 1676import { UIAbility } from '@kit.AbilityKit'; 1677import { window } from '@kit.ArkUI'; 1678import { BusinessError } from '@kit.BasicServicesKit'; 1679 1680export default class EntryAbility extends UIAbility { 1681 onWindowStageCreate(windowStage: window.WindowStage) { 1682 windowStage.loadContent('pages/Index', (err, data) => { 1683 if (err.code) { 1684 return; 1685 } 1686 1687 try { 1688 this.context.reportDrawnCompleted((err) => { 1689 if (err.code) { 1690 // Process service logic errors. 1691 console.error(`reportDrawnCompleted failed, code is ${err.code}, message is ${err.message}`); 1692 return; 1693 } 1694 // Carry out normal service processing. 1695 console.info('reportDrawnCompleted succeed'); 1696 }); 1697 } catch (err) { 1698 // Capture the synchronization parameter error. 1699 let code = (err as BusinessError).code; 1700 let message = (err as BusinessError).message; 1701 console.error(`reportDrawnCompleted failed, code is ${code}, message is ${message}`); 1702 } 1703 }); 1704 console.log("MainAbility onWindowStageCreate"); 1705 } 1706}; 1707``` 1708 1709## UIAbilityContext.startAbilityByType<sup>11+</sup> 1710 1711startAbilityByType(type: string, wantParam: Record<string, Object>, 1712 abilityStartCallback: AbilityStartCallback, callback: AsyncCallback\<void>) : void 1713 1714Implicitly 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. 1715 1716**Atomic service API**: This API can be used in atomic services since API version 11. 1717 1718**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1719 1720**Parameters** 1721 1722| Name| Type| Mandatory| Description| 1723| -------- | -------- | -------- | -------- | 1724| 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).| 1725| wantParam | Record<string, Object> | Yes| Extended parameter.| 1726| abilityStartCallback | [AbilityStartCallback](js-apis-inner-application-abilityStartCallback.md) | Yes| Callback used to return the result.| 1727| callback | AsyncCallback<void> | Yes| Callback used to return the result.| 1728 1729**Error codes** 1730 1731For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1732 1733| ID| Error Message| 1734| ------- | -------------------------------- | 1735| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1736| 16000050 | Internal error. | 1737 1738**Example** 1739 1740```ts 1741import { UIAbility, common } from '@kit.AbilityKit'; 1742 1743export default class EntryAbility extends UIAbility { 1744 onForeground() { 1745 let wantParam: Record<string, Object> = { 1746 'time': '2023-10-23 20:45' 1747 }; 1748 let abilityStartCallback: common.AbilityStartCallback = { 1749 onError: (code: number, name: string, message: string) => { 1750 console.log(`code:` + code + `name:` + name + `message:` + message); 1751 }, 1752 onResult: (abilityResult: common.AbilityResult) => { 1753 console.log(`resultCode:` + abilityResult.resultCode + `bundleName:` + abilityResult.want?.bundleName); 1754 } 1755 }; 1756 1757 this.context.startAbilityByType("photoEditor", wantParam, abilityStartCallback, (err) => { 1758 if (err) { 1759 console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`); 1760 } else { 1761 console.log(`success`); 1762 } 1763 }); 1764 } 1765} 1766``` 1767 1768## UIAbilityContext.startAbilityByType<sup>11+</sup> 1769 1770startAbilityByType(type: string, wantParam: Record<string, Object>, 1771 abilityStartCallback: AbilityStartCallback) : Promise\<void> 1772 1773Implicitly 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. 1774 1775**Atomic service API**: This API can be used in atomic services since API version 11. 1776 1777**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1778 1779**Parameters** 1780 1781| Name| Type| Mandatory| Description| 1782| -------- | -------- | -------- | -------- | 1783| 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).| 1784| wantParam | Record<string, Object> | Yes| Extended parameter.| 1785| abilityStartCallback | [AbilityStartCallback](js-apis-inner-application-abilityStartCallback.md) | Yes| Callback used to return the result.| 1786 1787**Return value** 1788 1789| Type| Description| 1790| -------- | -------- | 1791| Promise<void> | Promise that returns no value.| 1792 1793**Error codes** 1794 1795For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1796 1797| ID| Error Message| 1798| ------- | -------------------------------- | 1799| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1800| 16000050 | Internal error. | 1801 1802**Example** 1803 1804```ts 1805import { UIAbility, common } from '@kit.AbilityKit'; 1806import { BusinessError } from '@kit.BasicServicesKit'; 1807 1808export default class EntryAbility extends UIAbility { 1809 onForeground() { 1810 let wantParam: Record<string, Object> = { 1811 'time': '2023-10-23 20:45' 1812 }; 1813 let abilityStartCallback: common.AbilityStartCallback = { 1814 onError: (code: number, name: string, message: string) => { 1815 console.log(`code:` + code + `name:` + name + `message:` + message); 1816 }, 1817 onResult: (abilityResult: common.AbilityResult) => { 1818 console.log(`resultCode:` + abilityResult.resultCode + `bundleName:` + abilityResult.want?.bundleName); 1819 } 1820 }; 1821 1822 this.context.startAbilityByType("photoEditor", wantParam, abilityStartCallback).then(() => { 1823 console.log(`startAbilityByType success`); 1824 }).catch((err: BusinessError) => { 1825 console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`); 1826 }); 1827 } 1828} 1829``` 1830 1831## UIAbilityContext.showAbility<sup>12+</sup> 1832 1833showAbility(): Promise\<void> 1834 1835Shows 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. 1836 1837Before calling this API, ensure that the application has been added to the status bar. 1838 1839**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1840 1841**Return value** 1842 1843| Type| Description| 1844| -------- | -------- | 1845| Promise<void> | Promise that returns no value.| 1846 1847**Error codes** 1848 1849For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1850 1851| ID| Error Message| 1852| ------- | -------------------------------- | 1853| 801 | Capability not support. | 1854| 16000050 | Internal error. | 1855| 16000067 | The StartOptions check failed. | 1856 1857**Example** 1858 1859```ts 1860// Index.ets 1861import { common } from '@kit.AbilityKit'; 1862import { BusinessError } from '@kit.BasicServicesKit'; 1863 1864@Entry 1865@Component 1866struct Index { 1867 @State showAbility: string = 'showAbility' 1868 1869 build() { 1870 Row() { 1871 Column() { 1872 Text(this.showAbility) 1873 .fontSize(30) 1874 .fontWeight(FontWeight.Bold) 1875 .onClick(() => { 1876 let context = getContext(this) as common.UIAbilityContext; 1877 1878 context.showAbility().then(() => { 1879 console.log(`showAbility success`); 1880 }).catch((err: BusinessError) => { 1881 console.error(`showAbility fail, err: ${JSON.stringify(err)}`); 1882 }); 1883 }); 1884 } 1885 .width('100%') 1886 } 1887 .height('100%') 1888 } 1889} 1890``` 1891```ts 1892// EntryAbility.ts 1893import { UIAbility, Want, StartOptions, contextConstant } from '@kit.AbilityKit'; 1894import { BusinessError } from '@kit.BasicServicesKit'; 1895 1896export default class EntryAbility extends UIAbility { 1897 onForeground() { 1898 let want: Want = { 1899 deviceId: '', 1900 bundleName: 'com.example.myapplication', 1901 abilityName: 'EntryAbility' 1902 }; 1903 let options: StartOptions = { 1904 displayId: 0, 1905 processMode: contextConstant.ProcessMode.NEW_PROCESS_ATTACH_TO_STATUS_BAR_ITEM 1906 }; 1907 1908 try { 1909 this.context.startAbility(want, options, (err: BusinessError) => { 1910 if (err.code) { 1911 // Process service logic errors. 1912 console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); 1913 return; 1914 } 1915 // Carry out normal service processing. 1916 console.info('startAbility succeed'); 1917 }); 1918 } catch (err) { 1919 // Process input parameter errors. 1920 let code = (err as BusinessError).code; 1921 let message = (err as BusinessError).message; 1922 console.error(`startAbility failed, code is ${code}, message is ${message}`); 1923 } 1924 } 1925} 1926``` 1927 1928## UIAbilityContext.hideAbility<sup>12+</sup> 1929 1930hideAbility(): Promise\<void> 1931 1932Hides 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. 1933 1934Before calling this API, ensure that the application has been added to the status bar. 1935 1936**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1937 1938**Return value** 1939 1940| Type| Description| 1941| -------- | -------- | 1942| Promise<void> | Promise that returns no value.| 1943 1944**Error codes** 1945 1946For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1947 1948| ID| Error Message| 1949| ------- | -------------------------------- | 1950| 801 | Capability not support. | 1951| 16000050 | Internal error. | 1952| 16000067 | The StartOptions check failed. | 1953 1954**Example** 1955 1956```ts 1957// Index.ets 1958import { common } from '@kit.AbilityKit'; 1959import { BusinessError } from '@kit.BasicServicesKit'; 1960 1961@Entry 1962@Component 1963struct Index { 1964 @State hideAbility: string = 'hideAbility' 1965 1966 build() { 1967 Row() { 1968 Column() { 1969 Text(this.hideAbility) 1970 .fontSize(30) 1971 .fontWeight(FontWeight.Bold) 1972 .onClick(() => { 1973 let context = getContext(this) as common.UIAbilityContext; 1974 1975 context.hideAbility().then(() => { 1976 console.log(`hideAbility success`); 1977 }).catch((err: BusinessError) => { 1978 console.error(`hideAbility fail, err: ${JSON.stringify(err)}`); 1979 }); 1980 }); 1981 } 1982 .width('100%') 1983 } 1984 .height('100%') 1985 } 1986} 1987``` 1988```ts 1989// EntryAbility.ts 1990import { UIAbility, Want, StartOptions, contextConstant } from '@kit.AbilityKit'; 1991import { BusinessError } from '@kit.BasicServicesKit'; 1992 1993export default class EntryAbility extends UIAbility { 1994 onForeground() { 1995 let want: Want = { 1996 deviceId: '', 1997 bundleName: 'com.example.myapplication', 1998 abilityName: 'EntryAbility' 1999 }; 2000 let options: StartOptions = { 2001 displayId: 0, 2002 processMode: contextConstant.ProcessMode.NEW_PROCESS_ATTACH_TO_STATUS_BAR_ITEM 2003 }; 2004 2005 try { 2006 this.context.startAbility(want, options, (err: BusinessError) => { 2007 if (err.code) { 2008 // Process service logic errors. 2009 console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); 2010 return; 2011 } 2012 // Carry out normal service processing. 2013 console.info('startAbility succeed'); 2014 }); 2015 } catch (err) { 2016 // Process input parameter errors. 2017 let code = (err as BusinessError).code; 2018 let message = (err as BusinessError).message; 2019 console.error(`startAbility failed, code is ${code}, message is ${message}`); 2020 } 2021 } 2022} 2023``` 2024 2025## UIAbilityContext.moveAbilityToBackground<sup>12+<sup> 2026moveAbilityToBackground(): Promise\<void> 2027 2028Moves 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--><!--RP1End--> 2029 2030**Atomic service API**: This API can be used in atomic services since API version 12. 2031 2032**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2033 2034**Return value** 2035 2036| Type| Description| 2037| -------- | -------- | 2038| Promise<void> | Promise that returns no value.| 2039 2040**Error codes** 2041 2042For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 2043 2044| ID| Error Message| 2045| ------- | -------------------------------- | 2046| 16000011 | The context does not exist. | 2047| 16000050 | Internal error. | 2048| 16000061 | Operation not supported. | 2049| 16000065 | The API can be called only when the ability is running in the foreground. | 2050| 16000066 | An ability cannot switch to the foreground or background in Wukong mode. | 2051 2052**Example** 2053 2054```ts 2055import { common } from '@kit.AbilityKit'; 2056import { BusinessError } from '@kit.BasicServicesKit'; 2057 2058@Entry 2059@Component 2060struct Index { 2061 @State moveAbilityToBackground: string = 'Move To Background' 2062 2063 build() { 2064 Row() { 2065 Column() { 2066 Text(this.moveAbilityToBackground) 2067 .fontSize(30) 2068 .fontWeight(FontWeight.Bold) 2069 .onClick(() => { 2070 let context = getContext(this) as common.UIAbilityContext; 2071 2072 context.moveAbilityToBackground().then(() => { 2073 console.log(`moveAbilityToBackground success.`); 2074 }).catch((err: BusinessError) => { 2075 console.log(`moveAbilityToBackground error: ${JSON.stringify(err)}.`); 2076 }); 2077 }); 2078 } 2079 .width('100%') 2080 } 2081 .height('100%') 2082 } 2083} 2084``` 2085 2086## UIAbilityContext.openAtomicService<sup>12+<sup> 2087 2088openAtomicService(appId: string, options?: AtomicServiceOptions): Promise<AbilityResult> 2089 2090Starts 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. 2091 2092The following situations may be possible for a started EmbeddableUIAbility: 2093 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the EmbeddableUIAbility. The result is returned to the caller. 2094 - If an exception occurs, for example, the EmbeddableUIAbility is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 2095 - 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. 2096 2097> **NOTE** 2098> 2099> 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). 2100 2101**Atomic service API**: This API can be used in atomic services since API version 12. 2102 2103**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2104 2105**Parameters** 2106 2107| Name| Type| Mandatory| Description| 2108| -------- | -------- | -------- | -------- | 2109| appId | string | Yes| Unique ID of the application, which is allocated by the cloud.| 2110| options | [AtomicServiceOptions](js-apis-app-ability-atomicServiceOptions.md) | No| Parameter carried in the request for starting the atomic service in jump-out mode.| 2111 2112 2113**Return value** 2114 2115| Type| Description| 2116| -------- | -------- | 2117| 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.| 2118 2119**Error codes** 2120 2121For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2122 2123| ID| Error Message| 2124| ------- | -------------------------------- | 2125| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2126| 16000002 | Incorrect ability type. | 2127| 16000003 | The specified ID does not exist. | 2128| 16000004 | Failed to start the invisible ability. | 2129| 16000011 | The context does not exist. | 2130| 16000012 | The application is controlled. | 2131| 16000050 | Internal error. | 2132| 16000053 | The ability is not on the top of the UI. | 2133| 16000055 | Installation-free timed out. | 2134| 16200001 | The caller has been released. | 2135 2136**Example** 2137 2138```ts 2139import { UIAbility, common, AtomicServiceOptions } from '@kit.AbilityKit'; 2140import { BusinessError } from '@kit.BasicServicesKit'; 2141 2142export default class EntryAbility extends UIAbility { 2143 onForeground() { 2144 let appId: string = '6918661953712445909'; 2145 let options: AtomicServiceOptions = { 2146 displayId: 0 2147 }; 2148 2149 try { 2150 this.context.openAtomicService(appId, options) 2151 .then((result: common.AbilityResult) => { 2152 // Carry out normal service processing. 2153 console.info('openAtomicService succeed'); 2154 }) 2155 .catch((err: BusinessError) => { 2156 // Process service logic errors. 2157 console.error(`openAtomicService failed, code is ${err.code}, message is ${err.message}`); 2158 }); 2159 } catch (err) { 2160 // Process input parameter errors. 2161 let code = (err as BusinessError).code; 2162 let message = (err as BusinessError).message; 2163 console.error(`openAtomicService failed, code is ${code}, message is ${message}`); 2164 } 2165 } 2166} 2167``` 2168 2169## UIAbilityContext.openLink<sup>12+<sup> 2170 2171openLink(link: string, options?: OpenLinkOptions, callback?: AsyncCallback<AbilityResult>): Promise<void> 2172 2173Starts a UIAbility through App Linking. This API uses a promise to return the result. It can be called only by the main thread. 2174 2175A 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: 2176- The **actions** field contains **ohos.want.action.viewData**. 2177- The **entities** field contains **entity.system.browsable**. 2178- The **uris** field contains elements whose **scheme** is **https** and **domainVerify** is **true**. 2179 2180If 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). 2181If 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. 2182 2183> **NOTE** 2184> 2185> 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). 2186 2187**Atomic service API**: This API can be used in atomic services since API version 12. 2188 2189**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2190 2191**Parameters** 2192 2193| Name| Type| Mandatory| Description| 2194| -------- | -------- | -------- | -------- | 2195| link | string | Yes| URL to open, which must be in the standard format.| 2196| options | [OpenLinkOptions](js-apis-app-ability-openLinkOptions.md) | No| Options of the URL.| 2197| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | No| Callback used to return the result.| 2198 2199**Return value** 2200 2201| Type| Description| 2202| -------- | -------- | 2203| Promise<void> | Promise that returns no value.| 2204 2205**Error codes** 2206 2207For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2208 2209| ID| Error Message| 2210| ------- | -------------------------------- | 2211| 201 | The application does not have permission to call the interface. | 2212| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2213| 16000001 | The specified ability does not exist. | 2214| 16000002 | Incorrect ability type. | 2215| 16000004 | Failed to start the invisible ability. | 2216| 16000005 | The specified process does not have the permission. | 2217| 16000006 | Cross-user operations are not allowed. | 2218| 16000008 | The crowdtesting application expires. | 2219| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2220| 16000010 | The call with the continuation flag is forbidden. | 2221| 16000011 | The context does not exist. | 2222| 16000012 | The application is controlled. | 2223| 16000013 | The application is controlled by EDM. | 2224| 16000019 | No matching ability is found. | 2225| 16200001 | The caller has been released. | 2226| 16000053 | The ability is not on the top of the UI. | 2227 2228**Example** 2229 2230```ts 2231import { common, OpenLinkOptions } from '@kit.AbilityKit'; 2232import { hilog } from '@kit.PerformanceAnalysisKit'; 2233import { BusinessError } from '@kit.BasicServicesKit'; 2234 2235const DOMAIN = 0xeeee; 2236const TAG: string = '[openLinkDemo]'; 2237 2238@Entry 2239@Component 2240struct Index { 2241 build() { 2242 RelativeContainer() { 2243 Button("Call StartAbilityForResult") 2244 .onClick(() => { 2245 let context = getContext(this) as common.UIAbilityContext; 2246 let link: string = 'https://www.example.com'; 2247 let openLinkOptions: OpenLinkOptions = { 2248 appLinkingOnly: true, 2249 parameters: { demo_key: 'demo_value' } 2250 }; 2251 2252 try { 2253 context.openLink( 2254 link, 2255 openLinkOptions, 2256 (err, result) => { 2257 hilog.error(DOMAIN, TAG, `openLink callback error.code: ${JSON.stringify(err)}`); 2258 hilog.info(DOMAIN, TAG, `openLink callback result: ${JSON.stringify(result.resultCode)}`); 2259 hilog.info(DOMAIN, TAG, `openLink callback result data: ${JSON.stringify(result.want)}`); 2260 } 2261 ).then(() => { 2262 hilog.info(DOMAIN, TAG, `open link success.`); 2263 }).catch((err: BusinessError) => { 2264 hilog.error(DOMAIN, TAG, `open link failed, errCode ${JSON.stringify(err.code)}`); 2265 }); 2266 } 2267 catch (e) { 2268 hilog.error(DOMAIN, TAG, `exception occured, errCode ${JSON.stringify(e.code)}`); 2269 } 2270 }) 2271 } 2272 .height('100%') 2273 .width('100%') 2274 } 2275} 2276``` 2277 2278## UIAbilityContext.backToCallerAbilityWithResult<sup>12+<sup> 2279 2280backToCallerAbilityWithResult(abilityResult: AbilityResult, requestCode: string): Promise<void> 2281 2282Returns 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. This API uses a promise to return the result. 2283 2284**Atomic service API**: This API can be used in atomic services since API version 12. 2285 2286**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2287 2288**Parameters** 2289 2290| Name| Type| Mandatory| Description| 2291| -------- | -------- | -------- | -------- | 2292| abilityResult | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Result returned to the caller.| 2293| 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**.| 2294 2295**Return value** 2296 2297| Type| Description| 2298| -------- | -------- | 2299| Promise<void> | Promise that returns no value.| 2300 2301**Error codes** 2302 2303For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2304 2305| ID| Error Message| 2306| ------- | -------------------------------- | 2307| 201 | The application does not have permission to call the interface. | 2308| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2309| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2310| 16000011 | The context does not exist. | 2311| 16000050 | Internal error. | 2312| 16000074 | The caller does not exist. | 2313| 16000075 | Not support back to caller. | 2314 2315**Example** 2316The caller uses **startAbilityForResult** to start an ability, and the target ability calls **backToCallerAbilityWithResult** to return the result to the caller. 2317 2318```ts 2319// Caller 2320// index.ets 2321import { common, Want } from '@kit.AbilityKit'; 2322import { BusinessError } from '@ohos.base'; 2323import { hilog } from '@kit.PerformanceAnalysisKit'; 2324 2325@Entry 2326@Component 2327struct Index { 2328 @State message: string = 'Hello World'; 2329 2330 build() { 2331 Row() { 2332 Column() { 2333 Text(this.message) 2334 .fontSize(30) 2335 .fontWeight(FontWeight.Bold) 2336 2337 Button("Call StartAbilityForResult") 2338 .onClick(() => { 2339 let context: common.UIAbilityContext = getContext() as common.UIAbilityContext; 2340 let want: Want = { 2341 bundleName: 'com.example.demo2', 2342 abilityName: 'EntryAbility' 2343 }; 2344 2345 try { 2346 // Use startAbilityForResult to start the target ability. 2347 context.startAbilityForResult(want, (err: BusinessError, result: common.AbilityResult) => { 2348 if (err.code) { 2349 // Process service logic errors. 2350 hilog.error(0x0000, 'testTag', `startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); 2351 this.message = `startAbilityForResult failed: code is ${err.code}, message is ${err.message}` 2352 return; 2353 } 2354 // Carry out normal service processing. 2355 hilog.info(0x0000, 'testTag', `startAbilityForResult succeed`); 2356 hilog.info(0x0000, 'testTag', `AbilityResult is ${JSON.stringify(result)}`); 2357 this.message = `AbilityResult.resultCode: ${JSON.stringify(result.resultCode)}` 2358 }); 2359 } catch (err) { 2360 // Process input parameter errors. 2361 let code = (err as BusinessError).code; 2362 let message = (err as BusinessError).message; 2363 hilog.error(0x0000, 'testTag', `startAbilityForResult failed, code is ${code}, message is ${message}`); 2364 this.message = `startAbilityForResult failed, code is ${code}, message is ${message}`; 2365 } 2366 }) 2367 } 2368 .width('100%') 2369 } 2370 .height('100%') 2371 } 2372} 2373``` 2374 2375```ts 2376// Target ability 2377// EntryAbility.ets 2378import { AbilityConstant, common, UIAbility, Want, wantConstant } from '@kit.AbilityKit'; 2379import { hilog } from '@kit.PerformanceAnalysisKit'; 2380import { BusinessError } from '@kit.BasicServicesKit'; 2381 2382export default class EntryAbility extends UIAbility { 2383 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2384 // Obtain the CALLER_REQUEST_CODE of the caller from want and save it. 2385 let callerRequestCode: string = want?.parameters?.[wantConstant.Params.CALLER_REQUEST_CODE] as string; 2386 AppStorage.setOrCreate<string>("callerRequestCode", callerRequestCode) 2387 } 2388 2389 onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2390 let callerRequestCode: string = want?.parameters?.[wantConstant.Params.CALLER_REQUEST_CODE] as string; 2391 AppStorage.setOrCreate<string>("callerRequestCode", callerRequestCode) 2392 } 2393 2394 onForeground(): void { 2395 // Obtain the saved CALLER_REQUEST_CODE. 2396 let callerRequestCode: string = AppStorage.get<string>("callerRequestCode") as string; 2397 hilog.info(0x0000, 'testTag', `callerRequestCode is ${callerRequestCode}`); 2398 let want: Want = {}; 2399 let resultCode = 100; 2400 let abilityResult: common.AbilityResult = { 2401 want, 2402 resultCode 2403 }; 2404 try { 2405 // Return the result to the caller. 2406 this.context.backToCallerAbilityWithResult(abilityResult, callerRequestCode) 2407 .then(() => { 2408 // Carry out normal service processing. 2409 hilog.info(0x0000, 'testTag', 'backToCallerAbilityWithResult succeed'); 2410 }) 2411 .catch((err: BusinessError) => { 2412 // Process service logic errors. 2413 hilog.error(0x0000, 'testTag', `backToCallerAbilityWithResult failed, code is ${err.code}, message is ${err.message}`); 2414 }); 2415 } catch (err) { 2416 // Capture the synchronization parameter error. 2417 let code = (err as BusinessError).code; 2418 let message = (err as BusinessError).message; 2419 hilog.error(0x0000, 'testTag', `backToCallerAbilityWithResult failed, code is ${code}, message is ${message}`); 2420 } 2421 } 2422} 2423``` 2424 2425## UIAbilityContext.setRestoreEnabled<sup>14+</sup> 2426 2427setRestoreEnabled(enabled: boolean): void 2428 2429Sets whether to enable backup and restore for this UIAbility. 2430 2431**Atomic service API**: This API can be used in atomic services since API version 14. 2432 2433**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2434 2435**Parameters** 2436 2437| Name| Type| Mandatory| Description| 2438| -------- | -------- | -------- | -------- | 2439| enabled | boolean | Yes| Whether to enable backup and restore. The value **true** means to enable backup and restore, and **false** means the opposite.| 2440 2441**Error codes** 2442 2443For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2444 2445| ID| Error Message| 2446| ------- | -------------------------------- | 2447| 401 | If the input parameter is not valid parameter. | 2448| 16000011 | The context does not exist. | 2449 2450**Example** 2451 2452```ts 2453import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; 2454import { BusinessError } from '@kit.BasicServicesKit'; 2455 2456export default class EntryAbility extends UIAbility { 2457 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2458 let enabled = true; 2459 try { 2460 this.context.setRestoreEnabled(enabled); 2461 } catch (paramError) { 2462 let code = (paramError as BusinessError).code; 2463 let message = (paramError as BusinessError).message; 2464 console.error(`setRestoreEnabled failed, err code: ${code}, err msg: ${message}`); 2465 } 2466 } 2467} 2468``` 2469 2470## UIAbilityContext.startUIServiceExtensionAbility<sup>14+<sup> 2471 2472startUIServiceExtensionAbility(want: Want): Promise<void> 2473 2474Starts a UIServiceExtensionAbility. This API uses a promise to return the result. 2475 2476> **NOTE** 2477> 2478> 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). 2479 2480**Atomic service API**: This API can be used in atomic services since API version 14. 2481 2482**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2483 2484**Parameters** 2485 2486| Name | Type | Mandatory| Description | 2487| -------- | --------------------------------------- | ---- | ------------------------ | 2488| want | [Want](js-apis-app-ability-want.md) | Yes| Want information required for startup.| 2489 2490**Return value** 2491 2492| Type | Description | 2493| ------------------- | -------------------------------------- | 2494| Promise<void> | Promise that returns no value.| 2495 2496**Error codes** 2497 2498For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2499 2500| ID| Error Message | 2501| -------- | ----------------------------------------------------------------------------------------------------------- | 2502| 201 | The application does not have permission to call the interface. | 2503| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2504| 801 | Capability not supported. | 2505| 16000001 | The specified ability does not exist. | 2506| 16000002 | Incorrect ability type. | 2507| 16000004 | Failed to start the invisible ability. | 2508| 16000005 | The specified process does not have the permission. | 2509| 16000008 | The crowdtesting application expires. | 2510| 16000011 | The context does not exist. | 2511| 16000012 | The application is controlled. | 2512| 16000013 | The application is controlled by EDM. | 2513| 16000019 | No matching ability is found. | 2514| 16000050 | Internal error. | 2515| 16200001 | The caller has been released. | 2516 2517**Example** 2518 2519```ts 2520import { common, Want } from '@kit.AbilityKit'; 2521import { BusinessError } from '@kit.BasicServicesKit'; 2522 2523@Entry 2524@Component 2525struct Index { 2526 build() { 2527 Column() { 2528 Row() { 2529 // Create a Start button. 2530 Button('start ability') 2531 .enabled(true) 2532 .onClick(() => { 2533 let context = getContext(this) as common.UIAbilityContext; 2534 let startWant: Want = { 2535 bundleName: 'com.acts.uiserviceextensionability', 2536 abilityName: 'UiServiceExtAbility', 2537 }; 2538 try { 2539 // Start the UIServiceExtensionAbility. 2540 context.startUIServiceExtensionAbility(startWant).then(() => { 2541 console.log('startUIServiceExtensionAbility success'); 2542 }).catch((error: BusinessError) => { 2543 console.log('startUIServiceExtensionAbility error', JSON.stringify(error)); 2544 }) 2545 } catch (err) { 2546 console.log('startUIServiceExtensionAbility failed', JSON.stringify(err)); 2547 } 2548 }) 2549 } 2550 } 2551 } 2552} 2553``` 2554 2555## UIAbilityContext.connectUIServiceExtensionAbility<sup>14+<sup> 2556 2557connectUIServiceExtensionAbility(want: Want, callback: UIServiceExtensionConnectCallback) : Promise<UIServiceProxy> 2558 2559Connects to a UIServiceExtensionAbility. This API uses a promise to return the result. 2560 2561> **NOTE** 2562> 2563> 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). 2564> 2565 2566**Atomic service API**: This API can be used in atomic services since API version 14. 2567 2568**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2569 2570**Parameters** 2571 2572| Name| Type| Mandatory| Description | 2573| ------ | ---- | ---- | ---- | 2574| want |[Want](js-apis-app-ability-want.md) | Yes | Want information required for connection.| 2575| callback | [UIServiceExtensionConnectCallback](js-apis-inner-application-uiServiceExtensionconnectcallback.md) | Yes | Callback for connecting to the UIServiceExtensionAbility.| 2576 2577**Return value** 2578 2579| Type | Description | 2580| ------------------- | -------------------------------------- | 2581| Promise<UIServiceProxy> | Promise used to return a [UIServiceProxy](js-apis-inner-application-uiserviceproxy.md) object.| 2582 2583**Error codes** 2584 2585For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2586 2587| ID| Error Message | 2588| -------- | ----------------------------------------------------------------------------------- | 2589| 201 | The application does not have permission to call the interface. | 2590| 801 | Capability not supported. | 2591| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2592| 16000001 | The specified ability does not exist. | 2593| 16000002 | Incorrect ability type. | 2594| 16000004 | Failed to start the invisible ability. | 2595| 16000005 | The specified process does not have the permission. | 2596| 16000008 | The crowdtesting application expires. | 2597| 16000011 | The context does not exist. | 2598| 16000013 | The EDM prohibits the application from launching. | 2599| 16000050 | Internal error. | 2600| 16000055 | Installation-free timed out. | 2601 2602**Example** 2603 2604```ts 2605import { common, Want } from '@kit.AbilityKit'; 2606import { BusinessError } from '@kit.BasicServicesKit'; 2607 2608const TAG: string = '[Extension] '; 2609 2610@Entry 2611@Component 2612struct UIServiceExtensionAbility { 2613 dataCallBack : common.UIServiceExtensionConnectCallback = { 2614 // Receive data 2615 onData: (data: Record<string, Object>) => { 2616 console.log(`dataCallBack received data`, JSON.stringify(data)); 2617 }, 2618 // Disconnect from the UIServiceExtensionAbility. 2619 onDisconnect: () => { 2620 console.log(`dataCallBack onDisconnect`); 2621 } 2622 } 2623 2624 async myConnect() { 2625 // Obtain the context. 2626 let context = getContext(this) as common.UIAbilityContext; 2627 let startWant: Want = { 2628 deviceId: '', 2629 bundleName: 'com.example.myapplication', 2630 abilityName: 'UiServiceExtAbility' 2631 }; 2632 2633 try { 2634 // Connect to the UIServiceExtensionAbility. 2635 context.connectUIServiceExtensionAbility(startWant, this.dataCallBack) 2636 .then((proxy: common.UIServiceProxy) => { 2637 console.log(TAG + `try to connectUIServiceExtensionAbility`, JSON.stringify(proxy)); 2638 }).catch((err: Error) => { 2639 let code = (err as BusinessError).code; 2640 let message = (err as BusinessError).message; 2641 console.log(TAG + `connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); 2642 }); 2643 } catch (err) { 2644 let code = (err as BusinessError).code; 2645 let message = (err as BusinessError).message; 2646 console.log(TAG + `connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); 2647 }; 2648 } 2649 2650 build() { 2651 RelativeContainer() { 2652 // Create a Connect button. 2653 Button('connectServiceExtensionAbility', { type: ButtonType.Capsule, stateEffect: true }) 2654 .alignRules({ 2655 center: { anchor: '__container__', align: VerticalAlign.Center }, 2656 middle: { anchor: '__container__', align: HorizontalAlign.Center } 2657 }) 2658 .onClick(() => { 2659 this.myConnect() 2660 }); 2661 } 2662 .height('100%') 2663 .width('100%') 2664 } 2665} 2666``` 2667 2668## UIAbilityContext.disconnectUIServiceExtensionAbility<sup>14+<sup> 2669 2670disconnectUIServiceExtensionAbility(proxy: UIServiceProxy): Promise<void> 2671 2672Disconnects from a UIServiceExtensionAbility. This API uses a promise to return the result. 2673 2674> **NOTE** 2675> 2676> 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). 2677> 2678 2679**Atomic service API**: This API can be used in atomic services since API version 14. 2680 2681**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2682 2683**Parameters** 2684 2685| Name| Type| Mandatory| Description | 2686| ------ | ---- | ---- | -------------------- | 2687| proxy | [UIServiceProxy](js-apis-inner-application-uiserviceproxy.md) | Yes| Proxy returned after [connectUIServiceExtensionAbility](#uiabilitycontextconnectuiserviceextensionability13) is called.| 2688 2689**Return value** 2690 2691| Type | Description | 2692| ------------------- | -------------------------------------- | 2693| Promise<void> | Promise that returns no value.| 2694 2695**Error codes** 2696 2697For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2698 2699| ID| Error Message | 2700| -------- | ------------------------------------------------------------------------------------------- | 2701| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2702| 16000011 | The context does not exist. | 2703| 16000050 | Internal error. | 2704 2705**Example** 2706 2707```ts 2708import { common } from '@kit.AbilityKit'; 2709import { BusinessError } from '@kit.BasicServicesKit'; 2710 2711const TAG: string = '[Extension] '; 2712 2713@Entry 2714@Component 2715struct UIServiceExtensionAbility { 2716 comProxy: common.UIServiceProxy | null = null; 2717 2718 build() { 2719 Scroll() { 2720 Column() { 2721 // Create a Disconnect button. 2722 Button('disconnectUIServiceExtensionAbility', { type: ButtonType.Capsule, stateEffect: true }) 2723 .margin({ 2724 top: 5, 2725 left: 10, 2726 right: 10, 2727 bottom: 5 2728 }) 2729 .alignRules({ 2730 center: { anchor: '__container__', align: VerticalAlign.Center }, 2731 middle: { anchor: '__container__', align: HorizontalAlign.Center } 2732 }) 2733 .onClick(() => { 2734 this.myDisconnectUIServiceExtensionAbility() 2735 }); 2736 } 2737 .width('100%') 2738 } 2739 .height('100%') 2740 } 2741 2742 myDisconnectUIServiceExtensionAbility() { 2743 let context = getContext(this) as common.UIAbilityContext; 2744 2745 try { 2746 // Disconnect from the UIServiceExtensionAbility. 2747 context.disconnectUIServiceExtensionAbility(this.comProxy) 2748 .then(() => { 2749 console.log(TAG + `disconnectUIServiceExtensionAbility succeed ${this.comProxy}}`); 2750 }).catch((err: Error) => { 2751 let code = (err as BusinessError).code; 2752 let message = (err as BusinessError).message; 2753 console.log(TAG + `disconnectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); 2754 }); 2755 } catch (err) { 2756 let code = (err as BusinessError).code; 2757 let message = (err as BusinessError).message; 2758 console.log(TAG + `disconnectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); 2759 } 2760 } 2761} 2762``` 2763 2764## UIAbilityContext.setAbilityInstanceInfo<sup>15+<sup> 2765 2766setAbilityInstanceInfo(label: string, icon: image.PixelMap) : Promise<void> 2767 2768Sets the icon and label for this UIAbility. The icon and label can be displayed in Recents and the shortcut bar. This API uses a promise to return the result. 2769 2770 2771> **NOTE** 2772> 2773> This API is available only for 2-in-1 devices. 2774 2775**Required permissions**: ohos.permission.SET_ABILITY_INSTANCE_INFO 2776 2777**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2778 2779**Parameters** 2780 2781| Name| Type | Mandatory| Description | 2782| ------ | -------------------------------------------------------------- | ---- | -------------------------------------------------- | 2783| label |string | Yes | Label. The label cannot be an empty string, and can contain a maximum of 1024 bytes. | 2784| icon | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | Yes | Icon. The recommended icon size is 512 px * 512 px. | 2785 2786**Return value** 2787 2788| Type | Description | 2789| ------------------- | -------------------------------------- | 2790| Promise<void> | Promise that returns no value.| 2791 2792**Error codes** 2793 2794For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2795 2796| ID| Error Message | 2797| -------- | ----------------------------------------------------------------------------------- | 2798| 201 | The application does not have permission to call the interface. | 2799| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2800| 801 | Capability not supported. | 2801| 16000011 | The context does not exist. | 2802| 16000050 | Internal error. | 2803 2804**Example** 2805 2806```ts 2807import { UIAbility } from '@kit.AbilityKit'; 2808import { image } from '@kit.ImageKit'; 2809import { BusinessError } from '@kit.BasicServicesKit'; 2810import { window } from '@kit.ArkUI'; 2811 2812export default class EntryAbility extends UIAbility { 2813 onWindowStageCreate(windowStage: window.WindowStage): void { 2814 windowStage.loadContent('pages/Index', async (err, data) => { 2815 if (err.code) { 2816 console.error(`loadContent failed, code is ${err.code}`); 2817 return; 2818 } 2819 2820 let newLabel: string = 'instance label'; 2821 let color = new ArrayBuffer(0); 2822 let imagePixelMap: image.PixelMap = await image.createPixelMap(color, { 2823 size: { 2824 height: 100, 2825 width: 100 2826 } 2827 }); 2828 this.context.setAbilityInstanceInfo(newLabel, imagePixelMap) 2829 .then(() => { 2830 console.info('setAbilityInstanceInfo success'); 2831 }).catch((err: BusinessError) => { 2832 console.error(`setAbilityInstanceInfo failed, code is ${err.code}, message is ${err.message}`); 2833 }); 2834 }); 2835 } 2836} 2837``` 2838