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