HarmonyOS的连接艺术之三:拉起指定类型的应用,打造个性化体验
本文旨在深入探讨华为鸿蒙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 代表公交 | 
| 开发步骤 | 
- 导入 ohos.app.ability.common模块。
- 构造 Want 对象并设置参数。
- 调用 startAbilityByType接口,传入类型navigation和 Want 对象。
- 处理回调结果。
 示例代码
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 = {
      onError: (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 | 否 | 银行卡卡号 | 
| 开发步骤 | 
- 导入 ohos.app.ability.common模块。
- 构造 Want 对象并设置参数。
- 调用 startAbilityByType接口,传入类型finance和 Want 对象。
- 处理回调结果。
 示例代码
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 = {
      onError: (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 | 
| 开发步骤 | 
- 导入 ohos.app.ability.common模块。
- 调用 startAbilityByType接口,传入类型photoEditor和 Want 对象。
- 处理回调结果,获取编辑后的图片 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 = {
      onError: (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 | 
| 开发步骤 | 
- 导入 ohos.app.ability.common模块。
- 获取文件的 URI。
- 构造 Want 对象并设置参数。
- 调用 startAbility接口,传入 Want 对象。
- 处理回调结果。
 示例代码
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. 邮件类应用
参数说明
| 参数 | 类型 | 必填 | 说明 | 
|---|---|---|---|
| string[] | 否 | 收件人邮箱地址 | |
| cc | string[] | 否 | 抄送人邮箱地址 | 
| bcc | string[] | 否 | 密送人邮箱地址 | 
| subject | string | 否 | 邮件主题 | 
| body | string | 否 | 邮件正文 | 
| stream | string[] | 否 | 邮件附件 URI | 
| 开发步骤 | 
- 导入 ohos.app.ability.common模块。
- 构造 Want 对象并设置参数。
- 调用 startAbilityByType接口,传入类型mail和 Want 对象。
- 处理回调结果。
 示例代码
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: ["xxx@example.com", "yyy@example.com"],
      cc: ["xxx@example.com", "yyy@example.com"],
      bcc: ["xxx@example.com", "yyy@example.com"],
      subject: "邮件主题",
      body: "邮件正文",
      stream: ["file:///system/app/ExamplePhotoEditorAbility/ExamplePhotoEditorAbility.ets"]
    };
    const abilityStartCallback: common.AbilityStartCallback = {
      onError: (code: number, name: string, message: string) =>
      onError: (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);
  }
}
总结:
拉起指定类型的应用是应用间跳转的另一种方式,它能够方便用户选择打开特定类型的应用,而不是指定具体的应用,我们可以根据实际需求选择合适的垂域应用类型。
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号