1# UIExtensionContext 2 3**UIExtensionContext**, inherited from [ExtensionContext](js-apis-inner-application-extensionContext.md), provides the context environment for the [UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md). It provides UIExtensionAbility-related configurations and APIs for operating the UIExtensionAbility. For example, you can use the APIs to start a UIExtensionAbility. 4 5> **NOTE** 6> 7> - The initial APIs of this module are supported since API version 10. 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> - The APIs of this module must be used in the main thread, but not in sub-threads such as Worker and TaskPool. 10 11## Modules to Import 12 13```ts 14import { common } from '@kit.AbilityKit'; 15``` 16 17## UIExtensionContext.startAbility 18 19startAbility(want: Want, callback: AsyncCallback<void>): void 20 21Starts an ability. This API uses an asynchronous callback to return the result. 22 23> **NOTE** 24> 25> 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). 26 27**System capability**: SystemCapability.Ability.AbilityRuntime.Core 28 29**Parameters** 30 31| Name| Type| Mandatory| Description| 32| -------- | -------- | -------- | -------- | 33| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 34| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 35 36**Error codes** 37 38For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 39 40| ID| Error Message| 41| ------- | -------------------------------- | 42| 201 | The application does not have permission to call the interface. | 43| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 44| 16000001 | The specified ability does not exist. | 45| 16000002 | Incorrect ability type. | 46| 16000004 | Failed to start the invisible ability. | 47| 16000005 | The specified process does not have the permission. | 48| 16000006 | Cross-user operations are not allowed. | 49| 16000008 | The crowdtesting application expires. | 50| 16000009 | An ability cannot be started or stopped in Wukong mode. | 51| 16000010 | The call with the continuation flag is forbidden. | 52| 16000011 | The context does not exist. | 53| 16000012 | The application is controlled. | 54| 16000013 | The application is controlled by EDM. | 55| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 56| 16000019 | No matching ability is found. | 57| 16000050 | Internal error. | 58| 16000053 | The ability is not on the top of the UI. | 59| 16000055 | Installation-free timed out. | 60| 16000069 | The extension cannot start the third party application. | 61| 16000070 | The extension cannot start the service. | 62| 16000071 | App clone is not supported. | 63| 16000072 | App clone or multi-instance is not supported. | 64| 16000073 | The app clone index is invalid. | 65| 16000076 | The app instance key is invalid. | 66| 16000077 | The number of app instances reaches the limit. | 67| 16000078 | The multi-instance is not supported. | 68| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 69| 16000080 | Creating an instance is not supported. | 70| 16000082 | The UIAbility is being started. | 71| 16200001 | The caller has been released. | 72 73**Example** 74 75```ts 76import { UIExtensionAbility, Want } from '@kit.AbilityKit'; 77import { BusinessError } from '@kit.BasicServicesKit'; 78 79export default class EntryAbility extends UIExtensionAbility { 80 81 onForeground() { 82 let want: Want = { 83 bundleName: 'com.example.myapplication', 84 abilityName: 'EntryAbility' 85 }; 86 87 try { 88 this.context.startAbility(want, (err: BusinessError) => { 89 if (err.code) { 90 // Process service logic errors. 91 console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); 92 return; 93 } 94 // Carry out normal service processing. 95 console.info('startAbility succeed'); 96 }); 97 } catch (err) { 98 // Process input parameter errors. 99 let code = (err as BusinessError).code; 100 let message = (err as BusinessError).message; 101 console.error(`startAbility failed, code is ${code}, message is ${message}`); 102 } 103 } 104} 105``` 106 107## UIExtensionContext.startAbility 108 109startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 110 111Starts an ability with the start options specified. This API uses an asynchronous callback to return the result. 112 113> **NOTE** 114> 115> 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). 116 117**System capability**: SystemCapability.Ability.AbilityRuntime.Core 118 119**Parameters** 120 121| Name| Type| Mandatory| Description| 122| -------- | -------- | -------- | -------- | 123| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 124| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 125| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 126 127**Error codes** 128 129For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 130 131| ID| Error Message| 132| ------- | -------------------------------- | 133| 201 | The application does not have permission to call the interface. | 134| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 135| 16000001 | The specified ability does not exist. | 136| 16000004 | Failed to start the invisible ability. | 137| 16000005 | The specified process does not have the permission. | 138| 16000006 | Cross-user operations are not allowed. | 139| 16000008 | The crowdtesting application expires. | 140| 16000009 | An ability cannot be started or stopped in Wukong mode. | 141| 16000011 | The context does not exist. | 142| 16000012 | The application is controlled. | 143| 16000013 | The application is controlled by EDM. | 144| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 145| 16000019 | No matching ability is found. | 146| 16000050 | Internal error. | 147| 16000053 | The ability is not on the top of the UI. | 148| 16000055 | Installation-free timed out. | 149| 16000069 | The extension cannot start the third party application. | 150| 16000070 | The extension cannot start the service. | 151| 16000071 | App clone is not supported. | 152| 16000072 | App clone or multi-instance is not supported. | 153| 16000073 | The app clone index is invalid. | 154| 16000076 | The app instance key is invalid. | 155| 16000077 | The number of app instances reaches the limit. | 156| 16000078 | The multi-instance is not supported. | 157| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 158| 16000080 | Creating an instance is not supported. | 159| 16000082 | The UIAbility is being started. | 160| 16200001 | The caller has been released. | 161 162**Example** 163 164```ts 165import { UIExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 166import { BusinessError } from '@kit.BasicServicesKit'; 167 168export default class EntryAbility extends UIExtensionAbility { 169 onForeground() { 170 let want: Want = { 171 deviceId: '', 172 bundleName: 'com.example.myapplication', 173 abilityName: 'EntryAbility' 174 }; 175 let options: StartOptions = { 176 displayId: 0 177 }; 178 179 try { 180 this.context.startAbility(want, options, (err: BusinessError) => { 181 if (err.code) { 182 // Process service logic errors. 183 console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); 184 return; 185 } 186 // Carry out normal service processing. 187 console.info('startAbility succeed'); 188 }); 189 } catch (err) { 190 // Process input parameter errors. 191 let code = (err as BusinessError).code; 192 let message = (err as BusinessError).message; 193 console.error(`startAbility failed, code is ${code}, message is ${message}`); 194 } 195 } 196} 197``` 198 199## UIExtensionContext.startAbility 200 201startAbility(want: Want, options?: StartOptions): Promise<void> 202 203Starts an ability. This API uses a promise to return the result. 204 205> **NOTE** 206> 207> 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). 208 209**System capability**: SystemCapability.Ability.AbilityRuntime.Core 210 211**Parameters** 212 213| Name| Type| Mandatory| Description| 214| -------- | -------- | -------- | -------- | 215| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 216| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 217 218**Return value** 219 220| Type| Description| 221| -------- | -------- | 222| Promise<void> | Promise that returns no value.| 223 224**Error codes** 225 226For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 227 228| ID| Error Message| 229| ------- | -------------------------------- | 230| 201 | The application does not have permission to call the interface. | 231| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 232| 16000001 | The specified ability does not exist. | 233| 16000002 | Incorrect ability type. | 234| 16000004 | Failed to start the invisible ability. | 235| 16000005 | The specified process does not have the permission. | 236| 16000006 | Cross-user operations are not allowed. | 237| 16000008 | The crowdtesting application expires. | 238| 16000009 | An ability cannot be started or stopped in Wukong mode. | 239| 16000010 | The call with the continuation flag is forbidden. | 240| 16000011 | The context does not exist. | 241| 16000012 | The application is controlled. | 242| 16000013 | The application is controlled by EDM. | 243| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 244| 16000019 | No matching ability is found. | 245| 16000050 | Internal error. | 246| 16000053 | The ability is not on the top of the UI. | 247| 16000055 | Installation-free timed out. | 248| 16000069 | The extension cannot start the third party application. | 249| 16000070 | The extension cannot start the service. | 250| 16000071 | App clone is not supported. | 251| 16000072 | App clone or multi-instance is not supported. | 252| 16000073 | The app clone index is invalid. | 253| 16000076 | The app instance key is invalid. | 254| 16000077 | The number of app instances reaches the limit. | 255| 16000078 | The multi-instance is not supported. | 256| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 257| 16000080 | Creating an instance is not supported. | 258| 16000082 | The UIAbility is being started. | 259| 16200001 | The caller has been released. | 260 261**Example** 262 263```ts 264import { UIExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 265import { BusinessError } from '@kit.BasicServicesKit'; 266 267export default class EntryAbility extends UIExtensionAbility { 268 onForeground() { 269 let want: Want = { 270 bundleName: 'com.example.myapplication', 271 abilityName: 'EntryAbility' 272 }; 273 let options: StartOptions = { 274 displayId: 0, 275 }; 276 277 try { 278 this.context.startAbility(want, options) 279 .then(() => { 280 // Carry out normal service processing. 281 console.info('startAbility succeed'); 282 }) 283 .catch((err: BusinessError) => { 284 // Process service logic errors. 285 console.error(`startAbility failed, code is ${err.code}, message is ${err.message}`); 286 }); 287 } catch (err) { 288 // Process input parameter errors. 289 let code = (err as BusinessError).code; 290 let message = (err as BusinessError).message; 291 console.error(`startAbility failed, code is ${code}, message is ${message}`); 292 } 293 } 294} 295``` 296 297## UIExtensionContext.startAbilityForResult 298 299startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void 300 301Starts an ability and obtains the result when the ability is terminated. This API uses an asynchronous callback to return the result. The following situations may be possible for a started ability: 302 - Normally, you can call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. 303 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 304 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an exception message, in which **resultCode** is **-1**, is returned to others. 305 306> **NOTE** 307> 308> 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). 309 310**System capability**: SystemCapability.Ability.AbilityRuntime.Core 311 312**Parameters** 313 314| Name| Type| Mandatory| Description| 315| -------- | -------- | -------- | -------- | 316| want |[Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 317| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result.| 318 319**Error codes** 320 321For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 322 323| ID| Error Message| 324| ------- | -------------------------------- | 325| 201 | The application does not have permission to call the interface. | 326| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 327| 16000001 | The specified ability does not exist. | 328| 16000002 | Incorrect ability type. | 329| 16000004 | Failed to start the invisible ability. | 330| 16000005 | The specified process does not have the permission. | 331| 16000006 | Cross-user operations are not allowed. | 332| 16000008 | The crowdtesting application expires. | 333| 16000009 | An ability cannot be started or stopped in Wukong mode. | 334| 16000010 | The call with the continuation flag is forbidden. | 335| 16000011 | The context does not exist. | 336| 16000012 | The application is controlled. | 337| 16000013 | The application is controlled by EDM. | 338| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 339| 16000019 | No matching ability is found. | 340| 16000050 | Internal error. | 341| 16000053 | The ability is not on the top of the UI. | 342| 16000055 | Installation-free timed out. | 343| 16000069 | The extension cannot start the third party application. | 344| 16000070 | The extension cannot start the service. | 345| 16000071 | App clone is not supported. | 346| 16000072 | App clone or multi-instance is not supported. | 347| 16000073 | The app clone index is invalid. | 348| 16000076 | The app instance key is invalid. | 349| 16000077 | The number of app instances reaches the limit. | 350| 16000078 | The multi-instance is not supported. | 351| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 352| 16000080 | Creating an instance is not supported. | 353| 16000082 | The UIAbility is being started. | 354| 16200001 | The caller has been released. | 355 356**Example** 357 358```ts 359import { UIExtensionAbility, Want, common } from '@kit.AbilityKit'; 360import { BusinessError } from '@kit.BasicServicesKit'; 361 362export default class EntryAbility extends UIExtensionAbility { 363 onForeground() { 364 let want: Want = { 365 deviceId: '', 366 bundleName: 'com.example.myapplication', 367 }; 368 369 try { 370 this.context.startAbilityForResult(want, (err: BusinessError, result: common.AbilityResult) => { 371 if (err.code) { 372 // Process service logic errors. 373 console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); 374 return; 375 } 376 // Carry out normal service processing. 377 console.info('startAbilityForResult succeed'); 378 }); 379 } catch (err) { 380 // Process input parameter errors. 381 let code = (err as BusinessError).code; 382 let message = (err as BusinessError).message; 383 console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`); 384 } 385 } 386} 387``` 388 389## UIExtensionContext.startAbilityForResult 390 391startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void 392 393Starts an ability with the start options specified and obtains the result when the ability is terminated. This API uses an asynchronous callback to return the result. The following situations may be possible for a started ability: 394 - Normally, you can call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. 395 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 396 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an exception message, in which **resultCode** is **-1**, is returned to others. 397 398> **NOTE** 399> 400> 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). 401 402**System capability**: SystemCapability.Ability.AbilityRuntime.Core 403 404**Parameters** 405 406| Name| Type| Mandatory| Description| 407| -------- | -------- | -------- | -------- | 408| want |[Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 409| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 410| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result.| 411 412**Error codes** 413 414For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 415 416| ID| Error Message| 417| ------- | -------------------------------- | 418| 201 | The application does not have permission to call the interface. | 419| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 420| 16000001 | The specified ability does not exist. | 421| 16000004 | Failed to start the invisible ability. | 422| 16000005 | The specified process does not have the permission. | 423| 16000006 | Cross-user operations are not allowed. | 424| 16000008 | The crowdtesting application expires. | 425| 16000009 | An ability cannot be started or stopped in Wukong mode. | 426| 16000011 | The context does not exist. | 427| 16000012 | The application is controlled. | 428| 16000013 | The application is controlled by EDM. | 429| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 430| 16000019 | No matching ability is found. | 431| 16000050 | Internal error. | 432| 16000053 | The ability is not on the top of the UI. | 433| 16000055 | Installation-free timed out. | 434| 16000069 | The extension cannot start the third party application. | 435| 16000070 | The extension cannot start the service. | 436| 16000071 | App clone is not supported. | 437| 16000072 | App clone or multi-instance is not supported. | 438| 16000073 | The app clone index is invalid. | 439| 16000076 | The app instance key is invalid. | 440| 16000077 | The number of app instances reaches the limit. | 441| 16000078 | The multi-instance is not supported. | 442| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 443| 16000080 | Creating an instance is not supported. | 444| 16000082 | The UIAbility is being started. | 445| 16200001 | The caller has been released. | 446 447**Example** 448 449```ts 450import { UIExtensionAbility, Want, common, StartOptions } from '@kit.AbilityKit'; 451import { BusinessError } from '@kit.BasicServicesKit'; 452 453export default class EntryAbility extends UIExtensionAbility { 454 onForeground() { 455 let want: Want = { 456 deviceId: '', 457 bundleName: 'com.example.myapplication', 458 abilityName: 'EntryAbility' 459 }; 460 let options: StartOptions = { 461 displayId: 0, 462 }; 463 464 try { 465 this.context.startAbilityForResult(want, options, (err: BusinessError, result: common.AbilityResult) => { 466 if (err.code) { 467 // Process service logic errors. 468 console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); 469 return; 470 } 471 // Carry out normal service processing. 472 console.info('startAbilityForResult succeed'); 473 }); 474 } catch (err) { 475 // Process input parameter errors. 476 let code = (err as BusinessError).code; 477 let message = (err as BusinessError).message; 478 console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`); 479 } 480 } 481} 482``` 483 484## UIExtensionContext.startAbilityForResult 485 486startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult> 487 488Starts an ability and obtains the result when the ability is terminated. This API uses a promise to return the result. The following situations may be possible for a started ability: 489 - Normally, you can call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult) to terminate the ability. The result is returned to the caller. 490 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 491 - If different applications call this API to start an ability that uses the singleton mode and then call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult) to terminate the ability, the normal result is returned to the last caller, and an exception message, in which **resultCode** is **-1**, is returned to others. 492 493> **NOTE** 494> 495> 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). 496 497**System capability**: SystemCapability.Ability.AbilityRuntime.Core 498 499**Parameters** 500 501| Name| Type| Mandatory| Description| 502| -------- | -------- | -------- | -------- | 503| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 504| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 505 506 507**Return value** 508 509| Type| Description| 510| -------- | -------- | 511| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise used to return the result.| 512 513**Error codes** 514 515For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 516 517| ID| Error Message| 518| ------- | -------------------------------- | 519| 201 | The application does not have permission to call the interface. | 520| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 521| 16000001 | The specified ability does not exist. | 522| 16000002 | Incorrect ability type. | 523| 16000004 | Failed to start the invisible ability. | 524| 16000005 | The specified process does not have the permission. | 525| 16000006 | Cross-user operations are not allowed. | 526| 16000008 | The crowdtesting application expires. | 527| 16000009 | An ability cannot be started or stopped in Wukong mode. | 528| 16000010 | The call with the continuation flag is forbidden. | 529| 16000011 | The context does not exist. | 530| 16000012 | The application is controlled. | 531| 16000013 | The application is controlled by EDM. | 532| 16000018 | Redirection to a third-party application is not allowed in API version 11 or later. | 533| 16000019 | No matching ability is found. | 534| 16000050 | Internal error. | 535| 16000053 | The ability is not on the top of the UI. | 536| 16000055 | Installation-free timed out. | 537| 16000069 | The extension cannot start the third party application. | 538| 16000070 | The extension cannot start the service. | 539| 16000071 | App clone is not supported. | 540| 16000072 | App clone or multi-instance is not supported. | 541| 16000073 | The app clone index is invalid. | 542| 16000076 | The app instance key is invalid. | 543| 16000077 | The number of app instances reaches the limit. | 544| 16000078 | The multi-instance is not supported. | 545| 16000079 | The APP_INSTANCE_KEY cannot be specified. | 546| 16000080 | Creating an instance is not supported. | 547| 16000082 | The UIAbility is being started. | 548| 16200001 | The caller has been released. | 549 550**Example** 551 552```ts 553import { UIExtensionAbility, Want, common, StartOptions } from '@kit.AbilityKit'; 554import { BusinessError } from '@kit.BasicServicesKit'; 555 556export default class EntryAbility extends UIExtensionAbility { 557 onForeground() { 558 let want: Want = { 559 bundleName: 'com.example.myapplication', 560 abilityName: 'EntryAbility' 561 }; 562 let options: StartOptions = { 563 displayId: 0, 564 }; 565 566 try { 567 this.context.startAbilityForResult(want, options) 568 .then((result: common.AbilityResult) => { 569 // Carry out normal service processing. 570 console.info('startAbilityForResult succeed'); 571 }) 572 .catch((err: BusinessError) => { 573 // Process service logic errors. 574 console.error(`startAbilityForResult failed, code is ${err.code}, message is ${err.message}`); 575 }); 576 } catch (err) { 577 // Process input parameter errors. 578 let code = (err as BusinessError).code; 579 let message = (err as BusinessError).message; 580 console.error(`startAbilityForResult failed, code is ${code}, message is ${message}`); 581 } 582 } 583} 584``` 585 586 587## UIExtensionContext.connectServiceExtensionAbility 588 589connectServiceExtensionAbility(want: Want, options: ConnectOptions): number 590 591Connects this ability to a ServiceExtensionAbility. 592 593> **NOTE** 594> 595> 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). 596 597**System capability**: SystemCapability.Ability.AbilityRuntime.Core 598 599**Parameters** 600 601| Name| Type| Mandatory| Description| 602| -------- | -------- | -------- | -------- | 603| want | [Want](js-apis-app-ability-want.md) | Yes| Want information for connecting to the ServiceExtensionAbility.| 604| options | [ConnectOptions](js-apis-inner-ability-connectOptions.md) | Yes| Instance of the callback function after the connection to the ServiceExtensionAbility is set up.| 605 606**Return value** 607 608| Type| Description| 609| -------- | -------- | 610| number | Result code of the connection.| 611 612**Error codes** 613 614For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 615 616| ID| Error Message| 617| ------- | -------------------------------- | 618| 201 | The application does not have permission to call the interface. | 619| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 620| 16000001 | The specified ability does not exist. | 621| 16000002 | Incorrect ability type. | 622| 16000004 | Failed to start the invisible ability. | 623| 16000005 | The specified process does not have the permission. | 624| 16000006 | Cross-user operations are not allowed. | 625| 16000008 | The crowdtesting application expires. | 626| 16000011 | The context does not exist. | 627| 16000050 | Internal error. | 628| 16000053 | The ability is not on the top of the UI. | 629| 16000055 | Installation-free timed out. | 630| 16000070 | The extension cannot start the service. | 631 632**Example** 633 634```ts 635import { UIExtensionAbility, Want, common } from '@kit.AbilityKit'; 636import { rpc } from '@kit.IPCKit'; 637import { BusinessError } from '@kit.BasicServicesKit'; 638 639export default class EntryAbility extends UIExtensionAbility { 640 onForeground() { 641 let want: Want = { 642 deviceId: '', 643 bundleName: 'com.example.myapplication', 644 abilityName: 'ServiceExtensionAbility' 645 }; 646 let commRemote: rpc.IRemoteObject; 647 let options: common.ConnectOptions = { 648 onConnect(elementName, remote) { 649 commRemote = remote; 650 console.info('onConnect...') 651 }, 652 onDisconnect(elementName) { 653 console.info('onDisconnect...') 654 }, 655 onFailed(code) { 656 console.info('onFailed...') 657 } 658 }; 659 let connection: number; 660 try { 661 connection = this.context.connectServiceExtensionAbility(want, options); 662 } catch (err) { 663 // Process input parameter errors. 664 let code = (err as BusinessError).code; 665 let message = (err as BusinessError).message; 666 console.error(`connectServiceExtensionAbility failed, code is ${code}, message is ${message}`); 667 } 668 } 669} 670``` 671 672## UIExtensionContext.disconnectServiceExtensionAbility 673 674disconnectServiceExtensionAbility(connection: number): Promise\<void> 675 676Disconnects 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. 677 678**System capability**: SystemCapability.Ability.AbilityRuntime.Core 679 680**Parameters** 681 682| Name| Type| Mandatory| Description| 683| -------- | -------- | -------- | -------- | 684| connection | number | Yes| Digital code of the connected ServiceExtensionAbility, that is, connectionId returned by **connectServiceExtensionAbility**.| 685 686**Return value** 687 688| Type| Description| 689| -------- | -------- | 690| Promise\<void> | Promise that returns no value.| 691 692**Error codes** 693 694For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 695 696| ID| Error Message| 697| ------- | -------------------------------- | 698| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 699| 16000011 | The context does not exist. | 700| 16000050 | Internal error. | 701 702**Example** 703 704```ts 705import { UIExtensionAbility } from '@kit.AbilityKit'; 706import { rpc } from '@kit.IPCKit'; 707import { BusinessError } from '@kit.BasicServicesKit'; 708 709export default class EntryAbility extends UIExtensionAbility { 710 onForeground() { 711 // connection is the return value of connectServiceExtensionAbility. 712 let connection = 1; 713 let commRemote: rpc.IRemoteObject | null; 714 715 try { 716 this.context.disconnectServiceExtensionAbility(connection).then(() => { 717 commRemote = null; 718 // Carry out normal service processing. 719 console.info('disconnectServiceExtensionAbility succeed'); 720 }).catch((err: BusinessError) => { 721 // Process service logic errors. 722 console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); 723 }) 724 } catch (err) { 725 commRemote = null; 726 // Process input parameter errors. 727 let code = (err as BusinessError).code; 728 let message = (err as BusinessError).message; 729 console.error(`disconnectServiceExtensionAbility failed, code is ${code}, message is ${message}`); 730 } 731 } 732} 733``` 734 735## UIExtensionContext.disconnectServiceExtensionAbility 736 737disconnectServiceExtensionAbility(connection: number, callback: AsyncCallback\<void>): void 738 739Disconnects 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. 740 741**System capability**: SystemCapability.Ability.AbilityRuntime.Core 742 743**Parameters** 744 745| Name| Type| Mandatory| Description| 746| -------- | -------- | -------- | -------- | 747| connection | number | Yes| Digital code of the connected ServiceExtensionAbility, that is, connectionId returned by **connectServiceExtensionAbility**.| 748| callback | AsyncCallback\<void> | Yes| Callback used to return the result. If the disconnection is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 749 750**Error codes** 751 752For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 753 754| ID| Error Message| 755| ------- | -------------------------------- | 756| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 757| 16000011 | The context does not exist. | 758| 16000050 | Internal error. | 759 760**Example** 761 762```ts 763import { UIExtensionAbility } from '@kit.AbilityKit'; 764import { rpc } from '@kit.IPCKit'; 765import { BusinessError } from '@kit.BasicServicesKit'; 766 767export default class EntryAbility extends UIExtensionAbility { 768 onForeground() { 769 // connection is the return value of connectServiceExtensionAbility. 770 let connection = 1; 771 let commRemote: rpc.IRemoteObject | null; 772 773 try { 774 this.context.disconnectServiceExtensionAbility(connection, (err: BusinessError) => { 775 commRemote = null; 776 if (err.code) { 777 // Process service logic errors. 778 console.error(`disconnectServiceExtensionAbility failed, code is ${err.code}, message is ${err.message}`); 779 return; 780 } 781 // Carry out normal service processing. 782 console.info('disconnectServiceExtensionAbility succeed'); 783 }); 784 } catch (err) { 785 commRemote = null; 786 // Process input parameter errors. 787 let code = (err as BusinessError).code; 788 let message = (err as BusinessError).message; 789 console.error(`disconnectServiceExtensionAbility failed, code is ${code}, message is ${message}`); 790 } 791 } 792} 793``` 794 795## UIExtensionContext.terminateSelf<sup>12+</sup> 796 797terminateSelf(callback: AsyncCallback<void>): void 798 799Stops the window object corresponding to this UIExtensionContext. This API uses an asynchronous callback to return the result. 800 801**System capability**: SystemCapability.Ability.AbilityRuntime.Core 802 803**Parameters** 804 805| Name | Type | Mandatory| Description | 806| -------- | ------------------------- | ---- | ------------------------------------------------------------ | 807| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the window object is stopped, **err** is **undefined**; otherwise, **err** is an error object.| 808 809**Error codes** 810 811For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 812 813| ID| Error Message| 814| ------- | -------- | 815| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 816 817**Example** 818 819```ts 820import { UIExtensionAbility } from '@kit.AbilityKit'; 821import { BusinessError } from '@kit.BasicServicesKit'; 822 823export default class EntryAbility extends UIExtensionAbility { 824 onForeground() { 825 try { 826 this.context.terminateSelf((err: BusinessError) => { 827 if (err.code) { 828 // Process service logic errors. 829 console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`); 830 return; 831 } 832 // Carry out normal service processing. 833 console.info('terminateSelf succeed'); 834 }); 835 } catch (err) { 836 // Capture the synchronization parameter error. 837 let code = (err as BusinessError).code; 838 let message = (err as BusinessError).message; 839 console.error(`terminateSelf failed, code is ${code}, message is ${message}`); 840 } 841 } 842} 843``` 844 845## UIExtensionContext.terminateSelf<sup>12+</sup> 846 847terminateSelf(): Promise<void> 848 849Stops the window object corresponding to this UIExtensionContext. This API uses a promise to return the result. 850 851**System capability**: SystemCapability.Ability.AbilityRuntime.Core 852 853**Return value** 854 855| Type | Description | 856| ------------------- | -------------------------------------- | 857| Promise<void> | Promise that returns no value.| 858 859**Example** 860 861```ts 862import { UIExtensionAbility } from '@kit.AbilityKit'; 863import { BusinessError } from '@kit.BasicServicesKit'; 864 865export default class EntryAbility extends UIExtensionAbility { 866 onForeground() { 867 try { 868 this.context.terminateSelf() 869 .then(() => { 870 // Carry out normal service processing. 871 console.info('terminateSelf succeed'); 872 }) 873 .catch((err: BusinessError) => { 874 // Process service logic errors. 875 console.error(`terminateSelf failed, code is ${err.code}, message is ${err.message}`); 876 }); 877 } catch (err) { 878 // Capture the synchronization parameter error. 879 let code = (err as BusinessError).code; 880 let message = (err as BusinessError).message; 881 console.error(`terminateSelf failed, code is ${code}, message is ${message}`); 882 } 883 } 884} 885``` 886 887## UIExtensionContext.terminateSelfWithResult<sup>12+</sup> 888 889terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback<void>): void 890 891Stops the window object corresponding to this UIExtensionContext and returns the result to the UIExtensionComponent. This API uses an asynchronous callback to return the result. 892 893**System capability**: SystemCapability.Ability.AbilityRuntime.Core 894 895**Parameters** 896 897| Name | Type | Mandatory| Description | 898| --------- | ------------------------------------------------------- | ---- | ------------------------------------------------------ | 899| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes | Result returned to the UIExtensionComponent. | 900| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the window object is stopped, **err** is **undefined**; otherwise, **err** is an error object.| 901 902**Error codes** 903 904For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 905 906| ID| Error Message| 907| ------- | -------- | 908| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 909 910**Example** 911 912```ts 913import { UIExtensionAbility, Want, common } from '@kit.AbilityKit'; 914import { BusinessError } from '@kit.BasicServicesKit'; 915 916export default class EntryAbility extends UIExtensionAbility { 917 onForeground() { 918 let want: Want = { 919 bundleName: 'com.example.myapplication', 920 abilityName: 'EntryAbility' 921 }; 922 let resultCode = 100; 923 // AbilityResult information returned to the caller. 924 let abilityResult: common.AbilityResult = { 925 want, 926 resultCode 927 }; 928 929 try { 930 this.context.terminateSelfWithResult(abilityResult, (err: BusinessError) => { 931 if (err.code) { 932 // Process service logic errors. 933 console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`); 934 return; 935 } 936 // Carry out normal service processing. 937 console.info('terminateSelfWithResult succeed'); 938 }); 939 } catch (err) { 940 // Process input parameter errors. 941 let code = (err as BusinessError).code; 942 let message = (err as BusinessError).message; 943 console.error(`terminateSelfWithResult failed, code is ${code}, message is ${message}`); 944 } 945 } 946} 947``` 948 949## UIExtensionContext.terminateSelfWithResult<sup>12+</sup> 950 951terminateSelfWithResult(parameter: AbilityResult): Promise<void> 952 953Stops the window object corresponding to this UIExtensionContext and returns the result to the UIExtensionComponent. This API uses a promise to return the result. 954 955**System capability**: SystemCapability.Ability.AbilityRuntime.Core 956 957**Parameters** 958 959| Name | Type | Mandatory| Description | 960| --------- | ------------------------------------------------------- | ---- | -------------------------------------- | 961| parameter | [AbilityResult](js-apis-inner-ability-abilityResult.md) | Yes | Result returned to the UIExtensionComponent.| 962 963**Return value** 964 965| Type | Description | 966| ------------------- | -------------------------------------- | 967| Promise<void> | Promise that returns no value.| 968 969**Error codes** 970 971For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 972 973| ID| Error Message| 974| ------- | -------- | 975| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 976 977```ts 978import { UIExtensionAbility, Want, common } from '@kit.AbilityKit'; 979import { BusinessError } from '@kit.BasicServicesKit'; 980 981export default class EntryAbility extends UIExtensionAbility { 982 onForeground() { 983 let want: Want = { 984 bundleName: 'com.example.myapplication', 985 abilityName: 'EntryAbility' 986 }; 987 let resultCode = 100; 988 // AbilityResult information returned to the caller. 989 let abilityResult: common.AbilityResult = { 990 want, 991 resultCode 992 }; 993 994 try { 995 this.context.terminateSelfWithResult(abilityResult) 996 .then(() => { 997 // Carry out normal service processing. 998 console.info('terminateSelfWithResult succeed'); 999 }) 1000 .catch((err: BusinessError) => { 1001 // Process service logic errors. 1002 console.error(`terminateSelfWithResult failed, code is ${err.code}, message is ${err.message}`); 1003 }); 1004 } catch (err) { 1005 // Process input parameter errors. 1006 let code = (err as BusinessError).code; 1007 let message = (err as BusinessError).message; 1008 console.error(`terminateSelfWithResult failed, code is ${code}, message is ${message}`); 1009 } 1010 } 1011} 1012``` 1013 1014## UIExtensionContext.reportDrawnCompleted<sup>12+<sup> 1015 1016reportDrawnCompleted(callback: AsyncCallback\<void>): void 1017 1018Reports an event indicating that page loading is complete (**onSessionCreate()** is successfully called). This API uses an asynchronous callback to return the result. 1019 1020**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1021 1022**Parameters** 1023 1024| Name| Type| Mandatory| Description| 1025| -------- | -------- | -------- | -------- | 1026| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the event is reported, **err** is **undefined**; otherwise, **err** is an error object.| 1027 1028**Error codes** 1029 1030For details about the error codes, see [Ability Error Codes](errorcode-ability.md). 1031 1032| ID| Error Message| 1033| ------- | -------------------------------- | 1034| 16000011 | The context does not exist. | 1035| 16000050 | Internal error. | 1036 1037**Example** 1038 1039```ts 1040import { UIExtensionAbility, Want, UIExtensionContentSession } from '@kit.AbilityKit'; 1041import { BusinessError } from '@kit.BasicServicesKit'; 1042 1043const TAG: string = '[testTag] UIExtAbility'; 1044 1045export default class UIExtAbility extends UIExtensionAbility { 1046 onSessionCreate(want: Want, session: UIExtensionContentSession) { 1047 console.info(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`); 1048 let data: Record<string, UIExtensionContentSession> = { 1049 'session': session 1050 }; 1051 let storage: LocalStorage = new LocalStorage(data); 1052 session.loadContent('pages/extension', storage); 1053 try { 1054 this.context.reportDrawnCompleted((err) => { 1055 if (err.code) { 1056 // Process service logic errors. 1057 console.error(`reportDrawnCompleted failed, code is ${err.code}, message is ${err.message}`); 1058 return; 1059 } 1060 // Carry out normal service processing. 1061 console.info('reportDrawnCompleted succeed'); 1062 }); 1063 } catch (err) { 1064 // Capture the synchronization parameter error. 1065 let code = (err as BusinessError).code; 1066 let message = (err as BusinessError).message; 1067 console.error(`reportDrawnCompleted failed, code is ${code}, message is ${message}`); 1068 } 1069 } 1070} 1071``` 1072 1073## UIExtensionContext.openAtomicService<sup>12+<sup> 1074openAtomicService(appId: string, options?: AtomicServiceOptions): Promise<AbilityResult> 1075 1076Starts an [EmbeddableUIAbility](js-apis-app-ability-embeddableUIAbility.md) in jump-out mode and returns the result. This API uses a promise to return the result. 1077The following situations may be possible for a started EmbeddableUIAbility: 1078 - Normally, you can call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#uiabilitycontextterminateselfwithresult) to terminate the EmbeddableUIAbility. The result is returned to the caller. 1079 - If an exception occurs, for example, the EmbeddableUIAbility is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 1080 - If different applications call this API to start an EmbeddableUIAbility and then call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#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. 1081 1082> **NOTE** 1083> 1084> 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). 1085 1086**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1087 1088**Parameters** 1089 1090| Name| Type| Mandatory| Description| 1091| -------- | -------- | -------- | -------- | 1092| appId | string | Yes| Unique ID of the application, which is allocated by the cloud.| 1093| options | [AtomicServiceOptions](js-apis-app-ability-atomicServiceOptions.md) | No| Parameter carried in the request for starting the atomic service in jump-out mode.| 1094 1095 1096**Return value** 1097 1098| Type| Description| 1099| -------- | -------- | 1100| 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.| 1101 1102**Error codes** 1103 1104For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1105 1106| ID| Error Message| 1107| ------- | -------------------------------- | 1108| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1109| 16000002 | Incorrect ability type. | 1110| 16000003 | The specified ID does not exist. | 1111| 16000004 | Failed to start the invisible ability. | 1112| 16000011 | The context does not exist. | 1113| 16000012 | The application is controlled. | 1114| 16000050 | Internal error. | 1115| 16000069 | The extension cannot start the third party application. | 1116| 16200001 | The caller has been released. | 1117 1118 1119**Example** 1120 1121```ts 1122import { UIExtensionAbility, common, AtomicServiceOptions } from '@kit.AbilityKit'; 1123import { BusinessError } from '@kit.BasicServicesKit'; 1124 1125export default class EntryAbility extends UIExtensionAbility { 1126 onForeground() { 1127 let appId: string = '6918661953712445909'; 1128 let options: AtomicServiceOptions = { 1129 displayId: 0, 1130 }; 1131 1132 try { 1133 this.context.openAtomicService(appId, options) 1134 .then((result: common.AbilityResult) => { 1135 // Carry out normal service processing. 1136 console.info('openAtomicService succeed'); 1137 }) 1138 .catch((err: BusinessError) => { 1139 // Process service logic errors. 1140 console.error(`openAtomicService failed, code is ${err.code}, message is ${err.message}`); 1141 }); 1142 } catch (err) { 1143 // Process input parameter errors. 1144 let code = (err as BusinessError).code; 1145 let message = (err as BusinessError).message; 1146 console.error(`openAtomicService failed, code is ${code}, message is ${message}`); 1147 } 1148 } 1149} 1150``` 1151 1152## UIExtensionContext.openLink<sup>12+<sup> 1153openLink(link:string, options?: OpenLinkOptions, callback?: AsyncCallback<AbilityResult>): Promise<void> 1154 1155Starts a UIAbility through App Linking. This API uses a promise to return the result. 1156 1157A 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: 1158- The **actions** field contains **ohos.want.action.viewData**. 1159- The **entities** field contains **entity.system.browsable**. 1160- The **uris** field contains elements whose **scheme** is **https** and **domainVerify** is **true**. 1161 1162If 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](#uiextensioncontextstartabilityforresult). 1163If 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. 1164 1165> **NOTE** 1166> 1167> 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). 1168 1169**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1170 1171**Parameters** 1172 1173| Name| Type| Mandatory| Description| 1174| -------- | -------- | -------- | -------- | 1175| link | string | Yes| URL to open, which must be in the standard format.| 1176| options | [OpenLinkOptions](js-apis-app-ability-openLinkOptions.md) | No| Options of the URL.| 1177| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | No| Callback used to return the result.| 1178 1179**Return value** 1180 1181| Type| Description| 1182| -------- | -------- | 1183| Promise<void> | Promise that returns no value.| 1184 1185**Error codes** 1186 1187For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1188 1189| ID| Error Message| 1190| ------- | -------------------------------- | 1191| 201 | The application does not have permission to call the interface. | 1192| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 1193| 16000001 | The specified ability does not exist. | 1194| 16000002 | Incorrect ability type. | 1195| 16000004 | Failed to start the invisible ability. | 1196| 16000005 | The specified process does not have the permission. | 1197| 16000006 | Cross-user operations are not allowed. | 1198| 16000008 | The crowdtesting application expires. | 1199| 16000009 | An ability cannot be started or stopped in Wukong mode. | 1200| 16000010 | The call with the continuation flag is forbidden. | 1201| 16000011 | The context does not exist. | 1202| 16000012 | The application is controlled. | 1203| 16000013 | The application is controlled by EDM. | 1204| 16000019 | No matching ability is found. | 1205| 16000069 | The extension cannot start the third party application. | 1206| 16200001 | The caller has been released. | 1207| 16000053 | The ability is not on the top of the UI. | 1208| 16000082 | The UIAbility is being started. | 1209 1210**Example** 1211 1212```ts 1213import { UIExtensionAbility, Want, UIExtensionContentSession, OpenLinkOptions } from '@kit.AbilityKit'; 1214import { BusinessError } from '@kit.BasicServicesKit'; 1215 1216function log(info: string) { 1217 console.error(`MyUIExtension:: ${JSON.stringify(info)}`); 1218} 1219 1220export default class UIExtAbility extends UIExtensionAbility { 1221 onCreate() { 1222 log(`UIExtAbility onCreate`); 1223 } 1224 1225 onForeground() { 1226 log(`UIExtAbility onForeground`); 1227 } 1228 1229 onBackground() { 1230 log(`UIExtAbility onBackground`); 1231 } 1232 1233 onDestroy() { 1234 log(`UIExtAbility onDestroy`); 1235 } 1236 1237 onSessionCreate(want: Want, session: UIExtensionContentSession) { 1238 log(`UIExtAbility onSessionCreate`); 1239 log(`UIExtAbility onSessionCreate, want: ${JSON.stringify(want)}`); 1240 let record: Record<string, UIExtensionContentSession> = { 1241 'session': session 1242 }; 1243 let storage: LocalStorage = new LocalStorage(record); 1244 session.loadContent('pages/UIExtensionIndex', storage); 1245 1246 let link: string = 'https://www.example.com'; 1247 let openLinkOptions: OpenLinkOptions = { 1248 appLinkingOnly: true 1249 }; 1250 try { 1251 this.context.openLink( 1252 link, 1253 openLinkOptions, 1254 (err, result) => { 1255 log(`openLink callback error.code: ${JSON.stringify(err)}`); 1256 log(`openLink callback result: ${JSON.stringify(result.resultCode)}`); 1257 log(`openLink callback result data: ${JSON.stringify(result.want)}`); 1258 } 1259 ).then(() => { 1260 log(`open link success.`); 1261 }).catch((err: BusinessError) => { 1262 log(`open link failed, errCode ${JSON.stringify(err.code)}`); 1263 }); 1264 } 1265 catch (e) { 1266 log(`exception occured, errCode ${JSON.stringify(e.code)}`); 1267 } 1268 1269 } 1270 1271 onSessionDestroy(session: UIExtensionContentSession) { 1272 log(`UIExtAbility onSessionDestroy`); 1273 } 1274} 1275``` 1276 1277## UIExtensionContext.startUIServiceExtensionAbility<sup>13+<sup> 1278startUIServiceExtensionAbility(want: Want): Promise<void> 1279 1280Starts a UIServiceExtensionAbility. 1281 1282> **NOTE** 1283> 1284> 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). 1285> 1286 1287 1288**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1289 1290**Parameters** 1291 1292| Name | Type |Read Only| Optional| Description | 1293| -------- | ---------------------------------------------------------------------------- | ---- | ---- |------------------------- | 1294| want | [Want](js-apis-app-ability-want.md) | Yes| No | Want information for starting the UIServiceExtensionAbility.| 1295 1296**Return value** 1297 1298| Type | Description | 1299| ------------------- | -------------------------------------- | 1300| Promise<void> | Promise that returns no value.| 1301 1302**Error codes** 1303 1304For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1305 1306| ID| Error Message | 1307| -------- | ----------------------------------------------------------------------------------------------------------- | 1308| 201 | The application does not have permission to call the interface. | 1309| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1310| 801 | The Ability is not supported. | 1311| 16000001 | The specified ability does not exist. | 1312| 16000002 | Incorrect ability type. | 1313| 16000004 | Failed to start the invisible ability. | 1314| 16000005 | The specified process does not have the permission. | 1315| 16000006 | Cross-user operations are not allowed. | 1316| 16000008 | The crowdtesting application expires. | 1317| 16000011 | The context does not exist. | 1318| 16000012 | The application is controlled. | 1319| 16000013 | The application is controlled by EDM. | 1320| 16000050 | Internal error. | 1321| 16200001 | The caller has been released. | 1322 1323**Example** 1324 1325```ts 1326import { common, Want } from '@kit.AbilityKit'; 1327import { BusinessError } from '@kit.BasicServicesKit'; 1328 1329@Entry 1330@Component 1331struct Index { 1332 build() { 1333 Column() { 1334 Row() { 1335 // Create a Start button. 1336 Button('start ability') 1337 .enabled(true) 1338 .onClick(() => { 1339 let context = getContext(this) as common.UIExtensionContext; 1340 let startWant: Want = { 1341 bundleName: 'com.acts.uiserviceextensionability', 1342 abilityName: 'UiServiceExtAbility', 1343 }; 1344 try { 1345 // Start the UIServiceExtensionAbility. 1346 context.startUIServiceExtensionAbility(startWant).then(() => { 1347 console.log('startUIServiceExtensionAbility success'); 1348 }).catch((error: BusinessError) => { 1349 console.log('startUIServiceExtensionAbility error', JSON.stringify(error)); 1350 }) 1351 } catch (err) { 1352 console.log('startUIServiceExtensionAbility failed', JSON.stringify(err)); 1353 } 1354 }) 1355 } 1356 } 1357 } 1358} 1359``` 1360 1361## UIExtensionContext.connectUIServiceExtensionAbility<sup>13+<sup> 1362connectUIServiceExtensionAbility(want: Want, callback: UIServiceExtensionConnectCallback) : Promise<UIServiceProxy> 1363 1364Connects to a UIServiceExtensionAbility. 1365 1366> **NOTE** 1367> 1368> 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). 1369> 1370 1371 1372**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1373 1374**Parameters** 1375 1376| Name | Type | Read Only| Optional| Description | 1377| -------------------- | -------------------------------- | ---- | -------------------- | -------------------- | 1378| want | Want | Yes | No| Want information used for connection.| 1379| callback | [UIServiceExtensionConnectCallback](js-apis-inner-application-uiServiceExtensionconnectcallback.md) | Yes|No | Callback for connecting to the UIServiceExtensionAbility. | 1380 1381**Return value** 1382 1383| Type | Description | 1384| ----------------------- | -------------------- | 1385| Promise<UIServiceProxy> | When the UIServiceExtensionAbility is connected, a [UIServiceProxy](js-apis-inner-application-uiserviceproxy.md) object is returned, which can be used to send data to the UIServiceExtensionAbility.| 1386 1387**Error codes** 1388 1389For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1390 1391| ID| Error Message | 1392| -------- | ---------------------------------- | 1393| 201 | The application does not have permission to call the interface. | 1394| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1395| 16000001 | The specified ability does not | 1396| 16000002 | Incorrect ability type. | 1397| 16000004 | Failed to start the invisible ability. | 1398| 16000005 | The specified process does not have the permission. | 1399| 16000006 | Cross-user operations are not allowed. | 1400| 16000008 | The crowdtesting application expires. | 1401| 16000011 | The context does not exist. | 1402| 16000050 | Internal error. | 1403| 16000053 | The ability is not on the top of the UI. | 1404| 16000055 | Installation-free timed out. | 1405 1406**Example** 1407 1408```ts 1409import { common, Want } from '@kit.AbilityKit'; 1410import { BusinessError } from '@kit.BasicServicesKit'; 1411 1412@Entry 1413@Component 1414struct Page_UIServiceExtensionAbility { 1415 @State uiServiceProxy: common.UIServiceProxy | null = null; 1416 1417 build() { 1418 Column() { 1419 //... 1420 Row() { 1421 //... 1422 }.onClick(() => { 1423 const context = getContext(this) as common.UIExtensionContext; 1424 const want: Want = { 1425 deviceId: '', 1426 bundleName: 'com.example.myapplication', 1427 abilityName: '' 1428 }; 1429 // Define a callback. 1430 const callback: common.UIServiceExtensionConnectCallback = { 1431 onData: (data: Record<string, Object>): void => { 1432 console.log('onData:', JSON.stringify(data)); 1433 }, 1434 onDisconnect: (): void => { 1435 console.log('onDisconnect'); 1436 } 1437 }; 1438 // Connect to the UIServiceExtensionAbility. 1439 context.connectUIServiceExtensionAbility(want, callback).then((uiServiceProxy: common.UIServiceProxy) => { 1440 this.uiServiceProxy = uiServiceProxy; 1441 console.log('connectUIServiceExtensionAbility success'); 1442 }).catch((error: BusinessError) => { 1443 console.log('connectUIServiceExtensionAbility failed', JSON.stringify(error)); 1444 }) 1445 }) 1446 } 1447 } 1448} 1449``` 1450 1451## UIExtensionContext.disconnectUIServiceExtensionAbility<sup>13+<sup> 1452disconnectUIServiceExtensionAbility(proxy: UIServiceProxy): Promise<void> 1453 1454Disconnects a UIServiceExtensionAbility. 1455 1456 1457**System capability**: SystemCapability.Ability.AbilityRuntime.Core 1458 1459**Parameters** 1460 1461| Name | Type | Read Only| Optional| Description | 1462| -------------------- | -------------------------------- | ---- | -------------------- | -------------------- | 1463| proxy | [UIServiceProxy](js-apis-inner-application-uiserviceproxy.md) | Yes| No | Proxy used returned by calling [connectUIServiceExtensionAbility](#uiextensioncontextconnectuiserviceextensionability13).| 1464 1465**Return value** 1466 1467| Type | Description | 1468| ----------------------- | -------------------- | 1469| Promise<void> | Promise that returns no value.| 1470 1471**Error codes** 1472 1473For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 1474 1475| ID| Error Message | 1476| -------- | ------------------------------------------------------------------------------------------------ | 1477| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1478| 16000011 | The context does not exist. | 1479| 16000050 | Internal error. | 1480 1481**Example** 1482 1483```ts 1484import { common } from '@kit.AbilityKit'; 1485import { BusinessError } from '@kit.BasicServicesKit'; 1486 1487@Entry 1488@Component 1489struct Page_UIServiceExtensionAbility { 1490 @State uiServiceProxy: common.UIServiceProxy | null = null; 1491 1492 build() { 1493 Column() { 1494 //... 1495 Row() { 1496 //... 1497 }.onClick(() => { 1498 const context = getContext(this) as common.UIExtensionContext; 1499 // this.uiServiceProxy is the proxy object saved during connection. 1500 context.disconnectUIServiceExtensionAbility(this.uiServiceProxy).then(() => { 1501 console.log('disconnectUIServiceExtensionAbility success'); 1502 }).catch((error: BusinessError) => { 1503 console.log('disconnectUIServiceExtensionAbility failed', JSON.stringify(error)); 1504 }) 1505 }) 1506 } 1507 } 1508} 1509``` 1510