首页 > 其他分享 >HarmonyOS的连接艺术之三:拉起指定类型的应用,打造个性化体验

HarmonyOS的连接艺术之三:拉起指定类型的应用,打造个性化体验

时间:2024-10-20 12:59:32浏览次数:1  
标签:code const string 之三 number HarmonyOS common message 个性化

本文旨在深入探讨华为鸿蒙HarmonyOS Next系统(截止目前API12)的技术细节,基于实际开发实践进行总结。
主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。
本文为原创内容,任何形式的转载必须注明出处及原作者。

在有些场景下,我们希望用户能够选择打开特定类型的应用,而不是指定具体的应用。例如,你可能在购物 App 中需要选择打开地图应用或导航应用来查看店铺位置。HarmonyOS 提供了拉起指定类型的应用功能,可以方便地实现这种需求。

常见的垂域应用类型

HarmonyOS 支持多种垂域应用类型,包括导航类应用、金融类应用、图片编辑类应用、文件处理类应用和邮件类应用等。每种类型的应用都有不同的参数和开发步骤,下面分别进行介绍。
1. 导航类应用
参数说明

参数 类型 必填 说明
sceneType number 场景类型,1 代表路线规划,2 代表导航,3 代表位置搜索
destinationLatitude number 是 (路线规划和导航) 终点纬度
destinationLongitude number 是 (路线规划和导航) 终点经度
destinationName string 是 (位置搜索) 终点名称
originName string 起点名称
originLatitude number 起点纬度
originLongitude number 起点经度
originPoiIds Record<number, string> 起点 POI ID 列表
destinationPoiIds Record<number, string> 终点 POI ID 列表
vehicleType number 交通出行工具,0 代表驾车,1 代表步行,2 代表骑行,3 代表公交
开发步骤
  1. 导入 ohos.app.ability.common 模块。
  2. 构造 Want 对象并设置参数。
  3. 调用 startAbilityByType 接口,传入类型 navigation 和 Want 对象。
  4. 处理回调结果。
    示例代码
import { common } from '@ohos.app.ability.common';
export default class EntryAbility extends common.UIAbility {
  onWindowStageCreate(windowStage: common.WindowStage) {
    const context = this.getContext(this) as common.UIAbilityContext;
    const wantParam: common.WantParam = {
      sceneType: 1,
      destinationLatitude: 32.060844,
      destinationLongitude: 118.78315,
      destinationName: "xx市xx路xx号",
      destinationPoiIds: { 1: "1111", 2: "2222" },
      originName: "xx市xx公园",
      originLatitude: 31.060844,
      originLongitude: 120.78315,
      originPoiIds: { 1: "3333", 2: "4444" },
      vehicleType: 0
    };
    const abilityStartCallback: common.AbilityStartCallback = {
      one rror: (code: number, name: string, message: string) => {
        console.log(`onError code ${code} name: ${name} message: ${message}`);
      },
      onResult: (result) => {
        console.log(`onResult result: ${JSON.stringify(result)}`);
      }
    };
    context.startAbilityByType("navigation", wantParam, abilityStartCallback);
  }
}

2. 金融类应用
参数说明

参数 类型 必填 说明
sceneType number 场景类型,1 代表转账汇款,2 代表信用卡还款
bankCardNo string 银行卡卡号
开发步骤
  1. 导入 ohos.app.ability.common 模块。
  2. 构造 Want 对象并设置参数。
  3. 调用 startAbilityByType 接口,传入类型 finance 和 Want 对象。
  4. 处理回调结果。
    示例代码
import { common } from '@ohos.app.ability.common';
export default class EntryAbility extends common.UIAbility {
  onWindowStageCreate(windowStage: common.WindowStage) {
    const context = this.getContext(this) as common.UIAbilityContext;
    const wantParam: common.WantParam = {
      sceneType: 1,
      bankCardNo: "123456789"
    };
    const abilityStartCallback: common.AbilityStartCallback = {
      one rror: (code: number, name: string, message: string) => {
        console.log(`onError code ${code} name: ${name} message: ${message}`);
      },
      onResult: (result) => {
        console.log(`onResult result: ${JSON.stringify(result)}`);
      }
    };
    context.startAbilityByType("finance", wantParam, abilityStartCallback);
  }
}

3. 图片编辑类应用
参数说明

参数 类型 必填 说明
uri string 图片编辑应用的 URL
开发步骤
  1. 导入 ohos.app.ability.common 模块。
  2. 调用 startAbilityByType 接口,传入类型 photoEditor 和 Want 对象。
  3. 处理回调结果,获取编辑后的图片 URL。
    示例代码
import { common } from '@ohos.app.ability.common';
export default class EntryAbility extends common.UIAbility {
  onWindowStageCreate(windowStage: common.WindowStage) {
    const context = this.getContext(this) as common.UIAbilityContext;
    const wantParam: common.WantParam = {
      uri: "file:///system/app/ExamplePhotoEditorAbility/ExamplePhotoEditorAbility.ets"
    };
    const abilityStartCallback: common.AbilityStartCallback = {
      one rror: (code: number, name: string, message: string) => {
        console.log(`onError code ${code} name: ${name} message: ${message}`);
      },
      onResult: (result) => {
        console.log(`onResult result: ${JSON.stringify(result)}`);
      }
    };
    context.startAbilityByType("photoEditor", wantParam, abilityStartCallback);
  }
}

