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 startupVisibility: contextConstant.StartupVisibility.STARTUP_SHOW 1907 }; 1908 1909 try { 1910 this.context.startAbility(want, options, (err: BusinessError) => { 1911 if (err.code) { 1912 // Process service logic errors. 1913 console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); 1914 return; 1915 } 1916 // Carry out normal service processing. 1917 console.info('startAbility succeed'); 1918 }); 1919 } catch (err) { 1920 // Process input parameter errors. 1921 let code = (err as BusinessError).code; 1922 let message = (err as BusinessError).message; 1923 console.error(`startAbility failed, code is ${code}, message is ${message}`); 1924 } 1925 } 1926} 1927``` 1928 1929## UIAbilityContext.hideAbility<sup>12+</sup> 1930 1931hideAbility(): Promise\<void> 1932 1933Hides 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. 1934 1935Before calling this API, ensure that the application has been added to the status bar. 1936 1937**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1938 1939**Return value** 1940 1941| Type| Description| 1942| -------- | -------- | 1943| Promise<void> | Promise that returns no value.| 1944 1945**Error codes** 1946 1947For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1948 1949| ID| Error Message| 1950| ------- | -------------------------------- | 1951| 801 | Capability not support. | 1952| 16000050 | Internal error. | 1953| 16000067 | The StartOptions check failed. | 1954 1955**Example** 1956 1957```ts 1958// Index.ets 1959import { common } from '@kit.AbilityKit'; 1960import { BusinessError } from '@kit.BasicServicesKit'; 1961 1962@Entry 1963@Component 1964struct Index { 1965 @State hideAbility: string = 'hideAbility' 1966 1967 build() { 1968 Row() { 1969 Column() { 1970 Text(this.hideAbility) 1971 .fontSize(30) 1972 .fontWeight(FontWeight.Bold) 1973 .onClick(() => { 1974 let context = getContext(this) as common.UIAbilityContext; 1975 1976 context.hideAbility().then(() => { 1977 console.log(`hideAbility success`); 1978 }).catch((err: BusinessError) => { 1979 console.error(`hideAbility fail, err: ${JSON.stringify(err)}`); 1980 }); 1981 }); 1982 } 1983 .width('100%') 1984 } 1985 .height('100%') 1986 } 1987} 1988``` 1989```ts 1990// EntryAbility.ts 1991import { UIAbility, Want, StartOptions, contextConstant } from '@kit.AbilityKit'; 1992import { BusinessError } from '@kit.BasicServicesKit'; 1993 1994export default class EntryAbility extends UIAbility { 1995 onForeground() { 1996 let want: Want = { 1997 deviceId: '', 1998 bundleName: 'com.example.myapplication', 1999 abilityName: 'EntryAbility' 2000 }; 2001 let options: StartOptions = { 2002 displayId: 0, 2003 processMode: contextConstant.ProcessMode.NEW_PROCESS_ATTACH_TO_STATUS_BAR_ITEM, 2004 startupVisibility: contextConstant.StartupVisibility.STARTUP_HIDE 2005 }; 2006 2007 try { 2008 this.context.startAbility(want, options, (err: BusinessError) => { 2009 if (err.code) { 2010 // Process service logic errors. 2011 console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); 2012 return; 2013 } 2014 // Carry out normal service processing. 2015 console.info('startAbility succeed'); 2016 }); 2017 } catch (err) { 2018 // Process input parameter errors. 2019 let code = (err as BusinessError).code; 2020 let message = (err as BusinessError).message; 2021 console.error(`startAbility failed, code is ${code}, message is ${message}`); 2022 } 2023 } 2024} 2025``` 2026 2027## UIAbilityContext.moveAbilityToBackground<sup>12+<sup> 2028moveAbilityToBackground(): Promise\<void> 2029 2030Moves 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--> 2031 2032**Atomic service API**: This API can be used in atomic services since API version 12. 2033 2034**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2035 2036**Return value** 2037 2038| Type| Description| 2039| -------- | -------- | 2040| Promise<void> | Promise that returns no value.| 2041 2042**Error codes** 2043 2044For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 2045 2046| ID| Error Message| 2047| ------- | -------------------------------- | 2048| 16000011 | The context does not exist. | 2049| 16000050 | Internal error. | 2050| 16000061 | Operation not supported. | 2051| 16000065 | The API can be called only when the ability is running in the foreground. | 2052| 16000066 | An ability cannot switch to the foreground or background in Wukong mode. | 2053 2054**Example** 2055 2056```ts 2057import { common } from '@kit.AbilityKit'; 2058import { BusinessError } from '@kit.BasicServicesKit'; 2059 2060@Entry 2061@Component 2062struct Index { 2063 @State moveAbilityToBackground: string = 'Move To Background' 2064 2065 build() { 2066 Row() { 2067 Column() { 2068 Text(this.moveAbilityToBackground) 2069 .fontSize(30) 2070 .fontWeight(FontWeight.Bold) 2071 .onClick(() => { 2072 let context = getContext(this) as common.UIAbilityContext; 2073 2074 context.moveAbilityToBackground().then(() => { 2075 console.log(`moveAbilityToBackground success.`); 2076 }).catch((err: BusinessError) => { 2077 console.log(`moveAbilityToBackground error: ${JSON.stringify(err)}.`); 2078 }); 2079 }); 2080 } 2081 .width('100%') 2082 } 2083 .height('100%') 2084 } 2085} 2086``` 2087 2088## UIAbilityContext.openAtomicService<sup>12+<sup> 2089 2090openAtomicService(appId: string, options?: AtomicServiceOptions): Promise<AbilityResult> 2091 2092Starts 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. 2093 2094The following situations may be possible for a started EmbeddableUIAbility: 2095 - Normally, you can call [terminateSelfWithResult](#uiabilitycontextterminateselfwithresult) to terminate the EmbeddableUIAbility. The result is returned to the caller. 2096 - If an exception occurs, for example, the EmbeddableUIAbility is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 2097 - 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. 2098 2099> **NOTE** 2100> 2101> 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). 2102 2103**Atomic service API**: This API can be used in atomic services since API version 12. 2104 2105**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2106 2107**Parameters** 2108 2109| Name| Type| Mandatory| Description| 2110| -------- | -------- | -------- | -------- | 2111| appId | string | Yes| Unique ID of the application, which is allocated by the cloud.| 2112| options | [AtomicServiceOptions](js-apis-app-ability-atomicServiceOptions.md) | No| Parameter carried in the request for starting the atomic service in jump-out mode.| 2113 2114 2115**Return value** 2116 2117| Type| Description| 2118| -------- | -------- | 2119| 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.| 2120 2121**Error codes** 2122 2123For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2124 2125| ID| Error Message| 2126| ------- | -------------------------------- | 2127| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2128| 16000002 | Incorrect ability type. | 2129| 16000003 | The specified ID does not exist. | 2130| 16000004 | Failed to start the invisible ability. | 2131| 16000011 | The context does not exist. | 2132| 16000012 | The application is controlled. | 2133| 16000050 | Internal error. | 2134| 16000053 | The ability is not on the top of the UI. | 2135| 16000055 | Installation-free timed out. | 2136| 16200001 | The caller has been released. | 2137 2138**Example** 2139 2140```ts 2141import { UIAbility, common, AtomicServiceOptions } from '@kit.AbilityKit'; 2142import { BusinessError } from '@kit.BasicServicesKit'; 2143 2144export default class EntryAbility extends UIAbility { 2145 onForeground() { 2146 let appId: string = '6918661953712445909'; 2147 let options: AtomicServiceOptions = { 2148 displayId: 0 2149 }; 2150 2151 try { 2152 this.context.openAtomicService(appId, options) 2153 .then((result: common.AbilityResult) => { 2154 // Carry out normal service processing. 2155 console.info('openAtomicService succeed'); 2156 }) 2157 .catch((err: BusinessError) => { 2158 // Process service logic errors. 2159 console.error(`openAtomicService failed, code is ${err.code}, message is ${err.message}`); 2160 }); 2161 } catch (err) { 2162 // Process input parameter errors. 2163 let code = (err as BusinessError).code; 2164 let message = (err as BusinessError).message; 2165 console.error(`openAtomicService failed, code is ${code}, message is ${message}`); 2166 } 2167 } 2168} 2169``` 2170 2171## UIAbilityContext.openLink<sup>12+<sup> 2172 2173openLink(link: string, options?: OpenLinkOptions, callback?: AsyncCallback<AbilityResult>): Promise<void> 2174 2175Starts a UIAbility through App Linking. This API uses a promise to return the result. It can be called only by the main thread. 2176 2177A 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: 2178- The **actions** field contains **ohos.want.action.viewData**. 2179- The **entities** field contains **entity.system.browsable**. 2180- The **uris** field contains elements whose **scheme** is **https** and **domainVerify** is **true**. 2181 2182If 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). 2183If 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. 2184 2185> **NOTE** 2186> 2187> 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). 2188 2189**Atomic service API**: This API can be used in atomic services since API version 12. 2190 2191**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2192 2193**Parameters** 2194 2195| Name| Type| Mandatory| Description| 2196| -------- | -------- | -------- | -------- | 2197| link | string | Yes| URL to open, which must be in the standard format.| 2198| options | [OpenLinkOptions](js-apis-app-ability-openLinkOptions.md) | No| Options of the URL.| 2199| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | No| Callback used to return the result.| 2200 2201**Return value** 2202 2203| Type| Description| 2204| -------- | -------- | 2205| Promise<void> | Promise that returns no value.| 2206 2207**Error codes** 2208 2209For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2210 2211| ID| Error Message| 2212| ------- | -------------------------------- | 2213| 201 | The application does not have permission to call the interface. | 2214| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2215| 16000001 | The specified ability does not exist. | 2216| 16000002 | Incorrect ability type. | 2217| 16000004 | Failed to start the invisible ability. | 2218| 16000005 | The specified process does not have the permission. | 2219| 16000006 | Cross-user operations are not allowed. | 2220| 16000008 | The crowdtesting application expires. | 2221| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2222| 16000010 | The call with the continuation flag is forbidden. | 2223| 16000011 | The context does not exist. | 2224| 16000012 | The application is controlled. | 2225| 16000013 | The application is controlled by EDM. | 2226| 16000019 | No matching ability is found. | 2227| 16200001 | The caller has been released. | 2228| 16000053 | The ability is not on the top of the UI. | 2229 2230**Example** 2231 2232```ts 2233import { common, OpenLinkOptions } from '@kit.AbilityKit'; 2234import { hilog } from '@kit.PerformanceAnalysisKit'; 2235import { BusinessError } from '@kit.BasicServicesKit'; 2236 2237const DOMAIN = 0xeeee; 2238const TAG: string = '[openLinkDemo]'; 2239 2240@Entry 2241@Component 2242struct Index { 2243 build() { 2244 RelativeContainer() { 2245 Button("Call StartAbilityForResult") 2246 .onClick(() => { 2247 let context = getContext(this) as common.UIAbilityContext; 2248 let link: string = 'https://www.example.com'; 2249 let openLinkOptions: OpenLinkOptions = { 2250 appLinkingOnly: true, 2251 parameters: { demo_key: 'demo_value' } 2252 }; 2253 2254 try { 2255 context.openLink( 2256 link, 2257 openLinkOptions, 2258 (err, result) => { 2259 hilog.error(DOMAIN, TAG, `openLink callback error.code: ${JSON.stringify(err)}`); 2260 hilog.info(DOMAIN, TAG, `openLink callback result: ${JSON.stringify(result.resultCode)}`); 2261 hilog.info(DOMAIN, TAG, `openLink callback result data: ${JSON.stringify(result.want)}`); 2262 } 2263 ).then(() => { 2264 hilog.info(DOMAIN, TAG, `open link success.`); 2265 }).catch((err: BusinessError) => { 2266 hilog.error(DOMAIN, TAG, `open link failed, errCode ${JSON.stringify(err.code)}`); 2267 }); 2268 } 2269 catch (e) { 2270 hilog.error(DOMAIN, TAG, `exception occured, errCode ${JSON.stringify(e.code)}`); 2271 } 2272 }) 2273 } 2274 .height('100%') 2275 .width('100%') 2276 } 2277} 2278``` 2279 2280## UIAbilityContext.backToCallerAbilityWithResult<sup>12+<sup> 2281 2282backToCallerAbilityWithResult(abilityResult: AbilityResult, requestCode: string): Promise<void> 2283 2284Returns 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. 2285 2286**Atomic service API**: This API can be used in atomic services since API version 12. 2287 2288**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2289 2290**Parameters** 2291 2292| Name| Type| Mandatory| Description| 2293| -------- | -------- | -------- | -------- | 2294| abilityResult | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes| Result returned to the caller.| 2295| 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**.| 2296 2297**Return value** 2298 2299| Type| Description| 2300| -------- | -------- | 2301| Promise<void> | Promise that returns no value.| 2302 2303**Error codes** 2304 2305For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2306 2307| ID| Error Message| 2308| ------- | -------------------------------- | 2309| 201 | The application does not have permission to call the interface. | 2310| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2311| 16000009 | An ability cannot be started or stopped in Wukong mode. | 2312| 16000011 | The context does not exist. | 2313| 16000050 | Internal error. | 2314| 16000074 | The caller does not exist. | 2315| 16000075 | Not support back to caller. | 2316 2317**Example** 2318The caller uses **startAbilityForResult** to start an ability, and the target ability calls **backToCallerAbilityWithResult** to return the result to the caller. 2319 2320```ts 2321// Caller 2322// index.ets 2323import { common, Want } from '@kit.AbilityKit'; 2324import { BusinessError } from '@ohos.base'; 2325import { hilog } from '@kit.PerformanceAnalysisKit'; 2326 2327@Entry 2328@Component 2329struct Index { 2330 @State message: string = 'Hello World'; 2331 2332 build() { 2333 Row() { 2334 Column() { 2335 Text(this.message) 2336 .fontSize(30) 2337 .fontWeight(FontWeight.Bold) 2338 2339 Button("Call StartAbilityForResult") 2340 .onClick(() => { 2341 let context: common.UIAbilityContext = getContext() as common.UIAbilityContext; 2342 let want: Want = { 2343 bundleName: 'com.example.demo2', 2344 abilityName: 'EntryAbility' 2345 }; 2346 2347 try { 2348 // Use startAbilityForResult to start the target ability. 2349 context.startAbilityForResult(want, (err: BusinessError, result: common.AbilityResult) => { 2350 if (err.code) { 2351 // Process service logic errors. 2352 hilog.error(0x0000, 'testTag', `startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); 2353 this.message = `startAbilityForResult failed: code is ${err.code}, message is ${err.message}` 2354 return; 2355 } 2356 // Carry out normal service processing. 2357 hilog.info(0x0000, 'testTag', `startAbilityForResult succeed`); 2358 hilog.info(0x0000, 'testTag', `AbilityResult is ${JSON.stringify(result)}`); 2359 this.message = `AbilityResult.resultCode: ${JSON.stringify(result.resultCode)}` 2360 }); 2361 } catch (err) { 2362 // Process input parameter errors. 2363 let code = (err as BusinessError).code; 2364 let message = (err as BusinessError).message; 2365 hilog.error(0x0000, 'testTag', `startAbilityForResult failed, code is ${code}, message is ${message}`); 2366 this.message = `startAbilityForResult failed, code is ${code}, message is ${message}`; 2367 } 2368 }) 2369 } 2370 .width('100%') 2371 } 2372 .height('100%') 2373 } 2374} 2375``` 2376 2377```ts 2378// Target ability 2379// EntryAbility.ets 2380import { AbilityConstant, common, UIAbility, Want, wantConstant } from '@kit.AbilityKit'; 2381import { hilog } from '@kit.PerformanceAnalysisKit'; 2382import { BusinessError } from '@kit.BasicServicesKit'; 2383 2384export default class EntryAbility extends UIAbility { 2385 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2386 // Obtain the CALLER_REQUEST_CODE of the caller from want and save it. 2387 let callerRequestCode: string = want?.parameters?.[wantConstant.Params.CALLER_REQUEST_CODE] as string; 2388 AppStorage.setOrCreate<string>("callerRequestCode", callerRequestCode) 2389 } 2390 2391 onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2392 let callerRequestCode: string = want?.parameters?.[wantConstant.Params.CALLER_REQUEST_CODE] as string; 2393 AppStorage.setOrCreate<string>("callerRequestCode", callerRequestCode) 2394 } 2395 2396 onForeground(): void { 2397 // Obtain the saved CALLER_REQUEST_CODE. 2398 let callerRequestCode: string = AppStorage.get<string>("callerRequestCode") as string; 2399 hilog.info(0x0000, 'testTag', `callerRequestCode is ${callerRequestCode}`); 2400 let want: Want = {}; 2401 let resultCode = 100; 2402 let abilityResult: common.AbilityResult = { 2403 want, 2404 resultCode 2405 }; 2406 try { 2407 // Return the result to the caller. 2408 this.context.backToCallerAbilityWithResult(abilityResult, callerRequestCode) 2409 .then(() => { 2410 // Carry out normal service processing. 2411 hilog.info(0x0000, 'testTag', 'backToCallerAbilityWithResult succeed'); 2412 }) 2413 .catch((err: BusinessError) => { 2414 // Process service logic errors. 2415 hilog.error(0x0000, 'testTag', `backToCallerAbilityWithResult failed, code is ${err.code}, message is ${err.message}`); 2416 }); 2417 } catch (err) { 2418 // Capture the synchronization parameter error. 2419 let code = (err as BusinessError).code; 2420 let message = (err as BusinessError).message; 2421 hilog.error(0x0000, 'testTag', `backToCallerAbilityWithResult failed, code is ${code}, message is ${message}`); 2422 } 2423 } 2424} 2425``` 2426 2427## UIAbilityContext.setRestoreEnabled<sup>14+</sup> 2428 2429setRestoreEnabled(enabled: boolean): void 2430 2431Sets whether to enable backup and restore for this UIAbility. 2432 2433**Atomic service API**: This API can be used in atomic services since API version 14. 2434 2435**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2436 2437**Parameters** 2438 2439| Name| Type| Mandatory| Description| 2440| -------- | -------- | -------- | -------- | 2441| enabled | boolean | Yes| Whether to enable backup and restore. The value **true** means to enable backup and restore, and **false** means the opposite.| 2442 2443**Error codes** 2444 2445For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2446 2447| ID| Error Message| 2448| ------- | -------------------------------- | 2449| 401 | If the input parameter is not valid parameter. | 2450| 16000011 | The context does not exist. | 2451 2452**Example** 2453 2454```ts 2455import { UIAbility, AbilityConstant, Want } from '@kit.AbilityKit'; 2456import { BusinessError } from '@kit.BasicServicesKit'; 2457 2458export default class EntryAbility extends UIAbility { 2459 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 2460 let enabled = true; 2461 try { 2462 this.context.setRestoreEnabled(enabled); 2463 } catch (paramError) { 2464 let code = (paramError as BusinessError).code; 2465 let message = (paramError as BusinessError).message; 2466 console.error(`setRestoreEnabled failed, err code: ${code}, err msg: ${message}`); 2467 } 2468 } 2469} 2470``` 2471 2472## UIAbilityContext.startUIServiceExtensionAbility<sup>14+<sup> 2473 2474startUIServiceExtensionAbility(want: Want): Promise<void> 2475 2476Starts a UIServiceExtensionAbility. This API uses a promise to return the result. 2477 2478> **NOTE** 2479> 2480> 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). 2481 2482**Atomic service API**: This API can be used in atomic services since API version 14. 2483 2484**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2485 2486**Parameters** 2487 2488| Name | Type | Mandatory| Description | 2489| -------- | --------------------------------------- | ---- | ------------------------ | 2490| want | [Want](js-apis-app-ability-want.md) | Yes| Want information required for startup.| 2491 2492**Return value** 2493 2494| Type | Description | 2495| ------------------- | -------------------------------------- | 2496| Promise<void> | Promise that returns no value.| 2497 2498**Error codes** 2499 2500For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2501 2502| ID| Error Message | 2503| -------- | ----------------------------------------------------------------------------------------------------------- | 2504| 201 | The application does not have permission to call the interface. | 2505| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2506| 801 | Capability not supported. | 2507| 16000001 | The specified ability does not exist. | 2508| 16000002 | Incorrect ability type. | 2509| 16000004 | Failed to start the invisible ability. | 2510| 16000005 | The specified process does not have the permission. | 2511| 16000008 | The crowdtesting application expires. | 2512| 16000011 | The context does not exist. | 2513| 16000012 | The application is controlled. | 2514| 16000013 | The application is controlled by EDM. | 2515| 16000019 | No matching ability is found. | 2516| 16000050 | Internal error. | 2517| 16200001 | The caller has been released. | 2518 2519**Example** 2520 2521```ts 2522import { common, Want } from '@kit.AbilityKit'; 2523import { BusinessError } from '@kit.BasicServicesKit'; 2524 2525@Entry 2526@Component 2527struct Index { 2528 build() { 2529 Column() { 2530 Row() { 2531 // Create a Start button. 2532 Button('start ability') 2533 .enabled(true) 2534 .onClick(() => { 2535 let context = getContext(this) as common.UIAbilityContext; 2536 let startWant: Want = { 2537 bundleName: 'com.acts.uiserviceextensionability', 2538 abilityName: 'UiServiceExtAbility', 2539 }; 2540 try { 2541 // Start the UIServiceExtensionAbility. 2542 context.startUIServiceExtensionAbility(startWant).then(() => { 2543 console.log('startUIServiceExtensionAbility success'); 2544 }).catch((error: BusinessError) => { 2545 console.log('startUIServiceExtensionAbility error', JSON.stringify(error)); 2546 }) 2547 } catch (err) { 2548 console.log('startUIServiceExtensionAbility failed', JSON.stringify(err)); 2549 } 2550 }) 2551 } 2552 } 2553 } 2554} 2555``` 2556 2557## UIAbilityContext.connectUIServiceExtensionAbility<sup>14+<sup> 2558 2559connectUIServiceExtensionAbility(want: Want, callback: UIServiceExtensionConnectCallback) : Promise<UIServiceProxy> 2560 2561Connects to a UIServiceExtensionAbility. This API uses a promise to return the result. 2562 2563> **NOTE** 2564> 2565> 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). 2566> 2567 2568**Atomic service API**: This API can be used in atomic services since API version 14. 2569 2570**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2571 2572**Parameters** 2573 2574| Name| Type| Mandatory| Description | 2575| ------ | ---- | ---- | ---- | 2576| want |[Want](js-apis-app-ability-want.md) | Yes | Want information required for connection.| 2577| callback | [UIServiceExtensionConnectCallback](js-apis-inner-application-uiServiceExtensionconnectcallback.md) | Yes | Callback for connecting to the UIServiceExtensionAbility.| 2578 2579**Return value** 2580 2581| Type | Description | 2582| ------------------- | -------------------------------------- | 2583| Promise<UIServiceProxy> | Promise used to return a [UIServiceProxy](js-apis-inner-application-uiserviceproxy.md) object.| 2584 2585**Error codes** 2586 2587For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2588 2589| ID| Error Message | 2590| -------- | ----------------------------------------------------------------------------------- | 2591| 201 | The application does not have permission to call the interface. | 2592| 801 | Capability not supported. | 2593| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2594| 16000001 | The specified ability does not exist. | 2595| 16000002 | Incorrect ability type. | 2596| 16000004 | Failed to start the invisible ability. | 2597| 16000005 | The specified process does not have the permission. | 2598| 16000008 | The crowdtesting application expires. | 2599| 16000011 | The context does not exist. | 2600| 16000013 | The EDM prohibits the application from launching. | 2601| 16000050 | Internal error. | 2602| 16000055 | Installation-free timed out. | 2603 2604**Example** 2605 2606```ts 2607import { common, Want } from '@kit.AbilityKit'; 2608import { BusinessError } from '@kit.BasicServicesKit'; 2609 2610const TAG: string = '[Extension] '; 2611 2612@Entry 2613@Component 2614struct UIServiceExtensionAbility { 2615 dataCallBack : common.UIServiceExtensionConnectCallback = { 2616 // Receive data 2617 onData: (data: Record<string, Object>) => { 2618 console.log(`dataCallBack received data`, JSON.stringify(data)); 2619 }, 2620 // Disconnect from the UIServiceExtensionAbility. 2621 onDisconnect: () => { 2622 console.log(`dataCallBack onDisconnect`); 2623 } 2624 } 2625 2626 async myConnect() { 2627 // Obtain the context. 2628 let context = getContext(this) as common.UIAbilityContext; 2629 let startWant: Want = { 2630 deviceId: '', 2631 bundleName: 'com.example.myapplication', 2632 abilityName: 'UiServiceExtAbility' 2633 }; 2634 2635 try { 2636 // Connect to the UIServiceExtensionAbility. 2637 context.connectUIServiceExtensionAbility(startWant, this.dataCallBack) 2638 .then((proxy: common.UIServiceProxy) => { 2639 console.log(TAG + `try to connectUIServiceExtensionAbility`, JSON.stringify(proxy)); 2640 }).catch((err: Error) => { 2641 let code = (err as BusinessError).code; 2642 let message = (err as BusinessError).message; 2643 console.log(TAG + `connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); 2644 }); 2645 } catch (err) { 2646 let code = (err as BusinessError).code; 2647 let message = (err as BusinessError).message; 2648 console.log(TAG + `connectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); 2649 }; 2650 } 2651 2652 build() { 2653 RelativeContainer() { 2654 // Create a Connect button. 2655 Button('connectServiceExtensionAbility', { type: ButtonType.Capsule, stateEffect: true }) 2656 .alignRules({ 2657 center: { anchor: '__container__', align: VerticalAlign.Center }, 2658 middle: { anchor: '__container__', align: HorizontalAlign.Center } 2659 }) 2660 .onClick(() => { 2661 this.myConnect() 2662 }); 2663 } 2664 .height('100%') 2665 .width('100%') 2666 } 2667} 2668``` 2669 2670## UIAbilityContext.disconnectUIServiceExtensionAbility<sup>14+<sup> 2671 2672disconnectUIServiceExtensionAbility(proxy: UIServiceProxy): Promise<void> 2673 2674Disconnects from a UIServiceExtensionAbility. This API uses a promise to return the result. 2675 2676> **NOTE** 2677> 2678> 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). 2679> 2680 2681**Atomic service API**: This API can be used in atomic services since API version 14. 2682 2683**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2684 2685**Parameters** 2686 2687| Name| Type| Mandatory| Description | 2688| ------ | ---- | ---- | -------------------- | 2689| proxy | [UIServiceProxy](js-apis-inner-application-uiserviceproxy.md) | Yes| Proxy returned after [connectUIServiceExtensionAbility](#uiabilitycontextconnectuiserviceextensionability13) is called.| 2690 2691**Return value** 2692 2693| Type | Description | 2694| ------------------- | -------------------------------------- | 2695| Promise<void> | Promise that returns no value.| 2696 2697**Error codes** 2698 2699For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2700 2701| ID| Error Message | 2702| -------- | ------------------------------------------------------------------------------------------- | 2703| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2704| 16000011 | The context does not exist. | 2705| 16000050 | Internal error. | 2706 2707**Example** 2708 2709```ts 2710import { common } from '@kit.AbilityKit'; 2711import { BusinessError } from '@kit.BasicServicesKit'; 2712 2713const TAG: string = '[Extension] '; 2714 2715@Entry 2716@Component 2717struct UIServiceExtensionAbility { 2718 comProxy: common.UIServiceProxy | null = null; 2719 2720 build() { 2721 Scroll() { 2722 Column() { 2723 // Create a Disconnect button. 2724 Button('disconnectUIServiceExtensionAbility', { type: ButtonType.Capsule, stateEffect: true }) 2725 .margin({ 2726 top: 5, 2727 left: 10, 2728 right: 10, 2729 bottom: 5 2730 }) 2731 .alignRules({ 2732 center: { anchor: '__container__', align: VerticalAlign.Center }, 2733 middle: { anchor: '__container__', align: HorizontalAlign.Center } 2734 }) 2735 .onClick(() => { 2736 this.myDisconnectUIServiceExtensionAbility() 2737 }); 2738 } 2739 .width('100%') 2740 } 2741 .height('100%') 2742 } 2743 2744 myDisconnectUIServiceExtensionAbility() { 2745 let context = getContext(this) as common.UIAbilityContext; 2746 2747 try { 2748 // Disconnect from the UIServiceExtensionAbility. 2749 context.disconnectUIServiceExtensionAbility(this.comProxy) 2750 .then(() => { 2751 console.log(TAG + `disconnectUIServiceExtensionAbility succeed ${this.comProxy}}`); 2752 }).catch((err: Error) => { 2753 let code = (err as BusinessError).code; 2754 let message = (err as BusinessError).message; 2755 console.log(TAG + `disconnectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); 2756 }); 2757 } catch (err) { 2758 let code = (err as BusinessError).code; 2759 let message = (err as BusinessError).message; 2760 console.log(TAG + `disconnectUIServiceExtensionAbility failed, code is ${code}, message is ${message}`); 2761 } 2762 } 2763} 2764``` 2765 2766## UIAbilityContext.setAbilityInstanceInfo<sup>15+<sup> 2767 2768setAbilityInstanceInfo(label: string, icon: image.PixelMap) : Promise<void> 2769 2770Sets 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. 2771 2772 2773> **NOTE** 2774> 2775> This API is available only for 2-in-1 devices. 2776 2777**Required permissions**: ohos.permission.SET_ABILITY_INSTANCE_INFO 2778 2779**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2780 2781**Parameters** 2782 2783| Name| Type | Mandatory| Description | 2784| ------ | -------------------------------------------------------------- | ---- | -------------------------------------------------- | 2785| label |string | Yes | Label. The label cannot be an empty string, and can contain a maximum of 1024 bytes. | 2786| icon | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | Yes | Icon. The recommended icon size is 512 px * 512 px. | 2787 2788**Return value** 2789 2790| Type | Description | 2791| ------------------- | -------------------------------------- | 2792| Promise<void> | Promise that returns no value.| 2793 2794**Error codes** 2795 2796For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2797 2798| ID| Error Message | 2799| -------- | ----------------------------------------------------------------------------------- | 2800| 201 | The application does not have permission to call the interface. | 2801| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 2802| 801 | Capability not supported. | 2803| 16000011 | The context does not exist. | 2804| 16000050 | Internal error. | 2805 2806**Example** 2807 2808```ts 2809import { UIAbility } from '@kit.AbilityKit'; 2810import { image } from '@kit.ImageKit'; 2811import { BusinessError } from '@kit.BasicServicesKit'; 2812import { window } from '@kit.ArkUI'; 2813 2814export default class EntryAbility extends UIAbility { 2815 onWindowStageCreate(windowStage: window.WindowStage): void { 2816 windowStage.loadContent('pages/Index', async (err, data) => { 2817 if (err.code) { 2818 console.error(`loadContent failed, code is ${err.code}`); 2819 return; 2820 } 2821 2822 let newLabel: string = 'instance label'; 2823 let color = new ArrayBuffer(0); 2824 let imagePixelMap: image.PixelMap = await image.createPixelMap(color, { 2825 size: { 2826 height: 100, 2827 width: 100 2828 } 2829 }); 2830 this.context.setAbilityInstanceInfo(newLabel, imagePixelMap) 2831 .then(() => { 2832 console.info('setAbilityInstanceInfo success'); 2833 }).catch((err: BusinessError) => { 2834 console.error(`setAbilityInstanceInfo failed, code is ${err.code}, message is ${err.message}`); 2835 }); 2836 }); 2837 } 2838} 2839``` 2840 2841## UIAbilityContext.setColorMode<sup>18+</sup> 2842 2843setColorMode(colorMode: ConfigurationConstant.ColorMode): void 2844 2845Sets the color mode for this UIAbility. Before calling this API, ensure that the page corresponding to the UIAbility has been loaded. This API can be called only by the main thread. 2846 2847> **NOTE** 2848> - After this API is called, a new resource manager object is created. If a resource manager was previously cached, it should be updated accordingly. 2849> - The priority of the color mode is as follows: UIAbility color mode > Application color mode (set via [ApplicationContext.setColorMode](js-apis-inner-application-applicationContext.md)) > System color mode. 2850 2851**Atomic service API**: This API can be used in atomic services since API version 18. 2852 2853**System capability**: SystemCapability.Ability.AbilityRuntime.Core 2854 2855**Parameters** 2856 2857| Name| Type | Mandatory| Description | 2858| ------ | ------------- | ---- | -------------------- | 2859| colorMode | [ConfigurationConstant.ColorMode](js-apis-app-ability-configurationConstant.md) | Yes | Color mode. The options are as follows:<br> - **COLOR_MODE_DARK**: dark mode.<br> - **COLOR_MODE_LIGHT**: light mode.<br> - **COLOR_MODE_NOT_SET**: not set (following the system or application).| 2860 2861**Error codes** 2862 2863For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 2864 2865| ID| Error Message| 2866| ------- | -------- | 2867| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 2868| 16000011 | The context does not exist. | 2869 2870**Example** 2871 2872```ts 2873import { UIAbility, ConfigurationConstant } from '@kit.AbilityKit'; 2874import { hilog } from '@kit.PerformanceAnalysisKit'; 2875import { window } from '@kit.ArkUI'; 2876 2877export default class MyAbility extends UIAbility { 2878 onWindowStageCreate(windowStage: window.WindowStage) { 2879 windowStage.loadContent('pages/Index', (err, data) => { 2880 if (err.code) { 2881 hilog.error(0x0000, 'testTag', 'Failed to load the content.'); 2882 return; 2883 } 2884 let uiAbilityContext = this.context; 2885 uiAbilityContext.setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_DARK); 2886 }); 2887 } 2888} 2889``` 2890