1e41f4b71Sopenharmony_ci# UIExtensionComponent (系统接口) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciUIExtensionComponent用于支持在本页面内嵌入其他应用提供的UI。展示的内容在另外一个进程中运行,本应用并不参与其中的布局和渲染。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci通常用于有进程隔离诉求的模块化开发场景。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci> **说明:** 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> 该组件从API Version 10开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> 本模块为系统接口。 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## 使用约束 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci本组件不支持预览。 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci被拉起的Ability必须是带UI的Ability扩展,如何实现带UI的Ability扩展请参考[实现带UI的Ability扩展](../../apis-ability-kit/js-apis-app-ability-uiExtensionAbility.md)。 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci必须显示设置组件宽高为非0有效值。 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci## 子组件 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci无 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci## 接口 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ciUIExtensionComponent(want: Want, options?: UIExtensionOptions) 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**参数:** 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci| 参数名 | 参数类型 | 必填 | 参数描述 | 32e41f4b71Sopenharmony_ci| --------------------- | ---------------------------------------------------------- | ---- | ------------------ | 33e41f4b71Sopenharmony_ci| want | [Want](../../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 要加载的Ability。 | 34e41f4b71Sopenharmony_ci| options<sup>11+</sup> | [UIExtensionOptions](#uiextensionoptions11) | 否 | 需要传递的构造项。 | 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci## 属性 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci支持[通用属性](ts-universal-attributes-size.md)。 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci## 事件 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci不支持[点击](ts-universal-events-click.md)等通用事件。 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci将事件经过坐标转换后传递给对端Ability处理。 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci支持以下事件: 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci### onRemoteReady 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_cionRemoteReady(callback: [Callback](../../apis-basic-services-kit/js-apis-base.md#callback)\<UIExtensionProxy>) 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ciUIExtensionAbility连接完成时的回调,之后可使用proxy向被拉起的Ability发送数据。 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci**参数:** 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci| 参数名 | 类型 | 说明 | 57e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ | 58e41f4b71Sopenharmony_ci| proxy | UIExtensionProxy | 用于向对端Ability发送数据。 | 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci### onReceive 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_cionReceive(callback: ReceiveCallback) 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci收到被拉起的Ability发送的数据时触发的回调。 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**参数:** 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci| 参数名 | 类型 | 说明 | 69e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ | 70e41f4b71Sopenharmony_ci| data | [ReceiveCallback](#receivecallback14) | 收到来自对端Ability的数据。 | 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_ci被拉起的Ability扩展调用terminateSelfWithResult时会先触发本回调函数,再触发OnRelease。 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci本回调内可处理对端Ability的结果数据,可参考[AbilityResult](../../apis-ability-kit/js-apis-inner-ability-abilityResult.md)。 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci> **说明:** 81e41f4b71Sopenharmony_ci> 从 API version 10 开始支持,从 API version 12 开始废弃,建议使用[onTerminated](#onterminated12)替代。 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci**参数:** 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci| 参数名 | 类型 | 说明 | 86e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ | 87e41f4b71Sopenharmony_ci| code | number | 收到来自对端Ability的处理結果code。 | 88e41f4b71Sopenharmony_ci| want | Want | 收到来自对端Ability的处理結果[Want](../../apis-ability-kit/js-apis-app-ability-want.md)。 | 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_ci用于处理被拉起的Ability销毁时的回调。 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci被拉起的Ability扩展调用terminateSelfWithResult或者terminateSelf时会触发本回调,此时releaseCode为0,即正常销毁。 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci被拉起的Ability扩展意外Crash或被kill时,触发本回调,此时releaseCode为1。 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci> **说明:** 101e41f4b71Sopenharmony_ci> 从 API version 10 开始支持,从 API version 12 开始废弃,建议使用[onTerminated](#onterminated12)或者[onError](#onerror)替代。 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**参数:** 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci| 参数名 | 类型 | 说明 | 106e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ | 107e41f4b71Sopenharmony_ci| releaseCode | number | 对端Ability销毁时的code,0为正常销毁,1为异常销毁。 | 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci### onError 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_cionError(callback:[ErrorCallback](../../apis-basic-services-kit/js-apis-base.md#errorcallback)) 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci被拉起的Ability扩展在运行过程中发生异常时触发本回调。可通过回调参数中的code、name和message获取错误信息并做处理。 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci**参数:** 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci| 参数名 | 类型 | 说明 | 118e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ | 119e41f4b71Sopenharmony_ci| err | [BusinessError](../../apis-basic-services-kit/js-apis-base.md#businesserror) | 报错信息。 | 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci### onTerminated<sup>12+<sup> 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_cionTerminated(callback: Callback<TerminationInfo>) 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci被拉起的UIExtensionAbility通过调用`terminateSelfWithResult`或者`terminateSelf`正常退出时,触发本回调函数。 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci**参数:** 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci| 参数名 | 类型 | 说明 | 130e41f4b71Sopenharmony_ci| ------- | ------ | ---------------------------------------------------------------------------------------- | 131e41f4b71Sopenharmony_ci| callback | [Callback](../../apis-basic-services-kit/js-apis-base.md#callback)\<[TerminationInfo](#terminationinfo12)> | 回调函数,入参用于接收UIExtensionAbility的返回结果,类型为[TerminationInfo](#terminationinfo12)。 | 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci> **说明:** 134e41f4b71Sopenharmony_ci> 135e41f4b71Sopenharmony_ci> - 若UIExtensionAbility通过调用`terminateSelfWithResult`退出,其携带的信息会传给回调函数的入参; 136e41f4b71Sopenharmony_ci> - 若UIExtensionAbility通过调用`terminateSelf`退出,上述回调函数的入参中,"code"取默认值"0","want"为"undefined"。 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci### TerminationInfo<sup>12+<sup> 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci用于表示被拉起的UIExtensionAbility通过调用`terminateSelfWithResult`或者`terminateSelf`正常退出时的返回结果。 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci| 属性名 | 类型 | 说明 | 143e41f4b71Sopenharmony_ci| ------- | ------ | --------------------------------------------------- | 144e41f4b71Sopenharmony_ci| code | number | 被拉起UIExtensionAbility退出时返回的结果码。 | 145e41f4b71Sopenharmony_ci| want | [Want](../../apis-ability-kit/js-apis-app-ability-want.md) | 被拉起UIExtensionAbility退出时返回的数据。 | 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci## ReceiveCallback<sup>14+<sup> 148e41f4b71Sopenharmony_citype ReceiveCallback = Callback\<Record\<string, Object\>\> 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci用于封装被拉起的Ability发送的数据。 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci**参数:** 155e41f4b71Sopenharmony_ci| 参数名 | 类型 | 说明 | 156e41f4b71Sopenharmony_ci| ---------------------------- | ------ | ------------------------------------------------------------ | 157e41f4b71Sopenharmony_ci| data | Record\<string, Object\> | 收到来自对端Ability的数据。 | 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci## UIExtensionOptions<sup>11+</sup> 160e41f4b71Sopenharmony_ci用于在UIExtensionComponent进行构造的时传递可选的构造参数。 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci**参数:** 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci| 参数名 | 参数类型 | 必填 | 参数描述 | 165e41f4b71Sopenharmony_ci| ---- | ---------------------------------------- | ---- | --------------- | 166e41f4b71Sopenharmony_ci| isTransferringCaller | boolean | 否 | 在使用UIExtensionComponent嵌套时,设置当前UIExtensionComponent是否转发上一级的Caller信息。</br> 默认值:false。 | 167e41f4b71Sopenharmony_ci| placeholder<sup>12+<sup> | [ComponentContent](../js-apis-arkui-ComponentContent.md) | 否 | 设置占位符,在UIExtensionComponent与UIExtensionAbility建立连接前显示。 | 168e41f4b71Sopenharmony_ci| dpiFollowStrategy<sup>12+<sup> | [DpiFollowStrategy](ts-container-ui-extension-component-sys.md#dpifollowstrategy12) | 否 | 提供接口支持设置DPI跟随宿主或跟随UIExtensionAbility。</br> 默认值:FOLLOW_UI_EXTENSION_ABILITY_DPI。 | 169e41f4b71Sopenharmony_ci| areaChangePlaceholder<sup>13+<sup> | Record<string, [ComponentContent](../js-apis-arkui-ComponentContent.md)> | 否 | 设置尺寸变化占位符,在UIExtensionComponent尺寸发生变化并且UIExtension内部渲染未完成时显示, key值支持"FOLD_TO_EXPAND"(折叠展开尺寸变化)、"UNDEFINED"(默认尺寸变化)。 | 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci## DpiFollowStrategy<sup>12+</sup> 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci| 名称 | 描述 | 174e41f4b71Sopenharmony_ci| -------------------------------- | --------------- | 175e41f4b71Sopenharmony_ci| FOLLOW_HOST_DPI | 表示DPI跟随宿主。 | 176e41f4b71Sopenharmony_ci| FOLLOW_UI_EXTENSION_ABILITY_DPI | 表示DPI跟随UIExtensionAbility。 | 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci## UIExtensionProxy 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci用于在双方建立连接成功后,组件使用方向被拉起的Ability发送数据、订阅和取消订阅注册。 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci### send 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_cisend(data: Record\<string, Object\>): void 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci用于在双方建立连接成功后,组件使用方向被拉起的Ability发送数据的场景,提供异步发送数据。 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci**参数:** 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci| 参数名 | 参数类型 | 必填 | 参数描述 | 193e41f4b71Sopenharmony_ci| ---- | ---------------------------------------- | ---- | --------------- | 194e41f4b71Sopenharmony_ci| data | Record\<string, Object\> | 是 | 异步发送给被拉起的扩展Ability的数据。 | 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci### sendSync<sup>11+</sup> 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_cisendSync(data: Record\<string, Object\>): Record\<string, Object\> 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_ci用于在双方建立连接成功后,组件使用方向被拉起的Ability发送数据的场景,提供同步发送数据。 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci**参数:** 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci| 参数名 | 参数类型 | 必填 | 参数描述 | 207e41f4b71Sopenharmony_ci| ---- | ---------------------------------------- | ---- | --------------- | 208e41f4b71Sopenharmony_ci| data | Record\<string, Object\> | 是 | 同步发送给被拉起的扩展Ability的数据。 | 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci**返回值:** 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci| 类型 | 描述 | 213e41f4b71Sopenharmony_ci| ---- | ----| 214e41f4b71Sopenharmony_ci| Record\<string, Object\> | 扩展Ability回复的数据。 | 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci**错误码:** 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci| 错误号 | 描述 | 219e41f4b71Sopenharmony_ci| ---- | ----| 220e41f4b71Sopenharmony_ci| 100011 | 扩展Ability未注册同步回调 | 221e41f4b71Sopenharmony_ci| 100012 | 数据发送失败 | 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_ci### on('asyncReceiverRegister')<sup>11+</sup> 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_cion(type: 'asyncReceiverRegister', callback: Callback\<UIExtensionProxy\>): void 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci用于在双方建立连接成功后,组件使用方订阅被拉起的Ability发生异步注册的场景。 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci**参数:** 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci| 参数名 | 参数类型 |必填 | 参数描述 | 234e41f4b71Sopenharmony_ci| ------ | -------- |---- | ------- | 235e41f4b71Sopenharmony_ci| type | string | 是 | 代表订阅扩展Ability发生异步注册回调。 | 236e41f4b71Sopenharmony_ci| callback | Callback\<UIExtensionProxy\> | 是 | 订阅扩展Ability注册setReceiveDataCallback后触发的回调。 | 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci### on('syncReceiverRegister')<sup>11+</sup> 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_cion(type: 'syncReceiverRegister', callback: Callback\<UIExtensionProxy\>): void; 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci用于在双方建立连接成功后,组件使用方订阅被拉起的Ability发生同步注册的场景。 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci**参数:** 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci| 参数名 | 参数类型 |必填 | 参数描述 | 249e41f4b71Sopenharmony_ci| ------ | -------- |---- | ------- | 250e41f4b71Sopenharmony_ci| type | string | 是 | 订阅扩展Ability发生同步注册回调。 | 251e41f4b71Sopenharmony_ci| callback | Callback\<UIExtensionProxy\> | 是 | 扩展Ability注册setReceiveDataForResultCallback后触发的回调。 | 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci### off('asyncReceiverRegister')<sup>11+</sup> 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_cioff(type: 'asyncReceiverRegister', callback?: Callback\<UIExtensionProxy\>): void 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci用于在双方建立连接成功后,组件使用方取消订阅被拉起的Ability发生异步注册的场景。 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci**参数:** 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci| 参数名 | 参数类型 | 必填 | 参数描述 | 264e41f4b71Sopenharmony_ci| ------ | -------- | ----- | ------- | 265e41f4b71Sopenharmony_ci| type | string | 是 | 取消订阅扩展Ability发生异步注册回调。 | 266e41f4b71Sopenharmony_ci| callback | Callback\<UIExtensionProxy\> | 否 | 为空代表取消订阅所有扩展Ability异步注册后触发回调。<br> 非空代表取消订阅异步对应回调。 | 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci### off('syncReceiverRegister')<sup>11+</sup> 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_cioff(type: 'syncReceiverRegister', callback?: Callback\<UIExtensionProxy\>): void 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci用于在双方建立连接成功后,组件使用方取消订阅被拉起的Ability发生同步注册的场景。 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci**参数:** 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci| 参数名 | 参数类型 | 必填 | 参数描述 | 279e41f4b71Sopenharmony_ci| ------ | -------- | ----- | ------- | 280e41f4b71Sopenharmony_ci| type | string | 是 | 取消订阅扩展Ability发生同步注册回调。 | 281e41f4b71Sopenharmony_ci| callback | Callback\<UIExtensionProxy\> | 否 | 为空代表取消订阅所有扩展Ability同步注册后触发回调<br> 非空代表取消订阅同步对应回调。 | 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci## 示例 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci本示例仅展示组件使用的方法和扩展的Ability,实际运行需在设备中安装bundleName为"com.example.uiextensionprovider",abilityName为"UIExtensionProvider"的Ability扩展。 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_ci```ts 288e41f4b71Sopenharmony_ci// 组件使用示例: 289e41f4b71Sopenharmony_ciimport { ComponentContent } from '@kit.ArkUI'; 290e41f4b71Sopenharmony_ciclass Params { 291e41f4b71Sopenharmony_ci} 292e41f4b71Sopenharmony_ci@Builder 293e41f4b71Sopenharmony_cifunction LoadingBuilder(params: Params) { 294e41f4b71Sopenharmony_ci Column() { 295e41f4b71Sopenharmony_ci LoadingProgress() 296e41f4b71Sopenharmony_ci .color(Color.Blue) 297e41f4b71Sopenharmony_ci } 298e41f4b71Sopenharmony_ci} 299e41f4b71Sopenharmony_ci@Builder 300e41f4b71Sopenharmony_cifunction AreaChangePlaceholderBuilder(params: Params) { 301e41f4b71Sopenharmony_ci Column() { 302e41f4b71Sopenharmony_ci } 303e41f4b71Sopenharmony_ci .width('100%') 304e41f4b71Sopenharmony_ci .height('100%') 305e41f4b71Sopenharmony_ci .backgroundColor(Color.Orange) 306e41f4b71Sopenharmony_ci} 307e41f4b71Sopenharmony_ci@Entry 308e41f4b71Sopenharmony_ci@Component 309e41f4b71Sopenharmony_cistruct Second { 310e41f4b71Sopenharmony_ci @State message1: string = 'Hello World 1' 311e41f4b71Sopenharmony_ci @State message2: string = 'Hello World 2' 312e41f4b71Sopenharmony_ci @State message3: string = 'Hello World 3' 313e41f4b71Sopenharmony_ci @State visible: Visibility = Visibility.Hidden 314e41f4b71Sopenharmony_ci @State wid: number = 300 315e41f4b71Sopenharmony_ci @State hei: number = 300 316e41f4b71Sopenharmony_ci private proxy: UIExtensionProxy | null = null; 317e41f4b71Sopenharmony_ci private initPlaceholder = new ComponentContent(this.getUIContext(), wrapBuilder(LoadingBuilder), new Params); 318e41f4b71Sopenharmony_ci private areaChangePlaceholder = new ComponentContent(this.getUIContext(), wrapBuilder(AreaChangePlaceholderBuilder), new Params); 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci build() { 322e41f4b71Sopenharmony_ci Row() { 323e41f4b71Sopenharmony_ci Column() { 324e41f4b71Sopenharmony_ci Text(this.message1).fontSize(30) 325e41f4b71Sopenharmony_ci Text(this.message2).fontSize(30) 326e41f4b71Sopenharmony_ci Text(this.message3).fontSize(30) 327e41f4b71Sopenharmony_ci UIExtensionComponent({ 328e41f4b71Sopenharmony_ci bundleName : "com.example.newdemo", 329e41f4b71Sopenharmony_ci abilityName: "UIExtensionProvider", 330e41f4b71Sopenharmony_ci parameters: { 331e41f4b71Sopenharmony_ci "ability.want.params.uiExtensionType": "sys/commonUI" 332e41f4b71Sopenharmony_ci }}, 333e41f4b71Sopenharmony_ci { 334e41f4b71Sopenharmony_ci placeholder: this.initPlaceholder, 335e41f4b71Sopenharmony_ci areaChangePlaceholder: { 336e41f4b71Sopenharmony_ci "FOLD_TO_EXPAND" : this.areaChangePlaceholder, 337e41f4b71Sopenharmony_ci } 338e41f4b71Sopenharmony_ci }) 339e41f4b71Sopenharmony_ci .width(this.wid) 340e41f4b71Sopenharmony_ci .height(this.hei) 341e41f4b71Sopenharmony_ci .border({width: 5, color: Color.Blue}) 342e41f4b71Sopenharmony_ci .onReceive((data) => { 343e41f4b71Sopenharmony_ci console.info('Lee onReceive, for test') 344e41f4b71Sopenharmony_ci this.message3 = JSON.stringify(data['data']) 345e41f4b71Sopenharmony_ci }) 346e41f4b71Sopenharmony_ci .onTerminated((info) => { 347e41f4b71Sopenharmony_ci console.info('onTerminated: code =' + info.code + ', want = ' + JSON.stringify(info.want)); 348e41f4b71Sopenharmony_ci }) 349e41f4b71Sopenharmony_ci .onRemoteReady((proxy) => { 350e41f4b71Sopenharmony_ci console.info('onRemoteReady, for test') 351e41f4b71Sopenharmony_ci this.proxy = proxy 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci this.proxy.on("syncReceiverRegister", syncRegisterCallback1); 354e41f4b71Sopenharmony_ci // this.proxy.on("syncReceiverRegister", syncRegisterCallback2); 355e41f4b71Sopenharmony_ci 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci // this.proxy.off("syncReceiverRegister"); 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci // this.proxy.off("syncReceiverRegister", (proxy) => { 360e41f4b71Sopenharmony_ci // console.info("off invoke for test, type is syncReceiverRegister"); 361e41f4b71Sopenharmony_ci // }); 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci this.proxy.on("asyncReceiverRegister", (proxy1) => { 364e41f4b71Sopenharmony_ci console.info("on invoke for test, type is asyncReceiverRegister"); 365e41f4b71Sopenharmony_ci }); 366e41f4b71Sopenharmony_ci // 367e41f4b71Sopenharmony_ci // this.proxy.off("asyncReceiverRegister"); 368e41f4b71Sopenharmony_ci }) 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci Button("点击向UIExtensionAbility发送数据").onClick(() => { 371e41f4b71Sopenharmony_ci if (this.proxy != undefined) { 372e41f4b71Sopenharmony_ci this.proxy.send({data: "你好1"}) 373e41f4b71Sopenharmony_ci 374e41f4b71Sopenharmony_ci try { 375e41f4b71Sopenharmony_ci let re = this.proxy.sendSync({data: "你好2"}) 376e41f4b71Sopenharmony_ci console.info("for test, re=" + JSON.stringify(re)); 377e41f4b71Sopenharmony_ci } catch (err) { 378e41f4b71Sopenharmony_ci console.error(`sendSync failed for test. errCode=${err.code}, msg=${err.message}`); 379e41f4b71Sopenharmony_ci } 380e41f4b71Sopenharmony_ci } 381e41f4b71Sopenharmony_ci }) 382e41f4b71Sopenharmony_ci } 383e41f4b71Sopenharmony_ci .width('100%') 384e41f4b71Sopenharmony_ci } 385e41f4b71Sopenharmony_ci .height('100%') 386e41f4b71Sopenharmony_ci } 387e41f4b71Sopenharmony_ci} 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_cifunction syncRegisterCallback1(proxy: UIExtensionProxy) { 390e41f4b71Sopenharmony_ci console.info("on invoke for test, syncRegisterCallback1, type is syncReceiverRegister"); 391e41f4b71Sopenharmony_ci} 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_cifunction syncRegisterCallback2(proxy: UIExtensionProxy) { 394e41f4b71Sopenharmony_ci console.info("on invoke for test, syncRegisterCallback2, type is syncReceiverRegister"); 395e41f4b71Sopenharmony_ci} 396e41f4b71Sopenharmony_ci``` 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci```ts 399e41f4b71Sopenharmony_ci// 扩展入口文件UIExtensionProvider.ts 400e41f4b71Sopenharmony_ciimport { UIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ciconst TAG: string = '[UIExtAbility]' 403e41f4b71Sopenharmony_ciexport default class UIExtAbility extends UIExtensionAbility { 404e41f4b71Sopenharmony_ci 405e41f4b71Sopenharmony_ci onCreate() { 406e41f4b71Sopenharmony_ci console.log(TAG, `UIExtAbility onCreate`) 407e41f4b71Sopenharmony_ci } 408e41f4b71Sopenharmony_ci 409e41f4b71Sopenharmony_ci onForeground() { 410e41f4b71Sopenharmony_ci console.log(TAG, `UIExtAbility onForeground`) 411e41f4b71Sopenharmony_ci } 412e41f4b71Sopenharmony_ci 413e41f4b71Sopenharmony_ci onBackground() { 414e41f4b71Sopenharmony_ci console.log(TAG, `UIExtAbility onBackground`) 415e41f4b71Sopenharmony_ci } 416e41f4b71Sopenharmony_ci 417e41f4b71Sopenharmony_ci onDestroy() { 418e41f4b71Sopenharmony_ci console.log(TAG, `UIExtAbility onDestroy`) 419e41f4b71Sopenharmony_ci } 420e41f4b71Sopenharmony_ci 421e41f4b71Sopenharmony_ci onSessionCreate(want: Want, session: UIExtensionContentSession) { 422e41f4b71Sopenharmony_ci console.log(TAG, `UIExtAbility onSessionCreate, want: ${JSON.stringify(want)}`) 423e41f4b71Sopenharmony_ci let param: Record<string, UIExtensionContentSession> = { 424e41f4b71Sopenharmony_ci 'session': session 425e41f4b71Sopenharmony_ci }; 426e41f4b71Sopenharmony_ci let storage: LocalStorage = new LocalStorage(param); 427e41f4b71Sopenharmony_ci session.loadContent('pages/extension', storage); 428e41f4b71Sopenharmony_ci } 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ci onSessionDestroy(session: UIExtensionContentSession) { 431e41f4b71Sopenharmony_ci console.log(TAG, `UIExtAbility onSessionDestroy`) 432e41f4b71Sopenharmony_ci } 433e41f4b71Sopenharmony_ci} 434e41f4b71Sopenharmony_ci``` 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci```ts 437e41f4b71Sopenharmony_ci// 扩展Ability入口页面文件extension.ets 438e41f4b71Sopenharmony_ciimport { UIExtensionContentSession } from '@kit.AbilityKit'; 439e41f4b71Sopenharmony_ciimport { router } from '@kit.ArkUI'; 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_cilet storage = LocalStorage.getShared() 442e41f4b71Sopenharmony_ciAppStorage.setOrCreate('message', 'UIExtensionAbility') 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci@Entry(storage) 445e41f4b71Sopenharmony_ci@Component 446e41f4b71Sopenharmony_cistruct Extension { 447e41f4b71Sopenharmony_ci @StorageLink('message') storageLink: string = ''; 448e41f4b71Sopenharmony_ci private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session'); 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci onPageShow() { 451e41f4b71Sopenharmony_ci if (this.session != undefined) { 452e41f4b71Sopenharmony_ci this.session.setReceiveDataCallback((data)=> { 453e41f4b71Sopenharmony_ci this.storageLink = JSON.stringify(data) 454e41f4b71Sopenharmony_ci console.info("invoke for test, handle callback set by setReceiveDataCallback successfully"); 455e41f4b71Sopenharmony_ci }) 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci this.session.setReceiveDataForResultCallback(func1) 458e41f4b71Sopenharmony_ci } 459e41f4b71Sopenharmony_ci } 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_ci build() { 462e41f4b71Sopenharmony_ci Row() { 463e41f4b71Sopenharmony_ci Column() { 464e41f4b71Sopenharmony_ci Text(this.storageLink) 465e41f4b71Sopenharmony_ci .fontSize(20) 466e41f4b71Sopenharmony_ci .fontWeight(FontWeight.Bold) 467e41f4b71Sopenharmony_ci Button("点击向Component发送数据").onClick(()=>{ 468e41f4b71Sopenharmony_ci if (this.session != undefined) { 469e41f4b71Sopenharmony_ci this.session.sendData({"data": 543321}) 470e41f4b71Sopenharmony_ci console.info('send 543321, for test') 471e41f4b71Sopenharmony_ci } 472e41f4b71Sopenharmony_ci }) 473e41f4b71Sopenharmony_ci Button("terminate").onClick(()=> { 474e41f4b71Sopenharmony_ci if (this.session != undefined) { 475e41f4b71Sopenharmony_ci this.session.terminateSelf(); 476e41f4b71Sopenharmony_ci } 477e41f4b71Sopenharmony_ci storage.clear() 478e41f4b71Sopenharmony_ci }) 479e41f4b71Sopenharmony_ci Button("terminate with result").onClick(()=>{ 480e41f4b71Sopenharmony_ci if (this.session != undefined) { 481e41f4b71Sopenharmony_ci this.session.terminateSelfWithResult({ 482e41f4b71Sopenharmony_ci resultCode: 0, 483e41f4b71Sopenharmony_ci want: { 484e41f4b71Sopenharmony_ci bundleName: "myBundleName", 485e41f4b71Sopenharmony_ci parameters: { "result": 123456 } 486e41f4b71Sopenharmony_ci } 487e41f4b71Sopenharmony_ci }) 488e41f4b71Sopenharmony_ci } 489e41f4b71Sopenharmony_ci storage.clear() 490e41f4b71Sopenharmony_ci }) 491e41f4b71Sopenharmony_ci 492e41f4b71Sopenharmony_ci Button("点击跳转").onClick(()=> { 493e41f4b71Sopenharmony_ci router.pushUrl({url: 'pages/hello'}) 494e41f4b71Sopenharmony_ci }) 495e41f4b71Sopenharmony_ci } 496e41f4b71Sopenharmony_ci } 497e41f4b71Sopenharmony_ci .height('100%') 498e41f4b71Sopenharmony_ci } 499e41f4b71Sopenharmony_ci} 500e41f4b71Sopenharmony_cifunction func1(data: Record<string, Object>): Record<string, Object> { 501e41f4b71Sopenharmony_ci let linkToMsg: SubscribedAbstractProperty<string> = AppStorage.link('message'); 502e41f4b71Sopenharmony_ci linkToMsg.set(JSON.stringify(data)) 503e41f4b71Sopenharmony_ci console.info("invoke for test, handle callback set by setReceiveDataForResultCallback successfully"); 504e41f4b71Sopenharmony_ci return data; 505e41f4b71Sopenharmony_ci} 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ci``` 508