• 鸿蒙Harmony应用开发—ArkTS声明式开发(容器组件:UIExtensionComponent (系统接口))


    UIExtensionComponent用于支持在本页面内嵌入其他应用提供的UI。展示的内容在另外一个进程中运行,本应用并不参与其中的布局和渲染。

    通常用于有进程隔离诉求的模块化开发场景。

    说明:

    该组件从API Version 10开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

    本模块为系统接口

    使用约束

    本组件不支持预览。

    被拉起的Ability必须是带UI的Ability扩展,如何实现带UI的Ability扩展请参考实现带UI的Ability扩展

    必须显示设置组件宽高为非0有效值。

    子组件

    接口

    UIExtensionComponent(want: Want, options?: UIExtensionOptions)

    参数:

    参数名参数类型必填参数描述
    wantWant要加载的Ability。
    options11+UIExtensionOptions需要传递的构造项。

    属性

    支持通用属性

    事件

    不支持通用事件

    将事件经过坐标转换后传递给对端Ability处理。

    支持以下事件:

    onRemoteReady

    onRemoteReady(callback: Callback)

    UIExtensionAbility连接完成时的回调,之后可使用proxy向被拉起的Ability发送数据。

    参数:

    参数名类型说明
    proxyUIExtensionProxy用于向对端Ability发送数据。

    onReceive

    onReceive(callback: Callback<{ [key: string]: Object }>)

    收到被拉起的Ability发送的数据时触发的回调。

    参数:

    参数名类型说明
    data{ [key: string]: Object }收到来自对端Ability的数据。

    onResult

    onResult(callback: Callback<{code: number; want?: Want}>)

    被拉起的Ability扩展调用terminateSelfWithResult时会先触发本回调函数,再触发OnRelease。

    本回调内可处理对端Ability的结果数据,可参考AbilityResult

    参数:

    参数名类型说明
    codenumber收到来自对端Ability的处理結果code。
    wantWant收到来自对端Ability的处理結果Want

    onRelease

    onRelease(callback: Callback)

    用于处理被拉起的Ability销毁时的回调。

    被拉起的Ability扩展调用terminateSelfWithResult或者terminateSelf时会触发本回调,此时releaseCode为0,即正常销毁。

    被拉起的Ability扩展意外Crash或被kill时,触发本回调,此时releaseCode为1。

    参数:

    参数名类型说明
    releaseCodenumber对端Ability销毁时的code,0为正常销毁,1为异常销毁。

    onError

    onError(callback:ErrorCallback)

    被拉起的Ability扩展在运行过程中发生异常时触发本回调。可通过回调参数中的code、name和message获取错误信息并做处理。

    参数:

    参数名类型说明
    errBusinessError报错信息。

    UIExtensionOptions11+

    用于在UIExtensionComponent进行构造的时传递可选的构造参数。

    参数:

    参数名参数类型必填参数描述
    isTransferringCallerboolean在使用UIExtensionComponent嵌套时,设置当前UIExtensionComponent是否转发上一级的Caller信息。 默认值:false。

    UIExtensionProxy

    用于在双方建立连接成功后,组件使用方向被拉起的Ability发送数据、订阅和取消订阅注册。

    send

    send(data: { [key: string]: Object }): void

    用于在双方建立连接成功后,组件使用方向被拉起的Ability发送数据的场景,提供异步发送数据。

    系统能力: SystemCapability.ArkUI.ArkUI.Full

    参数:

    参数名参数类型必填参数描述
    data{ [key: string]: Object }异步发送给被拉起的扩展Ability的数据。

    sendSync11+

    sendSync(data: { [key: string]: Object }): { [key: string]: Object }

    用于在双方建立连接成功后,组件使用方向被拉起的Ability发送数据的场景,提供同步发送数据。

    系统能力: SystemCapability.ArkUI.ArkUI.Full

    参数:

    参数名参数类型必填参数描述
    data{ [key: string]: Object }同步发送给被拉起的扩展Ability的数据。

    返回值:

    类型描述
    { [key: string]: Object }扩展Ability回复的数据。

    错误码:

    错误号描述
    100011扩展Ability未注册同步回调
    100012数据发送失败

    on('asyncReceiverRegister')11+

    on(type: 'asyncReceiverRegister', callback: (proxy: UIExtensionProxy) => void): void

    用于在双方建立连接成功后,组件使用方订阅被拉起的Ability发生异步注册的场景。

    系统能力: SystemCapability.ArkUI.ArkUI.Full

    参数:

    参数名参数类型必填参数描述
    typestring代表订阅扩展Ability发生异步注册回调。
    callback(proxy: UIExtensionProxy) => void订阅扩展Ability注册setReceiveDataCallback后触发的回调。

    on('syncReceiverRegister')11+

    on(type: 'syncReceiverRegister', callback: (proxy: UIExtensionProxy) => void): void

    用于在双方建立连接成功后,组件使用方订阅被拉起的Ability发生同步注册的场景。

    系统能力: SystemCapability.ArkUI.ArkUI.Full

    参数:

    参数名参数类型必填参数描述
    typestring订阅扩展Ability发生同步注册回调。
    callback(proxy: UIExtensionProxy) => void扩展Ability注册setReceiveDataForResultCallback后触发的回调。

    off('asyncReceiverRegister')11+

    off(type: 'asyncReceiverRegister', callback?: (proxy: UIExtensionProxy) => void): void

    用于在双方建立连接成功后,组件使用方取消订阅被拉起的Ability发生异步注册的场景。

    系统能力: SystemCapability.ArkUI.ArkUI.Full

    参数:

    参数名参数类型必填参数描述
    typestring取消订阅扩展Ability发生异步注册回调。
    callback(proxy: UIExtensionProxy) => void为空代表取消订阅所有扩展Ability异步注册后触发回调。
    非空代表取消订阅异步对应回调。

    off('syncReceiverRegister')11+

    off(type: 'syncReceiverRegister', callback?: (proxy: UIExtensionProxy) => void): void

    用于在双方建立连接成功后,组件使用方取消订阅被拉起的Ability发生同步注册的场景。

    系统能力: SystemCapability.ArkUI.ArkUI.Full

    参数:

    参数名参数类型必填参数描述
    typestring取消订阅扩展Ability发生同步注册回调。
    callback(proxy: UIExtensionProxy) => void为空代表取消订阅所有扩展Ability同步注册后触发回调
    非空代表取消订阅同步对应回调。

    示例

    本示例仅展示组件使用的方法和扩展的Ability,实际运行需在设备中安装bundleName为"com.example.uiextensionprovider",abilityName为"UIExtensionProvider"的Ability扩展。

    1. // 组件使用示例:
    2. @Entry
    3. @Component
    4. struct Second {
    5. @State message1: string = 'Hello World 1'
    6. @State message2: string = 'Hello World 2'
    7. @State message3: string = 'Hello World 3'
    8. @State visible: Visibility = Visibility.Hidden
    9. @State wid: number = 300
    10. @State hei: number = 300
    11. private proxy: UIExtensionProxy = undefined
    12. build() {
    13. Row() {
    14. Column() {
    15. Text(this.message1).fontSize(30)
    16. Text(this.message2).fontSize(30)
    17. Text(this.message3).fontSize(30)
    18. UIExtensionComponent({
    19. bundleName : "com.example.newdemo",
    20. abilityName: "UIExtensionProvider",
    21. parameters: {
    22. "ability.want.params.uiExtensionType": "dialog"
    23. }
    24. })
    25. .width(this.wid)
    26. .height(this.hei)
    27. .border({width: 5, color: Color.Blue})
    28. .onResult((data)=>{
    29. this.message1 = JSON.stringify(data['want']['bundleName'])
    30. })
    31. .onRelease((code)=>{
    32. this.message2 = "release code : " + code
    33. })
    34. .onReceive((data) => {
    35. console.info('Lee onReceive, for test')
    36. this.message3 = JSON.stringify(data['data'])
    37. })
    38. .onRemoteReady((proxy) => {
    39. console.info('onRemoteReady, for test')
    40. this.proxy = proxy
    41. this.proxy.on("syncReceiverRegister", syncRegisterCallback1);
    42. // this.proxy.on("syncReceiverRegister", syncRegisterCallback2);
    43. // this.proxy.off("syncReceiverRegister");
    44. // this.proxy.off("syncReceiverRegister", (proxy) => {
    45. // console.info("off invoke for test, type is syncReceiverRegister");
    46. // });
    47. this.proxy.on("asyncReceiverRegister", (proxy1) => {
    48. console.info("on invoke for test, type is asyncReceiverRegister");
    49. });
    50. //
    51. // this.proxy.off("asyncReceiverRegister");
    52. })
    53. Button("点击向UIExtensionAbility发送数据").onClick(() => {
    54. if (this.proxy != undefined) {
    55. this.proxy.send({data: "你好1"})
    56. try {
    57. let re = this.proxy.sendSync({data: "你好2"})
    58. console.info("for test, re=" + JSON.stringify(re));
    59. } catch (err) {
    60. console.error(`sendSync failed for test. errCode=${err.code}, msg=${err.message}`);
    61. }
    62. }
    63. })
    64. }
    65. .width('100%')
    66. }
    67. .height('100%')
    68. }
    69. }
    70. function syncRegisterCallback1(proxy: UIExtensionProxy) {
    71. console.info("on invoke for test, syncRegisterCallback1, type is syncReceiverRegister");
    72. }
    73. function syncRegisterCallback2(proxy: UIExtensionProxy) {
    74. console.info("on invoke for test, syncRegisterCallback2, type is syncReceiverRegister");
    75. }
    1. // 扩展入口文件UIExtensionProvider.ts
    2. import UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility'
    3. import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'
    4. import Want from '@ohos.app.ability.Want';
    5. const TAG: string = '[UIExtAbility]'
    6. export default class UIExtAbility extends UIExtensionAbility {
    7. onCreate() {
    8. console.log(TAG, `UIExtAbility onCreate`)
    9. }
    10. onForeground() {
    11. console.log(TAG, `UIExtAbility onForeground`)
    12. }
    13. onBackground() {
    14. console.log(TAG, `UIExtAbility onBackground`)
    15. }
    16. onDestroy() {
    17. console.log(TAG, `UIExtAbility onDestroy`)
    18. }
    19. onSessionCreate(want: Want, session: UIExtensionContentSession) {
    20. console.log(TAG, `UIExtAbility onSessionCreate, want: ${JSON.stringify(want)}`)
    21. let param: Record<string, UIExtensionContentSession> = {
    22. 'session': session
    23. };
    24. let storage: LocalStorage = new LocalStorage(param);
    25. session.loadContent('pages/extension', storage);
    26. }
    27. onSessionDestroy(session: UIExtensionContentSession) {
    28. console.log(TAG, `UIExtAbility onSessionDestroy`)
    29. }
    30. }
    1. // 扩展Ability入口页面文件extension.ets
    2. import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
    3. import router from '@ohos.router';
    4. let storage = LocalStorage.getShared()
    5. @Entry(storage)
    6. @Component
    7. struct Extension {
    8. @State message: string = 'UIExtensionAbility'
    9. private session: UIExtensionContentSession = storage.get<UIExtensionContentSession>('session');
    10. onPageShow() {
    11. if (this.session != undefined) {
    12. this.session.setReceiveDataCallback((data)=> {
    13. this.message = JSON.stringify(data['data'])
    14. console.info("invoke for test, handle callback set by setReceiveDataCallback successfully");
    15. })
    16. this.session.setReceiveDataForResultCallback(func1)
    17. }
    18. }
    19. build() {
    20. Row() {
    21. Column() {
    22. Text(this.message)
    23. .fontSize(20)
    24. .fontWeight(FontWeight.Bold)
    25. Button("点击向Component发送数据").onClick(()=>{
    26. if (this.session != undefined) {
    27. this.session.sendData({"data": 543321})
    28. console.info('send 543321, for test')
    29. }
    30. })
    31. Button("terminate").onClick(()=> {
    32. if (this.session != undefined) {
    33. this.session.terminateSelf();
    34. }
    35. storage.clear()
    36. })
    37. Button("terminate with result").onClick(()=>{
    38. if (this.session != undefined) {
    39. this.session.terminateSelfWithResult({
    40. resultCode: 0,
    41. want: {
    42. bundleName: "myBundleName",
    43. parameters: { "result": 123456 }
    44. }
    45. })
    46. }
    47. storage.clear()
    48. })
    49. Button("点击跳转").onClick(()=> {
    50. router.pushUrl({url: 'pages/hello'})
    51. })
    52. }
    53. }
    54. .height('100%')
    55. }
    56. }
    57. function func1(data: { [key: string]: Object; }): { [key: string]: Object; } {
    58. this.message = JSON.stringify(data['data'])
    59. console.info("invoke for test, handle callback set by setReceiveDataForResultCallback successfully");
    60. return data;
    61. }

    最后,有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习是非常有必要的。 

    这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开发必掌握的核心知识要点,内容包含了ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。

    希望这一份鸿蒙学习资料能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!

    获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

    鸿蒙(HarmonyOS NEXT)最新学习路线

    •  HarmonOS基础技能

    • HarmonOS就业必备技能 
    •  HarmonOS多媒体技术

    • 鸿蒙NaPi组件进阶

    • HarmonOS高级技能

    • 初识HarmonOS内核 
    • 实战就业级设备开发

    有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。

    获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料

    《鸿蒙 (OpenHarmony)开发入门教学视频》

    《鸿蒙生态应用开发V2.0白皮书》

    图片

    《鸿蒙 (OpenHarmony)开发基础到实战手册》

    OpenHarmony北向、南向开发环境搭建

    图片

     《鸿蒙开发基础》

    • ArkTS语言
    • 安装DevEco Studio
    • 运用你的第一个ArkTS应用
    • ArkUI声明式UI开发
    • .……

    图片

     《鸿蒙开发进阶》

    • Stage模型入门
    • 网络管理
    • 数据管理
    • 电话服务
    • 分布式应用开发
    • 通知与窗口管理
    • 多媒体技术
    • 安全技能
    • 任务管理
    • WebGL
    • 国际化开发
    • 应用测试
    • DFX面向未来设计
    • 鸿蒙系统移植和裁剪定制
    • ……

    图片

    《鸿蒙进阶实战》

    • ArkTS实践
    • UIAbility应用
    • 网络案例
    • ……

    图片

     获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料

    总结

    总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。 

  • 相关阅读:
    MySQL数据xtrabackup物理备份方法
    智源发布线虫生命模型,超级人脑有望在未来15-30年实现
    网安学习-应急响应3
    C++ 内联函数的作用
    Android Studio 计算当前时间 转化为时间戳
    用JpaTransactionManager操作数据库事务
    基于vue的实时交流聊天系统软件设计
    Vue3+node.js网易云音乐实战项目(七)
    Ubuntu空间不足,如何扩容
    Hippo4J v1.3.1 发布,增加 Netty 监控上报、SpringCloud Hystrix 线程池监控等特性
  • 原文地址:https://blog.csdn.net/m0_64420071/article/details/136771073