鸿蒙HarmonyOS实战-Stage模型(信息传递载体Want)_want标签匹配规则

69 阅读4分钟

| --- | --- | --- | | 为空 | 不为空 | 成功 | | 为空 | 为空 | 成功 | | 不为空 | 为空 | 失败 | | 不为空 | 包含调用方传入的entities | 成功 | | 不为空 | 不完全包含调用方传入的entities | 失败 |

在这里插入图片描述

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

在这里插入图片描述

调用方传入的want参数待匹配Ability的skills配置中的uris数组匹配结果
uri为空,type为空uris为空匹配成功
uri为空,type为空uris存在uri的scheme和type都为空的元素匹配成功
uri为空,type为空其他情况匹配失败
uri不为空,type为空uris为空匹配失败
uri不为空,type为空uris存在一条数据uri匹配成功且type为空匹配成功
uri为空,type不为空uris为空匹配失败
uri为空,type不为空uris存在一条数据uri的scheme为空且type匹配成功匹配成功
uri不为空,type不为空uris为空匹配失败
uri不为空,type不为空uris存在一条数据uri匹配和type匹配需要均匹配成功匹配成功
uri不为空,type不为空其他情况匹配失败

在这里插入图片描述

☀️2.1.4 uri匹配规则

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

s_uri.schemes_uri.hosts_uri.paths_uri.pathStartWiths_uri.pathRegexw_uri匹配结果
abc://def成功
失败
abc://defabc://def成功
abc://defdef://abc失败
/path/path成功
/path/path/123失败
/pathStart/pathStart成功
/pathStart/pathStart2失败
^/regex$/regex成功
^/regex$/path/regex失败
abc://def/pathabc://def/path成功
abc://def/pathabc://def/path/123失败
/pathStart/pathStart2/test失败
^/regex$/path/regex/test失败

待匹配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匹配规则
Ability Skills Regex匹配规则w_type匹配规则匹配结果
s_type为空任意失败
s_type为通配符"/"任意成功
s_type为通配符"prefixType/*"含有"prefixType/"成功
s_type为通配符"prefixType/*"不含有"prefixType/"失败
w_type为通配符"/"任意成功
w_type为通配符"prefixType/*"含有"prefixType/"成功
w_type为通配符"prefixType/*"不含有"prefixType/"失败

3.常见action与entities

🦋3.1 action

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

Action(动作)描述
ACTION_HOME启动应用入口组件的动作,需要和ENTITY_HOME配合使用;系统桌面应用图标就是显式的入口组件,点击也是启动入口组件;入口组件可以配置多个。
ACTION_CHOOSE选择本地资源数据,例如联系人、相册等;系统一般对不同类型的数据有对应的Picker应用,例如联系人和图库。
ACTION_VIEW_DATA查看数据,当使用网址uri时,则表示显示该网址对应的内容。
ACTION_VIEW_MULTIPLE_DATA发送多个数据记录的操作。

🦋3.2 entities

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

类别名称描述
ENTITY_DEFAULT默认类别,没有实际意义。
ENTITY_HOME主屏幕有图标点击入口类别。
ENTITY_BROWSABLE指示浏览器类别。

4.使用显式Want启动Ability

1、启动方

新建callerAbility
在这里插入图片描述

2、被启动方

同理新建calleeAbility
在这里插入图片描述
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、执行

在这里插入图片描述

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", // ... } // ... ] }, ]

在这里插入图片描述

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": "www.test.com:8080/query/stude…", "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}) } }

匹配条件匹配结果
wantaction不为空,且被skillsaction包括匹配成功
wantentities不为空,且被skillsentities包括匹配成功
skillsuris拼接为https://www.test.com:8080/query* ( *为通配符)包含wanturi,匹配成功
wanttype不为空,且被skillstype包含匹配成功

在这里插入图片描述
3、运行

在这里插入图片描述

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} } } } }

字段说明
ability.picker.type应用选择器根据该字段渲染相应的文件类型图标
ability.picker.fileNames应用选择器根据该字段展示文件名
ability.picker.fileSizes应用选择器根据该字段展示文件大小。以字节为单位
ability.picker.fileNames[]文件名数组,与ability.picker.fileSizes[]有一一对应的关系
ability.picker.fileSizes[]文件大小数组,与ability.picker.fileNames[]有一一对应的关系

在这里插入图片描述

2、被分享方

定义skills

"skills": [ { "entities": [ // ... ], "actions": [

img img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!