1e41f4b71Sopenharmony_ci# UIExtensionComponent (System API)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci**\<UIExtensionComponent>** is used to embed UIs provided by other applications in the local application UI. The embedded content runs in another process, and the local application does not participate in its layout and rendering.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciThis component is usually used in modular development scenarios where process isolation is required.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **NOTE**
8e41f4b71Sopenharmony_ci>
9e41f4b71Sopenharmony_ci> This component is supported since API version 10. Updates will be marked with a superscript to indicate their earliest API version.
10e41f4b71Sopenharmony_ci>
11e41f4b71Sopenharmony_ci> The APIs provided by this component are system APIs.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci## Restrictions
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ciThis component does not support preview.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciThe ability to be started must be a UIExtensionAbility, an extension ability with UI. For details about how to implement a UIExtensionAbility, see [@ohos.app.ability.UIExtensionAbility (Base Class for ExtensionAbilities with UI)](../../apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md).
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciThe width and height of the component must be set to non-zero valid values.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci## Child Components
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciNot supported
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci## APIs
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ciUIExtensionComponent(want: Want, options?: UIExtensionOptions)
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci**Parameters**
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci| Name               | Type                                                  | Mandatory| Description          |
32e41f4b71Sopenharmony_ci| --------------------- | ---------------------------------------------------------- | ---- | ------------------ |
33e41f4b71Sopenharmony_ci| want                  | [Want](../../apis-ability-kit/js-apis-app-ability-want.md) | Yes  | Ability to start. |
34e41f4b71Sopenharmony_ci| options<sup>11+</sup> | [UIExtensionOptions](#uiextensionoptions11)                | No  | Construction parameters to be transferred.|
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci## Attributes
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ciThe [universal attributes](ts-universal-attributes-size.md) are supported.
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci## Events
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ciThe [universal events](ts-universal-events-click.md) are not supported.
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ciThe events are passed to the remote UIExtensionAbility for processing after coordinate conversion.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ciThe following events are supported:
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci### onRemoteReady
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_cionRemoteReady(callback: [Callback](../../apis-basic-services-kit/js-apis-base.md#callback)\<UIExtensionProxy>)
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ciInvoked when the connection to the remote UIExtensionAbility is set up, that is, the UIExtensionAbility is ready to receive data through the proxy.
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci**Parameters**
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci| Name                      | Type  | Description                                                        |
57e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ |
58e41f4b71Sopenharmony_ci| proxy                        | UIExtensionProxy | Proxy used to send data to the remote UIExtensionAbility.                         |
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci### onReceive
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_cionReceive(callback: [Callback](../../apis-basic-services-kit/js-apis-base.md#callback)\<{ [key: string]: Object }>)
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ciInvoked when the data sent by the started UIExtensionAbility is received.
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci**Parameters**
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci| Name                      | Type  | Description                                                        |
69e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ |
70e41f4b71Sopenharmony_ci| data                        | { [key: string]: Object } | Data from the remote UIExtensionAbility.                |
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci### onResult<sup>(deprecated)</sup>
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_cionResult(callback: [Callback](../../apis-basic-services-kit/js-apis-base.md#callback)\<{code: number; want?: Want}>)
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ciInvoked when the started UIExtensionAbility calls **terminateSelfWithResult**. After this callback is invoked, **OnRelease** is invoked.
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ciThe result data of the remote UIExtensionAbility can be processed in this callback. For details, see [AbilityResult](../../apis-ability-kit/js-apis-inner-ability-abilityResult.md).
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci> **NOTE**
81e41f4b71Sopenharmony_ci> This API is supported since API version 10 and deprecated since API version 12. You are advised to use [onTerminated](#onterminated12) instead.
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci**Parameters**
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci| Name                      | Type  | Description                                                        |
86e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ |
87e41f4b71Sopenharmony_ci| code                        | number | Result code from the remote UIExtensionAbility.                         |
88e41f4b71Sopenharmony_ci| want                        | Want | [Want](../../apis-ability-kit/js-apis-app-ability-want.md) of the result from the remote UIExtensionAbility.|
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci### onRelease<sup>(deprecated)</sup>
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_cionRelease(callback: [Callback](../../apis-basic-services-kit/js-apis-base.md#callback)\<number>)
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ciInvoked when the started UIExtensionAbility is destroyed.
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ciIf the UIExtensionAbility is destroyed correctly by calling **terminateSelfWithResult** or **terminateSelf**, the value of **releaseCode** is **0**.
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ciIf the UIExtensionAbility is destroyed because it crashes or is forced stopped, the value of **releaseCode** is **1**.
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci> **NOTE**
101e41f4b71Sopenharmony_ci> This API is supported since API version 10 and deprecated since API version 12. You are advised to use [onTerminated](#onterminated12) or [onError](#onerror) instead.
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**Parameters**
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci| Name                      | Type  | Description                                                        |
106e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ |
107e41f4b71Sopenharmony_ci| releaseCode                        | number | Code that indicates how the remote UIExtensionAbility is destroyed. The value **0** means normal destruction, and **1** means abnormal destruction.                         |
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci### onError
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_cionError(callback:[ErrorCallback](../../apis-basic-services-kit/js-apis-base.md#errorcallback))
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ciInvoked when an exception occurs during the running of the UIExtensionAbility. You can obtain the error information based on the **code**, **name**, and **message** parameters in the callback and rectify the exception accordingly.
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci**Parameters**
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci| Name                      | Type  | Description                                                        |
118e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ |
119e41f4b71Sopenharmony_ci| err                        | [BusinessError](../../apis-basic-services-kit/js-apis-base.md#businesserror) | Error information.   |
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci### onTerminated<sup>12+<sup>
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_cionTerminated(callback: Callback&lt;TerminationInfo&gt;)
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ciCalled when the started UIExtensionAbility is terminated by calling **terminateSelfWithResult** or **terminateSelf**.
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci**Parameters**
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ci| Name  | Type  | Description                                                                                    |
130e41f4b71Sopenharmony_ci| -------  | ------ | ---------------------------------------------------------------------------------------- |
131e41f4b71Sopenharmony_ci| callback | [Callback](../../apis-basic-services-kit/js-apis-base.md#callback)\<[TerminationInfo](#terminationinfo12)> | Callback used to return the result from the UIExtensionAbility.|
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci> **NOTE**
134e41f4b71Sopenharmony_ci>
135e41f4b71Sopenharmony_ci> - If the UIExtensionAbility is terminated by calling **terminateSelfWithResult**, the carried information is passed as arguments into the callback function.
136e41f4b71Sopenharmony_ci> - If the UIExtensionAbility is terminated by calling **terminateSelf**, the input parameters **code** and **want** in the callback function are at their default values: **0** and **undefined**, respectively.
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci### TerminationInfo<sup>12+<sup>
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ciRepresents the result returned when the UIExtensionAbility that has been started exits properly by calling **terminateSelfWithResult** or **terminateSelf**.
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci| Name | Type  | Description                                                |
143e41f4b71Sopenharmony_ci| ------- | ------ | ---------------------------------------------------  |
144e41f4b71Sopenharmony_ci| code    | number | Result code returned when the UIExtensionAbility exits.|
145e41f4b71Sopenharmony_ci| want    | [Want](../../apis-ability-kit/js-apis-app-ability-want.md)   | Data returned when the UIExtensionAbility exits.  |
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci## UIExtensionOptions<sup>11+</sup>
148e41f4b71Sopenharmony_ciDescribes the optional construction parameters during **UIExtensionComponent** construction.
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci**Parameters**
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ci| Name              | Type                                | Mandatory| Description                                                                                                     |
153e41f4b71Sopenharmony_ci| ----                 | ---------------------------------------- | ---- | ---------------                                                                                               |
154e41f4b71Sopenharmony_ci| isTransferringCaller | boolean                                  | No  | Whether the UIExtensionComponent forwards the upper-level caller information when it is used for nesting.<br> Default value: **false**|
155e41f4b71Sopenharmony_ci| placeholder<sup>12+<sup> | [ComponentContent](../js-apis-arkui-ComponentContent.md)       | No  | Placeholder to be displayed before the UIExtensionComponent establishes a connection with the UIExtensionAbility.|
156e41f4b71Sopenharmony_ci| dpiFollowStrategy<sup>12+<sup> | [DpiFollowStrategy](ts-container-ui-extension-component-sys.md#dpifollowstrategy12)                  | No  | Whether the DPI settings follow the host or UIExtensionAbility.<br> Default value: **FOLLOW_UI_EXTENSION_ABILITY_DPI**|
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci## DpiFollowStrategy<sup>12+</sup>
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ci| Name                            | Description            |
161e41f4b71Sopenharmony_ci| -------------------------------- | --------------- |
162e41f4b71Sopenharmony_ci| FOLLOW_HOST_DPI                  | The DPI settings follow the host.|
163e41f4b71Sopenharmony_ci| FOLLOW_UI_EXTENSION_ABILITY_DPI  | The DPI settings follow the UIExtensionAbility.|
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci## UIExtensionProxy
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ciImplements a **UIExtensionProxy** instance for the component host to send data to, subscribe to, or unsubscribe from the started UIExtensionAbility through the connection established between the two parties.
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci### send
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_cisend(data: { [key: string]: Object }): void
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ciAsynchronously sends data from the component host to the started UIExtensionAbility through the connection established between the two parties.
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci**Parameters**
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci| Name | Type                                    | Mandatory  | Description           |
180e41f4b71Sopenharmony_ci| ---- | ---------------------------------------- | ---- | --------------- |
181e41f4b71Sopenharmony_ci| data | { [key: string]: Object } | Yes   | Data to be asynchronously sent to the started UIExtensionAbility.|
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci### sendSync<sup>11+</sup>
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_cisendSync(data: { [key: string]: Object }): { [key: string]: Object }
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ciSynchronously sends data from the component host to the started UIExtensionAbility through the connection established between the two parties.
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci**Parameters**
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci| Name | Type                                    | Mandatory  | Description           |
194e41f4b71Sopenharmony_ci| ---- | ---------------------------------------- | ---- | --------------- |
195e41f4b71Sopenharmony_ci| data | { [key: string]: Object } | Yes   | Data to be synchronously sent to the started UIExtensionAbility.|
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci**Return value**
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci| Type| Description|
200e41f4b71Sopenharmony_ci| ---- | ----|
201e41f4b71Sopenharmony_ci|{ [key: string]: Object } | Data returned by the UIExtensionAbility.|
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci**Error codes**
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci| ID| Description|
206e41f4b71Sopenharmony_ci| ---- | ----|
207e41f4b71Sopenharmony_ci| 100011 | UIExtensionAbility not registered.|
208e41f4b71Sopenharmony_ci| 100012 | Failed to send data.|
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci### on('asyncReceiverRegister')<sup>11+</sup>
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_cion(type: 'asyncReceiverRegister', callback: (proxy: UIExtensionProxy) => void): void
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ciSubscribes to asynchronous registration of the started UIExtensionAbility through the connection established between the component host and UIExtensionAbility.
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci**Parameters**
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci| Name | Type|Mandatory| Description|
221e41f4b71Sopenharmony_ci| ------ | -------- |---- | ------- |
222e41f4b71Sopenharmony_ci| type   | string | Yes| Event type. The value is fixed at **'asyncReceiverRegister'**.|
223e41f4b71Sopenharmony_ci| callback   | (proxy: UIExtensionProxy) => void | Yes| Callback used to return the result.|
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ci### on('syncReceiverRegister')<sup>11+</sup>
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_cion(type: 'syncReceiverRegister', callback: (proxy: UIExtensionProxy) => void): void
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ciSubscribes to synchronous registration of the started UIExtensionAbility through the connection established between the component host and UIExtensionAbility.
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci**Parameters**
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ci| Name | Type|Mandatory| Description|
236e41f4b71Sopenharmony_ci| ------ | -------- |---- | ------- |
237e41f4b71Sopenharmony_ci| type   | string | Yes| Event type. The value is fixed at **'syncReceiverRegister'**.|
238e41f4b71Sopenharmony_ci| callback   | (proxy: UIExtensionProxy) => void | Yes| Callback used to return the result.|
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci### off('asyncReceiverRegister')<sup>11+</sup>
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_cioff(type: 'asyncReceiverRegister', callback?: (proxy: UIExtensionProxy) => void): void
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ciUnsubscribes from asynchronous registration of the started UIExtensionAbility through the connection established between the component host and UIExtensionAbility.
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci**Parameters**
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description|
251e41f4b71Sopenharmony_ci| ------ | -------- | ----- | ------- |
252e41f4b71Sopenharmony_ci| type   | string | Yes| Event type. The value is fixed at **'asyncReceiverRegister'**.|
253e41f4b71Sopenharmony_ci| callback | (proxy: UIExtensionProxy) => void | No| Callback used for unsubscription.<br> If this parameter is not set, this API unsubscribes from all callbacks corresponding to **type**.|
254e41f4b71Sopenharmony_ci
255e41f4b71Sopenharmony_ci### off('syncReceiverRegister')<sup>11+</sup>
256e41f4b71Sopenharmony_ci
257e41f4b71Sopenharmony_cioff(type: 'syncReceiverRegister', callback?: (proxy: UIExtensionProxy) => void): void
258e41f4b71Sopenharmony_ci
259e41f4b71Sopenharmony_ciUnsubscribes from synchronous registration of the started UIExtensionAbility through the connection established between the component host and UIExtensionAbility.
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci**Parameters**
264e41f4b71Sopenharmony_ci
265e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description|
266e41f4b71Sopenharmony_ci| ------ | -------- | ----- | ------- |
267e41f4b71Sopenharmony_ci| type   | string | Yes| Event type. The value is fixed at **'syncReceiverRegister'**.|
268e41f4b71Sopenharmony_ci| callback | (proxy: UIExtensionProxy) => void | No| Callback used for unsubscription.<br> If this parameter is not set, this API unsubscribes from all callbacks corresponding to **type**.|
269e41f4b71Sopenharmony_ci
270e41f4b71Sopenharmony_ci## Example
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ciThis example shows only the method used by the component and the UIExtensionAbility. For the code to run properly, you need to install the ability whose **bundleName** is **"com.example.uiextensionprovider"** and **abilityName** is **"UIExtensionProvider"** on the device.
273e41f4b71Sopenharmony_ci
274e41f4b71Sopenharmony_ci```ts
275e41f4b71Sopenharmony_ci// Component usage example:
276e41f4b71Sopenharmony_ciimport { ComponentContent } from '@kit.ArkUI';
277e41f4b71Sopenharmony_ciclass Params {
278e41f4b71Sopenharmony_ci}
279e41f4b71Sopenharmony_ci@Builder
280e41f4b71Sopenharmony_cifunction LoadingBuilder(params: Params) {
281e41f4b71Sopenharmony_ci  Column() {
282e41f4b71Sopenharmony_ci   LoadingProgress()
283e41f4b71Sopenharmony_ci      .color(Color.Blue)
284e41f4b71Sopenharmony_ci  }
285e41f4b71Sopenharmony_ci}
286e41f4b71Sopenharmony_ci@Entry
287e41f4b71Sopenharmony_ci@Component
288e41f4b71Sopenharmony_cistruct Second {
289e41f4b71Sopenharmony_ci  @State message1: string = 'Hello World 1'
290e41f4b71Sopenharmony_ci  @State message2: string = 'Hello World 2'
291e41f4b71Sopenharmony_ci  @State message3: string = 'Hello World 3'
292e41f4b71Sopenharmony_ci  @State visible: Visibility = Visibility.Hidden
293e41f4b71Sopenharmony_ci  @State wid: number = 300
294e41f4b71Sopenharmony_ci  @State hei: number = 300
295e41f4b71Sopenharmony_ci  private proxy: UIExtensionProxy | null = null;
296e41f4b71Sopenharmony_ci  private contentNode = new ComponentContent(this.getUIContext(), wrapBuilder(LoadingBuilder), new Params);
297e41f4b71Sopenharmony_ci
298e41f4b71Sopenharmony_ci
299e41f4b71Sopenharmony_ci  build() {
300e41f4b71Sopenharmony_ci    Row() {
301e41f4b71Sopenharmony_ci      Column() {
302e41f4b71Sopenharmony_ci        Text(this.message1).fontSize(30)
303e41f4b71Sopenharmony_ci        Text(this.message2).fontSize(30)
304e41f4b71Sopenharmony_ci        Text(this.message3).fontSize(30)
305e41f4b71Sopenharmony_ci        UIExtensionComponent({
306e41f4b71Sopenharmony_ci          bundleName : "com.example.newdemo",
307e41f4b71Sopenharmony_ci          abilityName: "UIExtensionProvider",
308e41f4b71Sopenharmony_ci          parameters: {
309e41f4b71Sopenharmony_ci            "ability.want.params.uiExtensionType": "dialog"
310e41f4b71Sopenharmony_ci          }},
311e41f4b71Sopenharmony_ci          {
312e41f4b71Sopenharmony_ci            placeholder: this.contentNode
313e41f4b71Sopenharmony_ci          })
314e41f4b71Sopenharmony_ci          .width(this.wid)
315e41f4b71Sopenharmony_ci          .height(this.hei)
316e41f4b71Sopenharmony_ci          .border({width: 5, color: Color.Blue})
317e41f4b71Sopenharmony_ci          .onReceive((data) => {
318e41f4b71Sopenharmony_ci            console.info('Lee onReceive, for test')
319e41f4b71Sopenharmony_ci            this.message3 = JSON.stringify(data['data'])
320e41f4b71Sopenharmony_ci          })
321e41f4b71Sopenharmony_ci          .onTerminated((info) => {
322e41f4b71Sopenharmony_ci            console.info('onTerminated: code =' + info.code + ', want = ' + JSON.stringify(info.want));
323e41f4b71Sopenharmony_ci          })
324e41f4b71Sopenharmony_ci          .onRemoteReady((proxy) => {
325e41f4b71Sopenharmony_ci            console.info('onRemoteReady, for test')
326e41f4b71Sopenharmony_ci            this.proxy = proxy
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ci            this.proxy.on("syncReceiverRegister", syncRegisterCallback1);
329e41f4b71Sopenharmony_ci            // this.proxy.on("syncReceiverRegister", syncRegisterCallback2);
330e41f4b71Sopenharmony_ci
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ci            // this.proxy.off("syncReceiverRegister");
333e41f4b71Sopenharmony_ci
334e41f4b71Sopenharmony_ci            // this.proxy.off("syncReceiverRegister", (proxy) => {
335e41f4b71Sopenharmony_ci            //   console.info("off invoke for test, type is syncReceiverRegister");
336e41f4b71Sopenharmony_ci            // });
337e41f4b71Sopenharmony_ci
338e41f4b71Sopenharmony_ci            this.proxy.on("asyncReceiverRegister", (proxy1) => {
339e41f4b71Sopenharmony_ci              console.info("on invoke for test, type is asyncReceiverRegister");
340e41f4b71Sopenharmony_ci            });
341e41f4b71Sopenharmony_ci            //
342e41f4b71Sopenharmony_ci            // this.proxy.off("asyncReceiverRegister");
343e41f4b71Sopenharmony_ci          })
344e41f4b71Sopenharmony_ci
345e41f4b71Sopenharmony_ci        Button ("Send to UIExtensionAbility").onClick(() => {
346e41f4b71Sopenharmony_ci          if (this.proxy != undefined) {
347e41f4b71Sopenharmony_ci            this.proxy.send({data: "Hello 1"})
348e41f4b71Sopenharmony_ci
349e41f4b71Sopenharmony_ci            try {
350e41f4b71Sopenharmony_ci              let re = this.proxy.sendSync({data: "Hello 2"})
351e41f4b71Sopenharmony_ci              console.info("for test, re=" + JSON.stringify(re));
352e41f4b71Sopenharmony_ci            } catch (err) {
353e41f4b71Sopenharmony_ci              console.error(`sendSync failed for test. errCode=${err.code}, msg=${err.message}`);
354e41f4b71Sopenharmony_ci            }
355e41f4b71Sopenharmony_ci          }
356e41f4b71Sopenharmony_ci        })
357e41f4b71Sopenharmony_ci      }
358e41f4b71Sopenharmony_ci      .width('100%')
359e41f4b71Sopenharmony_ci    }
360e41f4b71Sopenharmony_ci    .height('100%')
361e41f4b71Sopenharmony_ci  }
362e41f4b71Sopenharmony_ci}
363e41f4b71Sopenharmony_ci
364e41f4b71Sopenharmony_cifunction syncRegisterCallback1(proxy: UIExtensionProxy) {
365e41f4b71Sopenharmony_ci  console.info("on invoke for test, syncRegisterCallback1, type is syncReceiverRegister");
366e41f4b71Sopenharmony_ci}
367e41f4b71Sopenharmony_ci
368e41f4b71Sopenharmony_cifunction syncRegisterCallback2(proxy: UIExtensionProxy) {
369e41f4b71Sopenharmony_ci  console.info("on invoke for test, syncRegisterCallback2, type is syncReceiverRegister");
370e41f4b71Sopenharmony_ci}
371e41f4b71Sopenharmony_ci```
372e41f4b71Sopenharmony_ci
373e41f4b71Sopenharmony_ci```ts
374e41f4b71Sopenharmony_ci// Extension entry point file UIExtensionProvider.ts
375e41f4b71Sopenharmony_ciimport { UIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
376e41f4b71Sopenharmony_ci
377e41f4b71Sopenharmony_ciconst TAG: string = '[UIExtAbility]'
378e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility {
379e41f4b71Sopenharmony_ci  
380e41f4b71Sopenharmony_ci  onCreate() {
381e41f4b71Sopenharmony_ci    console.log(TAG, `UIExtAbility onCreate`)
382e41f4b71Sopenharmony_ci  }
383e41f4b71Sopenharmony_ci
384e41f4b71Sopenharmony_ci  onForeground() {
385e41f4b71Sopenharmony_ci    console.log(TAG, `UIExtAbility onForeground`)
386e41f4b71Sopenharmony_ci  }
387e41f4b71Sopenharmony_ci
388e41f4b71Sopenharmony_ci  onBackground() {
389e41f4b71Sopenharmony_ci    console.log(TAG, `UIExtAbility onBackground`)
390e41f4b71Sopenharmony_ci  }
391e41f4b71Sopenharmony_ci
392e41f4b71Sopenharmony_ci  onDestroy() {
393e41f4b71Sopenharmony_ci    console.log(TAG, `UIExtAbility onDestroy`)
394e41f4b71Sopenharmony_ci  }
395e41f4b71Sopenharmony_ci
396e41f4b71Sopenharmony_ci  onSessionCreate(want: Want, session: UIExtensionContentSession) {
397e41f4b71Sopenharmony_ci    console.log(TAG, `UIExtAbility onSessionCreate, want: ${JSON.stringify(want)}`)
398e41f4b71Sopenharmony_ci    let param: Record<string, UIExtensionContentSession> = {
399e41f4b71Sopenharmony_ci      'session': session
400e41f4b71Sopenharmony_ci    };
401e41f4b71Sopenharmony_ci    let storage: LocalStorage = new LocalStorage(param);
402e41f4b71Sopenharmony_ci    session.loadContent('pages/extension', storage);
403e41f4b71Sopenharmony_ci  }
404e41f4b71Sopenharmony_ci
405e41f4b71Sopenharmony_ci  onSessionDestroy(session: UIExtensionContentSession) {
406e41f4b71Sopenharmony_ci    console.log(TAG, `UIExtAbility onSessionDestroy`)
407e41f4b71Sopenharmony_ci  }
408e41f4b71Sopenharmony_ci}
409e41f4b71Sopenharmony_ci```
410e41f4b71Sopenharmony_ci
411e41f4b71Sopenharmony_ci```ts
412e41f4b71Sopenharmony_ci// Entry page file of the Extension Ability: extension.ets
413e41f4b71Sopenharmony_ciimport { UIExtensionContentSession } from '@kit.AbilityKit';
414e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI';
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_cilet storage = LocalStorage.getShared()
417e41f4b71Sopenharmony_ciAppStorage.setOrCreate('message', 'UIExtensionAbility')
418e41f4b71Sopenharmony_ci
419e41f4b71Sopenharmony_ci@Entry(storage)
420e41f4b71Sopenharmony_ci@Component
421e41f4b71Sopenharmony_cistruct Extension {
422e41f4b71Sopenharmony_ci  @StorageLink('message') storageLink: string = '';
423e41f4b71Sopenharmony_ci  private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session');
424e41f4b71Sopenharmony_ci
425e41f4b71Sopenharmony_ci  onPageShow() {
426e41f4b71Sopenharmony_ci    if (this.session != undefined) {
427e41f4b71Sopenharmony_ci      this.session.setReceiveDataCallback((data)=> {
428e41f4b71Sopenharmony_ci        this.storageLink = JSON.stringify(data)
429e41f4b71Sopenharmony_ci        console.info("invoke for test, handle callback set by setReceiveDataCallback successfully");
430e41f4b71Sopenharmony_ci      })
431e41f4b71Sopenharmony_ci
432e41f4b71Sopenharmony_ci      this.session.setReceiveDataForResultCallback(func1)
433e41f4b71Sopenharmony_ci    }
434e41f4b71Sopenharmony_ci  }
435e41f4b71Sopenharmony_ci
436e41f4b71Sopenharmony_ci  build() {
437e41f4b71Sopenharmony_ci    Row() {
438e41f4b71Sopenharmony_ci      Column() {
439e41f4b71Sopenharmony_ci        Text(this.storageLink)
440e41f4b71Sopenharmony_ci          .fontSize(20)
441e41f4b71Sopenharmony_ci          .fontWeight(FontWeight.Bold)
442e41f4b71Sopenharmony_ci        Button("Send to Component").onClick(()=>{
443e41f4b71Sopenharmony_ci          if (this.session != undefined) {
444e41f4b71Sopenharmony_ci            this.session.sendData({"data": 543321})
445e41f4b71Sopenharmony_ci            console.info('send 543321, for test')
446e41f4b71Sopenharmony_ci          }
447e41f4b71Sopenharmony_ci        })
448e41f4b71Sopenharmony_ci        Button("terminate").onClick(()=> {
449e41f4b71Sopenharmony_ci          if (this.session != undefined) {
450e41f4b71Sopenharmony_ci            this.session.terminateSelf();
451e41f4b71Sopenharmony_ci          }
452e41f4b71Sopenharmony_ci          storage.clear()
453e41f4b71Sopenharmony_ci        })
454e41f4b71Sopenharmony_ci        Button("terminate with result").onClick(()=>{
455e41f4b71Sopenharmony_ci          if (this.session != undefined) {
456e41f4b71Sopenharmony_ci            this.session.terminateSelfWithResult({
457e41f4b71Sopenharmony_ci              resultCode: 0,
458e41f4b71Sopenharmony_ci              want: {
459e41f4b71Sopenharmony_ci                bundleName: "myBundleName",
460e41f4b71Sopenharmony_ci                parameters: { "result": 123456 }
461e41f4b71Sopenharmony_ci              }
462e41f4b71Sopenharmony_ci            })
463e41f4b71Sopenharmony_ci          }
464e41f4b71Sopenharmony_ci          storage.clear()
465e41f4b71Sopenharmony_ci        })
466e41f4b71Sopenharmony_ci
467e41f4b71Sopenharmony_ci        Button("Redirect").onClick(()=> {
468e41f4b71Sopenharmony_ci          router.pushUrl({url: 'pages/hello'})
469e41f4b71Sopenharmony_ci        })
470e41f4b71Sopenharmony_ci      }
471e41f4b71Sopenharmony_ci    }
472e41f4b71Sopenharmony_ci    .height('100%')
473e41f4b71Sopenharmony_ci  }
474e41f4b71Sopenharmony_ci}
475e41f4b71Sopenharmony_cifunction func1(data: Record<string, Object>): Record<string, Object> {
476e41f4b71Sopenharmony_ci  let linkToMsg: SubscribedAbstractProperty<string> = AppStorage.link('message');
477e41f4b71Sopenharmony_ci  linkToMsg.set(JSON.stringify(data))
478e41f4b71Sopenharmony_ci  console.info("invoke for test, handle callback set by setReceiveDataForResultCallback successfully");
479e41f4b71Sopenharmony_ci  return data;
480e41f4b71Sopenharmony_ci}
481e41f4b71Sopenharmony_ci
482e41f4b71Sopenharmony_ci```
483