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