1# @ohos.app.ability.UIExtensionContentSession (UI Operation Class for ExtensionAbilities with UI) (System API) 2 3UIExtensionContentSession is an instance created when the [UIExtensionAbility](js-apis-app-ability-uiExtensionAbility.md) loads UI content. When the UIExtensionComponent starts a UIExtensionAbility, the UIExtensionAbility creates a UIExtensionContentSession instance and returns it through the [onSessionCreate](js-apis-app-ability-uiExtensionAbility.md#onsessioncreate) callback. One UIExtensionComponent corresponds to one UIExtensionContentSession instance, which provides methods such as UI loading and result notification. The UIExtensionContentSession instances of multiple UIExtensionAbilities are operated separately. 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> 9> The APIs of this module can be used only in the stage model. 10> 11> This topic describes only system APIs provided by the module. For details about its public APIs, see [@ohos.app.ability.UIExtensionContentSession (UI Operation Class for ExtensionAbilities with UI)](js-apis-app-ability-uiExtensionContentSession.md). 12 13## Modules to Import 14 15```ts 16import { UIExtensionContentSession } from '@kit.AbilityKit'; 17``` 18 19## UIExtensionContentSession 20 21### sendData 22 23sendData(data: Record\<string, Object>): void 24 25Sends data to the UIExtensionComponent. 26 27**System capability**: SystemCapability.Ability.AbilityRuntime.Core 28 29**System API**: This is a system API. 30 31**Parameters** 32 33| Name| Type| Mandatory| Description| 34| -------- | -------- | -------- | -------- | 35| data | Record\<string, Object> | Yes| Data to send.| 36 37**Error codes** 38 39For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 40 41| ID| Error Message| 42| ------- | -------------------------------- | 43| 202 | Not System App. Interface caller is not a system app. | 44| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 45| 16000050 | Internal error. | 46 47**Example** 48 49```ts 50import { UIExtensionContentSession } from '@kit.AbilityKit'; 51 52@Entry() 53@Component 54struct Index { 55 private storage: LocalStorage | undefined = this.getUIContext().getSharedLocalStorage(); 56 private session: UIExtensionContentSession | undefined = 57 this.storage?.get<UIExtensionContentSession>('session'); 58 59 build() { 60 RelativeContainer() { 61 Button('SendData') 62 .onClick(() => { 63 let data: Record<string, Object> = { 64 'number': 123456, 65 'message': 'test' 66 }; 67 68 try { 69 this.session?.sendData(data); 70 } catch (err) { 71 console.error('sendData err:' + JSON.stringify(err)); 72 } 73 }) 74 } 75 .height('100%') 76 .width('100%') 77 } 78} 79``` 80 81### setReceiveDataCallback 82 83setReceiveDataCallback(callback: (data: Record\<string, Object>) => void): void 84 85Sets a callback to receive data from the UIExtensionComponent. This API uses an asynchronous callback to return the result. 86 87**System capability**: SystemCapability.Ability.AbilityRuntime.Core 88 89**System API**: This is a system API. 90 91**Parameters** 92 93| Name| Type| Mandatory| Description| 94| -------- | -------- | -------- | -------- | 95| callback | (data: Record\<string, Object>) => void | Yes| Callback used to return the received data.| 96 97**Error codes** 98 99For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 100 101| ID| Error Message| 102| ------- | -------------------------------- | 103| 202 | Not System App. Interface caller is not a system app. | 104| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 105| 16000050 | Internal error. | 106 107**Example** 108 109```ts 110import { UIExtensionContentSession } from '@kit.AbilityKit'; 111 112@Entry() 113@Component 114struct Index { 115 storage: LocalStorage | undefined = this.getUIContext().getSharedLocalStorage(); 116 private session: UIExtensionContentSession | undefined = 117 this.storage?.get<UIExtensionContentSession>('session'); 118 119 build() { 120 RelativeContainer() { 121 Button('SendData') 122 .onClick(() => { 123 this.session?.setReceiveDataCallback((data: Record<string, Object>) => { 124 console.info(`Succeeded in setReceiveDataCallback, data: ${JSON.stringify(data)}`); 125 }); 126 }) 127 } 128 .height('100%') 129 .width('100%') 130 } 131} 132``` 133 134### setReceiveDataForResultCallback<sup>11+</sup> 135 136setReceiveDataForResultCallback(callback: (data: Record<string, Object>) => Record<string, Object>): void 137 138Sets a callback with a return value to receive data from the UIExtensionComponent. This API uses an asynchronous callback to return the result. 139 140**System API**: This is a system API. 141 142**System capability**: SystemCapability.Ability.AbilityRuntime.Core 143 144 145**Parameters** 146 147| Name| Type| Mandatory| Description | 148| -------- | -------- | -------- |----------------| 149| callback | (data: Record\<string, Object>) => Record\<string, Object> | Yes| Callback used to return the received data with a return value.| 150 151**Error codes** 152 153For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 154 155| ID| Error Message| 156| ------- | -------------------------------- | 157| 202 | Not System App. Interface caller is not a system app. | 158| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 159| 16000050 | Internal error. | 160 161**Example** 162 163```ts 164import { UIExtensionContentSession } from '@kit.AbilityKit'; 165 166@Entry() 167@Component 168struct Index { 169 storage: LocalStorage | undefined = this.getUIContext().getSharedLocalStorage(); 170 private session: UIExtensionContentSession | undefined = 171 this.storage?.get<UIExtensionContentSession>('session'); 172 173 build() { 174 RelativeContainer() { 175 Button('SetReceiveDataForResultCallback') 176 .onClick(() => { 177 this.session?.setReceiveDataForResultCallback((data: Record<string, Object>) => { 178 console.info(`Succeeded in setReceiveDataCallback, data: ${JSON.stringify(data)}`); 179 return data; 180 }); 181 }) 182 } 183 .height('100%') 184 .width('100%') 185 } 186} 187``` 188 189### startAbility 190 191startAbility(want: Want, callback: AsyncCallback<void>): void 192 193Starts an ability. This API uses an asynchronous callback to return the result. 194 195> **NOTE** 196> 197> 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). 198> The application where the UIExtensionComponent is located must be running in the foreground and gain focus. 199 200**System capability**: SystemCapability.Ability.AbilityRuntime.Core 201 202**System API**: This is a system API. 203 204**Parameters** 205 206| Name| Type| Mandatory| Description| 207| -------- | -------- | -------- | -------- | 208| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 209| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 210 211**Error codes** 212 213For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 214 215| ID| Error Message| 216| ------- | -------------------------------- | 217| 201 | The application does not have permission to call the interface. | 218| 202 | Not System App. Interface caller is not a system app. | 219| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 220| 16000001 | The specified ability does not exist. | 221| 16000002 | Incorrect ability type. | 222| 16000004 | Cannot start an invisible component. | 223| 16000005 | The specified process does not have the permission. | 224| 16000006 | Cross-user operations are not allowed. | 225| 16000008 | The crowdtesting application expires. | 226| 16000009 | An ability cannot be started or stopped in Wukong mode. | 227| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 228| 16000011 | The context does not exist. | 229| 16000012 | The application is controlled. | 230| 16000013 | The application is controlled by EDM. | 231| 16000050 | Internal error. | 232| 16000053 | The ability is not on the top of the UI. | 233| 16000055 | Installation-free timed out. | 234| 16200001 | The caller has been released. | 235 236**Example** 237 238```ts 239import { UIExtensionContentSession, UIExtensionAbility, Want } from '@kit.AbilityKit'; 240import { BusinessError } from '@kit.BasicServicesKit'; 241 242export default class UIExtAbility extends UIExtensionAbility { 243 // ... 244 245 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 246 session.startAbility(want, (err: BusinessError) => { 247 if (err) { 248 console.error(`Failed to startAbility, code: ${err.code}, msg: ${err.message}`); 249 return; 250 } 251 console.info(`Succeeded in startAbility`); 252 }) 253 } 254 255 // ... 256} 257``` 258 259### startAbility 260 261startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void 262 263Starts an ability with **options** specified. This API uses an asynchronous callback to return the result. 264 265> **NOTE** 266> 267> 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). 268> The application where the UIExtensionComponent is located must be running in the foreground and gain focus. 269 270**System capability**: SystemCapability.Ability.AbilityRuntime.Core 271 272**System API**: This is a system API. 273 274**Parameters** 275 276| Name| Type| Mandatory| Description| 277| -------- | -------- | -------- | -------- | 278| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 279| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 280| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the ability is started, **err** is **undefined**; otherwise, **err** is an error object.| 281 282**Error codes** 283 284For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 285 286| ID| Error Message| 287| ------- | -------------------------------- | 288| 201 | The application does not have permission to call the interface. | 289| 202 | Not System App. Interface caller is not a system app. | 290| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 291| 16000001 | The specified ability does not exist. | 292| 16000004 | Cannot start an invisible component. | 293| 16000005 | The specified process does not have the permission. | 294| 16000006 | Cross-user operations are not allowed. | 295| 16000008 | The crowdtesting application expires. | 296| 16000009 | An ability cannot be started or stopped in Wukong mode. | 297| 16000011 | The context does not exist. | 298| 16000012 | The application is controlled. | 299| 16000013 | The application is controlled by EDM. | 300| 16000050 | Internal error. | 301| 16000053 | The ability is not on the top of the UI. | 302| 16000055 | Installation-free timed out. | 303| 16200001 | The caller has been released. | 304 305**Example** 306 307```ts 308import { UIExtensionContentSession, UIExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 309import { BusinessError } from '@kit.BasicServicesKit'; 310 311export default class UIExtAbility extends UIExtensionAbility { 312 // ... 313 314 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 315 let startOptions: StartOptions = { 316 displayId: 0 317 }; 318 319 session.startAbility(want, startOptions, (err: BusinessError) => { 320 if (err) { 321 console.error(`Failed to startAbility, code: ${err.code}, msg: ${err.message}`); 322 return; 323 } 324 console.info(`Succeeded in startAbility`); 325 }) 326 } 327 328 // ... 329} 330``` 331 332### startAbility 333 334startAbility(want: Want, options?: StartOptions): Promise<void> 335 336Starts an ability. This API uses a promise to return the result. 337 338> **NOTE** 339> 340> 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). 341> The application where the UIExtensionComponent is located must be running in the foreground and gain focus. 342 343**System capability**: SystemCapability.Ability.AbilityRuntime.Core 344 345**System API**: This is a system API. 346 347**Parameters** 348 349| Name| Type| Mandatory| Description| 350| -------- | -------- | -------- | -------- | 351| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 352| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 353 354**Return value** 355 356| Type| Description| 357| -------- | -------- | 358| Promise<void> | Promise that returns no value.| 359 360**Error codes** 361 362For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 363 364| ID| Error Message| 365| ------- | -------------------------------- | 366| 201 | The application does not have permission to call the interface. | 367| 202 | Not System App. Interface caller is not a system app. | 368| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 369| 16000001 | The specified ability does not exist. | 370| 16000002 | Incorrect ability type. | 371| 16000004 | Cannot start an invisible component. | 372| 16000005 | The specified process does not have the permission. | 373| 16000006 | Cross-user operations are not allowed. | 374| 16000008 | The crowdtesting application expires. | 375| 16000009 | An ability cannot be started or stopped in Wukong mode. | 376| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 377| 16000011 | The context does not exist. | 378| 16000012 | The application is controlled. | 379| 16000013 | The application is controlled by EDM. | 380| 16000050 | Internal error. | 381| 16000053 | The ability is not on the top of the UI. | 382| 16000055 | Installation-free timed out. | 383| 16200001 | The caller has been released. | 384 385**Example** 386 387```ts 388import { UIExtensionContentSession, UIExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 389import { BusinessError } from '@kit.BasicServicesKit'; 390 391export default class UIExtAbility extends UIExtensionAbility { 392 // ... 393 394 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 395 let startOptions: StartOptions = { 396 displayId: 0 397 }; 398 399 session.startAbility(want, startOptions) 400 .then(() => { 401 console.info(`Succeeded in startAbility`); 402 }) 403 .catch((err: BusinessError) => { 404 console.error(`Failed to startAbility, code: ${err.code}, msg: ${err.message}`); 405 }); 406 } 407 408 // ... 409} 410``` 411 412### startAbilityForResult 413 414startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): void 415 416Starts an ability and returns the result to the caller after the ability is terminated. This API uses an asynchronous callback to return the result. 417 418An ability can be terminated in the following ways: 419 - Normally, you can call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#terminateselfwithresult) to terminate the ability. The result is returned to the caller. 420 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 421 - 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#terminateselfwithresult) 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. 422 423> **NOTE** 424> 425> 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). 426> The application where the UIExtensionComponent is located must be running in the foreground and gain focus. 427 428**System capability**: SystemCapability.Ability.AbilityRuntime.Core 429 430**System API**: This is a system API. 431 432**Parameters** 433 434| Name| Type| Mandatory| Description| 435| -------- | -------- | -------- | -------- | 436| want |[Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 437| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result. If the ability is started and terminated, **err** is **undefined** and **data** is the obtained result code and data; otherwise, **err** is an error object.| 438 439**Error codes** 440 441For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 442 443| ID| Error Message| 444| ------- | -------------------------------- | 445| 201 | The application does not have permission to call the interface. | 446| 202 | Not System App. Interface caller is not a system app. | 447| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 448| 16000001 | The specified ability does not exist. | 449| 16000002 | Incorrect ability type. | 450| 16000004 | Cannot start an invisible component. | 451| 16000005 | The specified process does not have the permission. | 452| 16000006 | Cross-user operations are not allowed. | 453| 16000008 | The crowdtesting application expires. | 454| 16000009 | An ability cannot be started or stopped in Wukong mode. | 455| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 456| 16000011 | The context does not exist. | 457| 16000012 | The application is controlled. | 458| 16000013 | The application is controlled by EDM. | 459| 16000050 | Internal error. | 460| 16000053 | The ability is not on the top of the UI. | 461| 16000055 | Installation-free timed out. | 462| 16200001 | The caller has been released. | 463 464**Example** 465 466```ts 467import { UIExtensionContentSession, UIExtensionAbility, Want, common } from '@kit.AbilityKit'; 468import { BusinessError } from '@kit.BasicServicesKit'; 469 470export default class UIExtAbility extends UIExtensionAbility { 471 // ... 472 473 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 474 session.startAbilityForResult(want, (err: BusinessError, data: common.AbilityResult) => { 475 if (err) { 476 console.error(`Failed to startAbilityForResult, code: ${err.code}, msg: ${err.message}`); 477 return; 478 } 479 console.info(`Succeeded in startAbilityForResult, data: ${JSON.stringify(data)}`); 480 }) 481 } 482 483 // ... 484} 485``` 486 487### startAbilityForResult 488 489startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void 490 491Starts an ability with **options** specified and returns the result to the caller after the ability is terminated. This API uses an asynchronous callback to return the result. 492 493An ability can be terminated in the following ways: 494 - Normally, you can call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#terminateselfwithresult) to terminate the ability. The result is returned to the caller. 495 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 496 - 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#terminateselfwithresult) 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. 497 498> **NOTE** 499> 500> 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). 501> The application where the UIExtensionComponent is located must be running in the foreground and gain focus. 502 503**System capability**: SystemCapability.Ability.AbilityRuntime.Core 504 505**System API**: This is a system API. 506 507**Parameters** 508 509| Name| Type| Mandatory| Description| 510| -------- | -------- | -------- | -------- | 511| want |[Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 512| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 513| callback | AsyncCallback<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Yes| Callback used to return the result. If the ability is started and terminated, **err** is **undefined** and **data** is the obtained result code and data; otherwise, **err** is an error object.| 514 515**Error codes** 516 517For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 518 519| ID| Error Message| 520| ------- | -------------------------------- | 521| 201 | The application does not have permission to call the interface. | 522| 202 | Not System App. Interface caller is not a system app. | 523| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 524| 16000001 | The specified ability does not exist. | 525| 16000004 | Cannot start an invisible component. | 526| 16000005 | The specified process does not have the permission. | 527| 16000006 | Cross-user operations are not allowed. | 528| 16000008 | The crowdtesting application expires. | 529| 16000009 | An ability cannot be started or stopped in Wukong mode. | 530| 16000011 | The context does not exist. | 531| 16000012 | The application is controlled. | 532| 16000013 | The application is controlled by EDM. | 533| 16000050 | Internal error. | 534| 16000053 | The ability is not on the top of the UI. | 535| 16000055 | Installation-free timed out. | 536| 16200001 | The caller has been released. | 537 538**Example** 539 540```ts 541import { UIExtensionContentSession, UIExtensionAbility, Want, StartOptions, common } from '@kit.AbilityKit'; 542import { BusinessError } from '@kit.BasicServicesKit'; 543 544export default class UIExtAbility extends UIExtensionAbility { 545 // ... 546 547 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 548 let startOptions: StartOptions = { 549 displayId: 0 550 }; 551 552 session.startAbilityForResult(want, startOptions, (err: BusinessError, data: common.AbilityResult) => { 553 if (err) { 554 console.error(`Failed to startAbilityForResult, code: ${err.code}, msg: ${err.message}`); 555 return; 556 } 557 console.info(`Succeeded in startAbilityForResult, data: ${JSON.stringify(data)}`); 558 }) 559 } 560 561 // ... 562} 563``` 564 565### startAbilityForResult 566 567startAbilityForResult(want: Want, options?: StartOptions): Promise<AbilityResult> 568 569Starts an ability and returns the result to the caller after the ability is terminated. This API uses a promise to return the result. 570 571An ability can be terminated in the following ways: 572 - Normally, you can call [terminateSelfWithResult](js-apis-inner-application-uiAbilityContext.md#terminateselfwithresult) to terminate the ability. The result is returned to the caller. 573 - If an exception occurs, for example, the ability is killed, an error message, in which **resultCode** is **-1**, is returned to the caller. 574 - 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#terminateselfwithresult) 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. 575 576> **NOTE** 577> 578> 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). 579> The application where the UIExtensionComponent is located must be running in the foreground and gain focus. 580 581**System capability**: SystemCapability.Ability.AbilityRuntime.Core 582 583**System API**: This is a system API. 584 585**Parameters** 586 587| Name| Type| Mandatory| Description| 588| -------- | -------- | -------- | -------- | 589| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 590| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 591 592 593**Return value** 594 595| Type| Description| 596| -------- | -------- | 597| Promise<[AbilityResult](js-apis-inner-ability-abilityResult.md)> | Promise used to return the result code and data.| 598 599**Error codes** 600 601For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 602 603| ID| Error Message| 604| ------- | -------------------------------- | 605| 201 | The application does not have permission to call the interface. | 606| 202 | Not System App. Interface caller is not a system app. | 607| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 608| 16000001 | The specified ability does not exist. | 609| 16000002 | Incorrect ability type. | 610| 16000004 | Cannot start an invisible component. | 611| 16000005 | The specified process does not have the permission. | 612| 16000006 | Cross-user operations are not allowed. | 613| 16000008 | The crowdtesting application expires. | 614| 16000009 | An ability cannot be started or stopped in Wukong mode. | 615| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 616| 16000011 | The context does not exist. | 617| 16000012 | The application is controlled. | 618| 16000013 | The application is controlled by EDM. | 619| 16000050 | Internal error. | 620| 16000053 | The ability is not on the top of the UI. | 621| 16000055 | Installation-free timed out. | 622| 16200001 | The caller has been released. | 623 624**Example** 625 626```ts 627import { UIExtensionContentSession, UIExtensionAbility, Want, StartOptions, common } from '@kit.AbilityKit'; 628import { BusinessError } from '@kit.BasicServicesKit'; 629 630export default class UIExtAbility extends UIExtensionAbility { 631 // ... 632 633 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 634 let startOptions: StartOptions = { 635 displayId: 0 636 }; 637 638 session.startAbilityForResult(want, startOptions) 639 .then((data: common.AbilityResult) => { 640 console.info(`Succeeded in startAbilityForResult, data: ${JSON.stringify(data)}`); 641 }) 642 .catch((err: BusinessError) => { 643 console.error(`Failed to startAbilityForResult, code: ${err.code}, msg: ${err.message}`); 644 }); 645 } 646 647 // ... 648} 649``` 650 651### setWindowBackgroundColor 652 653setWindowBackgroundColor(color: string): void 654 655Sets the background color for the loading page of the UIExtensionAbility. This API can be used only after [loadContent()](js-apis-app-ability-uiExtensionContentSession.md#loadcontent) is called and takes effect. 656 657**System capability**: SystemCapability.Ability.AbilityRuntime.Core 658 659**System API**: This is a system API. 660 661**Parameters** 662 663| Name| Type| Mandatory| Description| 664| -------- | -------- | -------- | -------- | 665| color | string | Yes| Background color to set. The value is a hexadecimal RGB or ARGB color code and is case insensitive, for example, **#00FF00** or **#FF00FF00**.| 666 667**Error codes** 668 669For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 670 671| ID| Error Message| 672| ------- | -------------------------------- | 673| 202 | Not System App. Interface caller is not a system app. | 674| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 675| 16000050 | Internal error. | 676 677**Example** 678 679```ts 680import { UIExtensionContentSession, UIExtensionAbility, Want } from '@kit.AbilityKit'; 681 682export default class UIExtAbility extends UIExtensionAbility { 683 // ... 684 685 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 686 let storage: LocalStorage = new LocalStorage(); 687 storage.setOrCreate('session', session); 688 689 try { 690 session.loadContent('pages/Extension', storage); 691 } catch (err) { 692 console.error('loadContent err:' + JSON.stringify(err)); 693 } 694 695 try { 696 session.setWindowBackgroundColor('#00FF00'); 697 } catch (err) { 698 console.error('setWindowBackgroundColor err:' + JSON.stringify(err)); 699 } 700 } 701 702 // ... 703} 704``` 705 706### startAbilityAsCaller<sup>11+</sup> 707 708startAbilityAsCaller(want: Want, callback: AsyncCallback\<void>): void 709 710Starts an ability as the caller. The initial ability places its caller information (such as the bundle name and ability name) in the **want** parameter and transfers the information to an ExtensionAbility at the middle layer. When the ExtensionAbility starts another ability by calling this API, the started ability can obtain the caller information of the initial ability from the **onCreate** lifecycle. This API uses an asynchronous callback to return the result. 711 712**System API**: This is a system API. 713 714**System capability**: SystemCapability.Ability.AbilityRuntime.Core 715 716**Parameters** 717 718| Name| Type| Mandatory| Description| 719| -------- | -------- | -------- | -------- | 720| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 721| callback | AsyncCallback\<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 722 723**Error codes** 724 725For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 726 727| ID| Error Message| 728| ------- | -------------------------------- | 729| 201 | The application does not have permission to call the interface. | 730| 202 | Not System App. Interface caller is not a system app. | 731| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 732| 16000001 | The specified ability does not exist. | 733| 16000002 | Incorrect ability type. | 734| 16000004 | Cannot start an invisible component. | 735| 16000005 | The specified process does not have the permission. | 736| 16000006 | Cross-user operations are not allowed. | 737| 16000008 | The crowdtesting application expires. | 738| 16000009 | An ability cannot be started or stopped in Wukong mode. | 739| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 740| 16000011 | The context does not exist. | 741| 16000012 | The application is controlled. | 742| 16000013 | The application is controlled by EDM. | 743| 16000050 | Internal error. | 744| 16000053 | The ability is not on the top of the UI. | 745| 16000055 | Installation-free timed out. | 746| 16200001 | The caller has been released. | 747 748**Example** 749 750```ts 751import { UIExtensionContentSession, UIExtensionAbility, Want } from '@kit.AbilityKit'; 752import { BusinessError } from '@kit.BasicServicesKit'; 753 754export default class UIExtAbility extends UIExtensionAbility { 755 // ... 756 757 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 758 let localWant: Want = want; 759 localWant.bundleName = 'com.example.demo'; 760 localWant.moduleName = 'entry'; 761 localWant.abilityName = 'TestAbility'; 762 763 session.startAbilityAsCaller(localWant, (err: BusinessError) => { 764 if (err) { 765 console.error(`Failed to startAbilityAsCaller, code: ${err.code}, msg: ${err.message}`); 766 return; 767 } 768 console.info(`Succeeded in startAbilityAsCaller`); 769 }) 770 } 771 772 // ... 773} 774``` 775 776### startAbilityAsCaller<sup>11+</sup> 777 778startAbilityAsCaller(want: Want, options: StartOptions, callback: AsyncCallback\<void>): void 779 780Starts an ability as the caller, with **options** specified. The initial ability places its caller information (such as the bundle name and ability name) in the **want** parameter and transfers the information to an ExtensionAbility at the middle layer. When the ExtensionAbility starts another ability by calling this API, the started ability can obtain the caller information of the initial ability from the **onCreate** lifecycle. This API uses an asynchronous callback to return the result. 781 782**System API**: This is a system API. 783 784**System capability**: SystemCapability.Ability.AbilityRuntime.Core 785 786**Parameters** 787 788| Name| Type| Mandatory| Description| 789| -------- | -------- | -------- | -------- | 790| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 791| options | [StartOptions](js-apis-app-ability-startOptions.md) | Yes| Parameters used for starting the ability.| 792| callback | AsyncCallback\<void> | Yes| Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 793 794**Error codes** 795 796For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 797 798| ID| Error Message| 799| ------- | -------------------------------- | 800| 201 | The application does not have permission to call the interface. | 801| 202 | Not System App. Interface caller is not a system app. | 802| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 803| 16000001 | The specified ability does not exist. | 804| 16000004 | Cannot start an invisible component. | 805| 16000005 | The specified process does not have the permission. | 806| 16000006 | Cross-user operations are not allowed. | 807| 16000008 | The crowdtesting application expires. | 808| 16000009 | An ability cannot be started or stopped in Wukong mode. | 809| 16000011 | The context does not exist. | 810| 16000012 | The application is controlled. | 811| 16000013 | The application is controlled by EDM. | 812| 16000050 | Internal error. | 813| 16000053 | The ability is not on the top of the UI. | 814| 16000055 | Installation-free timed out. | 815| 16200001 | The caller has been released. | 816 817**Example** 818 819```ts 820import { UIExtensionContentSession, UIExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 821import { BusinessError } from '@kit.BasicServicesKit'; 822 823export default class UIExtAbility extends UIExtensionAbility { 824 // ... 825 826 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 827 let localWant: Want = want; 828 localWant.bundleName = 'com.example.demo'; 829 localWant.moduleName = 'entry'; 830 localWant.abilityName = 'TestAbility'; 831 832 let startOptions: StartOptions = { 833 displayId: 0 834 }; 835 836 session.startAbilityAsCaller(localWant, startOptions, (err: BusinessError) => { 837 if (err) { 838 console.error(`Failed to startAbilityAsCaller, code: ${err.code}, msg: ${err.message}`); 839 return; 840 } 841 console.info(`Succeeded in startAbilityAsCaller`); 842 }) 843 } 844 845 // ... 846} 847``` 848 849### startAbilityAsCaller<sup>11+</sup> 850 851startAbilityAsCaller(want: Want, options?: StartOptions): Promise\<void> 852 853Starts an ability as the caller. The initial ability places its caller information (such as the bundle name and ability name) in the **want** parameter and transfers the information to an ExtensionAbility at the middle layer. When the ExtensionAbility starts another ability by calling this API, the started ability can obtain the caller information of the initial ability from the **onCreate** lifecycle. This API uses a promise to return the result. 854 855**System API**: This is a system API. 856 857**System capability**: SystemCapability.Ability.AbilityRuntime.Core 858 859**Parameters** 860 861| Name| Type| Mandatory| Description| 862| -------- | -------- | -------- | -------- | 863| want | [Want](js-apis-app-ability-want.md) | Yes| Want information about the target ability.| 864| options | [StartOptions](js-apis-app-ability-startOptions.md) | No| Parameters used for starting the ability.| 865 866**Return value** 867 868| Type| Description| 869| -------- | -------- | 870| Promise\<void> | Promise that returns no value.| 871 872**Error codes** 873 874For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 875 876| ID| Error Message| 877| ------- | -------------------------------- | 878| 201 | The application does not have permission to call the interface. | 879| 202 | Not System App. Interface caller is not a system app. | 880| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 881| 16000001 | The specified ability does not exist. | 882| 16000002 | Incorrect ability type. | 883| 16000004 | Cannot start an invisible component. | 884| 16000005 | The specified process does not have the permission. | 885| 16000006 | Cross-user operations are not allowed. | 886| 16000008 | The crowdtesting application expires. | 887| 16000009 | An ability cannot be started or stopped in Wukong mode. | 888| 16000010 | The call with the continuation and prepare continuation flag is forbidden. | 889| 16000011 | The context does not exist. | 890| 16000012 | The application is controlled. | 891| 16000013 | The application is controlled by EDM. | 892| 16000050 | Internal error. | 893| 16000053 | The ability is not on the top of the UI. | 894| 16000055 | Installation-free timed out. | 895| 16200001 | The caller has been released. | 896 897**Example** 898 899```ts 900import { UIExtensionContentSession, UIExtensionAbility, Want, StartOptions } from '@kit.AbilityKit'; 901import { BusinessError } from '@kit.BasicServicesKit'; 902 903export default class UIExtAbility extends UIExtensionAbility { 904 // ... 905 906 onSessionCreate(want: Want, session: UIExtensionContentSession): void { 907 let localWant: Want = want; 908 localWant.bundleName = 'com.example.demo'; 909 localWant.moduleName = 'entry'; 910 localWant.abilityName = 'TestAbility'; 911 912 let startOptions: StartOptions = { 913 displayId: 0 914 }; 915 916 session.startAbilityAsCaller(localWant, startOptions) 917 .then(() => { 918 console.info(`Succeeded in startAbilityAsCaller`); 919 }) 920 .catch((err: BusinessError) => { 921 console.error(`Failed to startAbilityAsCaller, code: ${err.code}, msg: ${err.message}`); 922 }); 923 } 924 925 // ... 926} 927``` 928 929### getUIExtensionHostWindowProxy<sup>11+</sup> 930 931getUIExtensionHostWindowProxy(): uiExtensionHost.UIExtensionHostWindowProxy 932 933Obtains the window object corresponding to the current UIExtension to notify the width, height, position, and avoided area. 934 935**System API**: This is a system API. 936 937**System capability**: SystemCapability.Ability.AbilityRuntime.Core 938 939**Return value** 940 941| Type| Description| 942| -------- | -------- | 943| [uiExtensionHost.UIExtensionHostWindowProxy](../apis-arkui/js-apis-uiExtensionHost-sys.md) | Window information of the host application.| 944 945**Error codes** 946 947For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Ability Error Codes](errorcode-ability.md). 948 949| ID| Error Message| 950| ------- | -------------------------------- | 951| 202 | Not System App. Interface caller is not a system app. | 952| 16000050 | Internal error. | 953 954**Example** 955 956```ts 957import { UIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 958import { uiExtensionHost } from '@kit.ArkUI'; 959 960const TAG: string = '[UIExtAbility]'; 961 962export default class UIExtAbility extends UIExtensionAbility { 963 onCreate() { 964 console.log(TAG, `UIExtAbility onCreate`); 965 } 966 967 onForeground() { 968 console.log(TAG, `UIExtAbility onForeground`); 969 } 970 971 onBackground() { 972 console.log(TAG, `UIExtAbility onBackground`); 973 } 974 975 onDestroy() { 976 console.log(TAG, `UIExtAbility onDestroy`); 977 } 978 979 onSessionCreate(want: Want, session: UIExtensionContentSession) { 980 let extensionHostWindow = session.getUIExtensionHostWindowProxy(); 981 let data: Record<string, UIExtensionContentSession | uiExtensionHost.UIExtensionHostWindowProxy> = { 982 'session': session, 983 'extensionHostWindow': extensionHostWindow 984 }; 985 let storage: LocalStorage = new LocalStorage(data); 986 987 try { 988 session.loadContent('pages/Extension', storage); 989 } catch (err) { 990 console.error('loadContent err:' + JSON.stringify(err)); 991 } 992 } 993 994 onSessionDestroy(session: UIExtensionContentSession) { 995 console.log(TAG, `UIExtAbility onSessionDestroy`); 996 } 997} 998``` 999