码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 鸿蒙HarmonyOS实战-Stage模型(信息传递载体Want)


    合集 - 鸿蒙入门实战全套(核心知识点)(53)
    1.鸿蒙HarmonyOS实战-ArkTS语言(基本语法)03-202.鸿蒙HarmonyOS实战-ArkTS语言(状态管理)03-213.鸿蒙HarmonyOS实战-ArkTS语言(渲染控制)03-214.鸿蒙HarmonyOS实战-ArkUI组件(Row/Column)03-225.鸿蒙HarmonyOS实战-ArkUI组件(Stack)03-236.鸿蒙HarmonyOS实战-ArkUI组件(Flex)03-247.鸿蒙HarmonyOS实战-ArkUI组件(RelativeContainer)03-258.鸿蒙HarmonyOS实战-ArkUI组件(GridRow/GridCol)03-269.鸿蒙HarmonyOS实战-ArkUI组件(mediaquery)03-2710.鸿蒙HarmonyOS实战-ArkUI组件(List)03-2911.鸿蒙HarmonyOS实战-ArkUI组件(Grid/GridItem)03-2912.鸿蒙HarmonyOS实战-ArkUI组件(Swiper)03-3013.鸿蒙HarmonyOS实战-ArkUI组件(Button)04-0114.鸿蒙HarmonyOS实战-ArkUI组件(Radio)04-0215.鸿蒙HarmonyOS实战-ArkUI组件(Toggle)04-0216.鸿蒙HarmonyOS实战-ArkUI组件(Progress)04-0317.鸿蒙HarmonyOS实战-ArkUI组件(Text/Span)04-0318.鸿蒙HarmonyOS实战-ArkUI组件(TextInput/TextArea)04-0719.鸿蒙HarmonyOS实战-ArkUI组件(CustomDialog)04-0820.鸿蒙HarmonyOS实战-ArkUI组件(Video)04-0921.鸿蒙HarmonyOS实战-ArkUI组件(Popup)04-1022.鸿蒙HarmonyOS实战-ArkUI组件(Menu)04-1123.鸿蒙HarmonyOS实战-ArkUI组件(页面路由)04-1224.鸿蒙HarmonyOS实战-ArkUI组件(Navigation)04-1325.鸿蒙HarmonyOS实战-ArkUI组件(Tabs)04-1526.鸿蒙HarmonyOS实战-ArkUI组件(Image)04-1627.鸿蒙HarmonyOS实战-ArkUI组件(Shape)04-1728.鸿蒙HarmonyOS实战-ArkUI组件(Canvas)04-1829.鸿蒙HarmonyOS实战-ArkUI动画(布局更新动画)04-1930.鸿蒙HarmonyO实战-ArkUI动画(组件内转场动画)04-2031.鸿蒙HarmonyOS实战-ArkUI动画(放大缩小视图)04-2332.鸿蒙HarmonyOS实战-ArkUI动画(页面转场动画)04-2433.鸿蒙HarmonyOS实战-ArkUI事件(触屏事件)04-2534.鸿蒙HarmonyOS实战-ArkUI事件(键鼠事件)04-2635.鸿蒙HarmonyOS实战-ArkUI事件(焦点事件)05-0736.鸿蒙HarmonyOS实战-ArkUI事件(手势方法)05-0837.鸿蒙HarmonyOS实战-ArkUI事件(单一手势)05-0938.鸿蒙HarmonyOS实战-ArkUI事件(组合手势)05-1239.鸿蒙HarmonyOS实战-Stage模型(概述和组件配置)05-1340.鸿蒙HarmonyOS实战-Stage模型(UIAbility组件)05-1441.鸿蒙HarmonyOS实战-Stage模型(ExtensionAbility组件)05-1542.鸿蒙HarmonyOS实战-Stage模型(服务卡片介绍和运行机制)05-1643.鸿蒙HarmonyOS实战-Stage模型(服务卡片的模块和创建)05-1744.鸿蒙HarmonyOS实战-Stage模型(开发卡片页面)05-1845.鸿蒙HarmonyOS实战-Stage模型(开发卡片事件)05-2046.鸿蒙HarmonyOS实战-Stage模型(卡片数据交互)05-2147.鸿蒙HarmonyOS实战-Stage模型(AbilityStage组件容器)05-2248.鸿蒙HarmonyOS实战-Stage模型(应用上下文Context)05-23
    49.鸿蒙HarmonyOS实战-Stage模型(信息传递载体Want)05-24
    50.鸿蒙HarmonyOS实战-Stage模型(进程模型)05-2551.鸿蒙HarmonyOS实战-Stage模型(线程模型)05-2752.鸿蒙HarmonyOS实战-Web组件(基本使用和属性)05-2853.鸿蒙HarmonyOS实战-Web组件(前端函数和应用侧函数相互调用)05-29
    收起

    🚀前言

    应用中的信息传递是为了实现各种功能和交互。信息传递可以帮助用户和应用之间进行有效的沟通和交流。通过信息传递,应用可以向用户传递重要的消息、通知和提示,以提供及时的反馈和指导。同时,用户也可以通过信息传递向应用发送指令、请求和反馈,以实现个性化的需求和操作。

    信息传递还可以帮助应用之间实现数据的共享和交互。通过信息传递,不同应用可以实现数据的互通,以实现更多的功能和服务。例如,一个购物应用可以通过信息传递与支付应用进行数据交互,以实现支付功能;一个社交媒体应用可以通过信息传递与地图应用进行数据交互,以实现位置分享功能。

    此外,信息传递还可以帮助应用之间实现联动和协作。通过信息传递,应用可以实现多个功能的组合和协同,以提供更加丰富和综合的服务。例如,一个音乐应用可以与闹钟应用进行信息传递,以实现在特定时间播放特定的音乐。

    🚀一、信息传递载体Want

    🔎1.Want概述

    🦋1.1 Want的定义与用途

    HarmonyOS中的"Want"是一个用于定义和控制应用程序之间通信的基本概念。它可以用来描述一个应用程序对某个特定操作的需求或意愿,比如获取某个设备的位置信息、访问某个传感器的数据等。

    使用"Want"可以实现应用程序之间的无缝协作和互操作。通过定义和使用"Wants",应用程序可以根据自身的需求发送请求,并且可以接收和处理其他应用程序发送的请求。这种机制能够促进应用程序之间的交互和共享,并且使得整个系统更加智能和高效。

    "Wants"的使用可以带来许多好处。首先,它可以简化应用程序之间的通信和协作,减少开发人员的工作量。其次,它可以增强系统的灵活性和可扩展性,使得应用程序能够动态地适应不同的环境和设备。最后,它可以提供更加个性化和智能化的用户体验,使得应用程序能够更好地理解用户的需求并作出相应的反应。

    image

    🦋1.2 Want的类型

    在HarmonyOS中,信息传递载体Want的类型可以分为两种:显式Want和隐式Want。

    显式Want:显式Want是指明确指定要操作的组件或服务的Want。通过显式Want,可以精确地指定要传递给目标组件或服务的信息,并指定具体的要执行的操作。显式Want会包含组件名和操作类型等明确的指令信息。例如,可以使用显式Want来启动指定的Activity或调用指定的服务。

    let wantInfo = {
        deviceId: '', // deviceId为空表示本设备
        bundleName: 'com.example.myapplication',
        abilityName: 'FuncAbility',
    }
    

    隐式Want:隐式Want是指不明确指定要操作的组件或服务的Want,而是根据一定的规则和条件来进行匹配。通过隐式Want,可以实现组件之间的解耦和灵活性。隐式Want一般包含一组动作、类别、数据类型等条件,系统会根据这些条件来匹配合适的组件或服务。例如,可以使用隐式Want来处理某个特定类型的数据或根据某个特定的动作执行相应的操作。

    let wantInfo = {
        // uncomment line below if wish to implicitly query only in the specific bundle.
        // bundleName: 'com.example.myapplication',
        action: 'ohos.want.action.search',
        // entities can be omitted
        entities: [ 'entity.system.browsable' ],
        uri: 'https://www.test.com:8080/query/student',
        type: 'text/plain',
    };
    

    🔎2.显式Want与隐式Want匹配规则

    🦋2.1 隐式Want匹配原理详解

    系统将调用方传入的want参数(包含action、entities、uri和type属性)与已安装待匹配的应用Ability的skills配置(包含actions、entities、uris和type属性)依次进行匹配。当四个属性匹配均通过,则此应用才会被应用选择器展示给用户进行选择。

    ☀️2.1.1 want参数的action匹配规则

    image

    image

    ☀️2.1.2 want参数的entities匹配规则

    image

    image

    ☀️2.1.3 want参数的uri和type匹配规则

    image

    image

    image

    ☀️2.1.4 uri匹配规则

    以下是根据给定匹配规则展示的表格:

    image

    待匹配Ability的skills配置的uris中scheme、host、port、path、pathStartWith和pathRegex属性拼接,如果依次声明了path、pathStartWith和pathRegex属性时,uris将分别拼接为如下三种表达式:

    • 全路径表达式:scheme://host:port/path

    • 前缀表达式:scheme://host:port/pathStartWith

    • 正则表达式:scheme://host:port/pathRegex

    ☀️2.1.5 type匹配规则

    image

    🔎3.常见action与entities

    🦋3.1 action

    表示调用方要执行的通用操作(如查看、分享、应用详情)

    image

    🦋3.2 entities

    表示目标Ability的类别信息(如浏览器、视频播放器)

    image

    🔎4.使用显式Want启动Ability

    1、启动方

    新建callerAbility
    image

    2、被启动方

    同理新建calleeAbility
    image

    3、启动方UI

    import common from '@ohos.app.ability.common';
    @Entry
    @Component
    struct Index {
      @State message: string = 'callerAbility'
    
      build() {
        Row() {
          Column() {
            Text('hello')
              .fontSize(50)
              .fontWeight(FontWeight.Bold)
            // A new button with will call explicitStartAbility() when clicked.
            Button("CLICKME")
              .onClick(this.explicitStartAbility) // explicitStartAbility见下面示例代码
            // ...
          }
          .width('100%')
        }
        .height('100%')
      }
      async explicitStartAbility() {
        try {
          // Explicit want with abilityName specified.
          let want = {
            deviceId: "",
            bundleName: "com.example.myapplication",
            abilityName: "calleeAbility"
          };
          let context = getContext(this) as common.UIAbilityContext;
          await context.startAbility(want);
          console.info(`explicit start ability succeed`);
        } catch (error) {
          console.info(`explicit start ability failed with ${error.code}`);
        }
      }
    }
    

    4、执行

    image

    🔎5.使用隐式Want打开网址

    1、module.json5配置

    "skills": [
      {
        "entities": [
          "entity.system.browsable"
          // ...
        ],
        "actions": [
            "ohos.want.action.viewData"
            // ...
        ],
        "uris": [
          {
            "scheme": "https",
            "host": "www.test.com",
            "port": "8080",
            // prefix matching
            "pathStartWith": "query",
            "type": "text/*"
          },
          {
            "scheme": "http",
            // ...
          }
          // ...
        ]
      },
    ]
    

    image

    2、定义跳转函数

    async implicitStartAbility() {
        try {
            let want = {
                // uncomment line below if wish to implicitly query only in the specific bundle.
                // bundleName: "com.example.myapplication",
                "action": "ohos.want.action.viewData",
                // entities can be omitted.
                "entities": [ "entity.system.browsable" ],
                "uri": "https://www.test.com:8080/query/student",
                "type": "text/plain"
            }
            let context = getContext(this) as common.UIAbilityContext;
            await context.startAbility(want)
            console.info(`explicit start ability succeed`)
        } catch (error) {
            console.info(`explicit start ability failed with ${error.code}`)
        }
     }
    

    image

    image

    3、运行

    image

    🔎6.应用间使用Want分享数据

    1、分享方

    读取文件

    import fileIO from '@ohos.fileio';
    
    // let path = ...
    // file open where path is a variable contains the file path.
    let fileFd = fileIO.openSync(path, 0o102, 0o666);
    

    传输文件信息构造

    import wantConstant from '@ohos.ability.wantConstant';
    
    // let path = ...
    // let fileFd = ...
    // let fileSize = ...
    let want = {
        // This action is used to implicitly match the application selctor.
        action: wantConstant.Action.ACTION_SELECT,
        // This is the custom parameter in the first layer of want
        // which is intended to add info to application selector.
        parameters: {
            // The MIME type of pdf
            "ability.picker.type": "application/pdf",
            "ability.picker.fileNames": [path],
            "ability.picker.fileSizes": [fileSize],
            // This a nested want which will be directly send to the user selected application.         
            "ability.want.params.INTENT": {
                "action": "ohos.want.action.sendData",
                "type": "application/pdf",
                "parameters": {
                   "keyFd": {"type": "FD", "value": fileFd}
                }
            }
        }
    }
    

    image

    image

    2、被分享方

    定义skills

    "skills": [
      {
        "entities": [
          // ...
        ],
        "actions": [
            "ohos.want.action.sendData"
            // ...
        ],
        "uris": [
          {
            "type": "application/pdf"
          },
          // ...
        ]
      },
    ]
    

    2、接收数据

    onCreate(want, launchParam) {
      // note when keyFd is undefined, app crash will happen.
      if (want["parameters"]["keyFd"] !== undefined) {
        // receive file descriptor
        let fd = want["parameters"]["keyFd"].value;
        // ...
      }
    }
    

    🚀写在最后

    • 如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
    • 点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
    • 关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
    • 更多鸿蒙最新技术知识点,请关注作者博客:https://t.doruo.cn/14DjR1rEY

    image

  • 相关阅读:
    C语言-存储管理
    torch.zeros
    路网编辑器技术预研
    ABAP 功能代码示例:判断此物料是否在其对应的工厂存在,如果不存在,则报错
    如何实现延迟任务,这11种方式才算优雅!
    猿创征文 | 2022初学者10个必备程序 “工具箱“
    【优化模型】求无约束多元函数最小值
    软件定制开发的细节|网站搭建|APP小程序定制
    ssm+安卓APP校园学生疫情防范监控系统毕业设计源码281444
    10分钟完成微信JSAPI支付对接过程-JAVA后端接口
  • 原文地址:https://www.cnblogs.com/shudaoshan/p/18211814
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号