4. 文件处理类应用
参数说明

参数 类型 必填 说明
uri string 文件路径
type string 文件类型,例如 "general.plain-text"
flags number 权限标志,例如 FLAG_AUTH_READ_URI_PERMISSION
开发步骤
  1. 导入 ohos.app.ability.common 模块。
  2. 获取文件的 URI。
  3. 构造 Want 对象并设置参数。
  4. 调用 startAbility 接口,传入 Want 对象。
  5. 处理回调结果。
    示例代码
import { common } from '@ohos.app.ability.common';
import { fileUri } from '@ohos.corefile';
export default class EntryAbility extends common.UIAbility {
  onWindowStageCreate(windowStage: common.WindowStage) {
    const context = this.getContext(this) as common.UIAbilityContext;
    const filePath = context.filesDir + "/test.txt";
    const uri = fileUri.getUriFromPath(filePath);
    const want: common.Want = {
      action: "ohos.want.action.viewData",
      uri: uri,
      type: "general.plain-text",
      flags: common.WantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION
    };
    context.startAbility(want);
  }
}

5. 邮件类应用
参数说明

参数 类型 必填 说明
email string[] 收件人邮箱地址
cc string[] 抄送人邮箱地址
bcc string[] 密送人邮箱地址
subject string 邮件主题
body string 邮件正文
stream string[] 邮件附件 URI
开发步骤
  1. 导入 ohos.app.ability.common 模块。
  2. 构造 Want 对象并设置参数。
  3. 调用 startAbilityByType 接口,传入类型 mail 和 Want 对象。
  4. 处理回调结果。
    示例代码
import { common } from '@ohos.app.ability.common';
export default class EntryAbility extends common.UIAbility {
  onWindowStageCreate(windowStage: common.WindowStage) {
    const context = this.getContext(this) as common.UIAbilityContext;
    const wantParam: common.WantParam = {
      email: ["[email protected]", "[email protected]"],
      cc: ["[email protected]", "[email protected]"],
      bcc: ["[email protected]", "[email protected]"],
      subject: "邮件主题",
      body: "邮件正文",
      stream: ["file:///system/app/ExamplePhotoEditorAbility/ExamplePhotoEditorAbility.ets"]
    };
    const abilityStartCallback: common.AbilityStartCallback = {
      one rror: (code: number, name: string, message: string) =>
      one rror: (code: number, name: string, message: string) => {
        console.log(`onError code ${code} name: ${name} message: ${message}`);
      },
      onResult: (result) => {
        console.log(`onResult result: ${JSON.stringify(result)}`);
      }
    };
    context.startAbilityByType("mail", wantParam, abilityStartCallback);
  }
}

总结
拉起指定类型的应用是应用间跳转的另一种方式,它能够方便用户选择打开特定类型的应用,而不是指定具体的应用,我们可以根据实际需求选择合适的垂域应用类型。

标签:code,const,string,之三,number,HarmonyOS,common,message,个性化
From: https://www.cnblogs.com/samex/p/18487147

相关文章

  • HarmonyOS的连接艺术之二:精准控制目标应用
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。在某些场景下,我们希望应用能够精准控......
  • HarmonyOS的连接艺术之四:便捷访问系统功能
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。HarmonyOS系统提供了丰富的系统应用,......
  • HarmonyOS的连接艺术之一:让应用之间无缝协作
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。应用之间协作的需求越来越普遍。例如,......
  • 基于Node.js+vue个性化学习推荐网站(开题+程序+论文) 计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容一、选题背景随着信息技术的飞速发展,教育领域也在不断变革。关于学习推荐系统的研究,现有研究主要以通用性学习推荐为主,专门针对个性化学习推荐的研究较少。在国内外,......
  • HarmonyOS:保存你的应用数据
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/......
  • HarmonyOS:使用HTTP访问网络
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/......
  • HarmonyOS:Navigation组件的使用
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/......
  • HarmonyOS Stage 模型:进程、线程与配置文件之浅析
    本文旨在深入探讨华为鸿蒙HarmonyOSNext系统(截止目前API12)的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。HarmonyOS的Stage模型为开发者提供......
  • HarmonyOS设置组件导航
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(MaoistLearning)➤博客园地址:为敢技术(https://www.cnblogs.com/strengthen/ )➤GitHub地址:https://github.com/strengthen➤原文地址:https://www.cnblogs.com/strengthen/p/......
  • AI声音克隆技术:打造个性化语音体验的新时代
    AI声音克隆技术:打造个性化语音体验的新时代在人工智能快速发展的今天,AI声音克隆技术正在为我们带来前所未有的个性化语音体验。这项技术能够通过分析一个人的声音样本,生成与原声高度相似的合成语音,为用户提供了一种全新的自我表达方式。本文将深入探讨AI声音克隆的工作原理、......