写点什么

【HarmonyOS】鸿蒙原生实现 3DTouch 快捷方式

  • 2025-03-21
    安徽
  • 本文字数:2044 字

    阅读完需:约 7 分钟

开发语言:ArkTs


开发工具:DevEco Studio 5.0.0 Release


API 版本:API 12


demo 演示Gitee:harmony-qrscan.git



需求:长按桌面图片显示快捷入口,通过快捷入口快速打开指定页面。

一、新建配置文件

/resources/base/profile/目录下新建shortcuts_config.json配置文件。


这里配置了扫一扫乘车码两个快捷入口,单个配置项中labelicon分别是快捷入口显示的文字和图标,其中parameters参数下的3DTouchType字段用于区分快捷入口打开 APP 时点击的业务类型,字段名可自定义。


{  "shortcuts": [    {      "shortcutId": "id_scan",      "label": "$string:3DTouch_SCAN",      "icon": "$media:foreground",      "wants": [        {          "bundleName": "com.example.qrcodescan",          "abilityName": "EntryAbility",          "moduleName": "entry",          "parameters": {            "3DTouchType": "scan"           }        }      ]    },    {      "shortcutId": "id_ride",      "label": "$string:3DTouch_RIDE",      "icon": "$media:foreground",      "wants": [        {          "bundleName": "com.example.qrcodescan",          "abilityName": "EntryAbility",          "moduleName": "entry",          "parameters": {            "3DTouchType": "ride"          }        }      ]    }  ]}
复制代码

二、生效配置文件

src/main/module.json5文件中引用配置文件。


module.json5配置文件的abilities标签中,针对需要添加快捷方式的 UIAbility 进行配置metadata标签,使 shortcut 配置文件对该 UIAbility 生效。


{  "module": {    //...    "abilities": [      {        "name": "EntryAbility",        "srcEntry": "./ets/entryability/EntryAbility.ets",        //...        "skills": [          {            "entities": [              "entity.system.home"            ],            "actions": [              "action.system.home"            ]          }        ],        "metadata": [          {            "name": "ohos.ability.shortcuts",            "resource": "$profile:shortcuts_config"          }        ]      }    ]  }}
复制代码

三、拦截快捷方式跳转

1、热启动跳转(APP 退到后台)


EntryAbility.ets文件中实现onNewWant()方法,在方法内拦截快捷方式的类型进行业务跳转。


onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {  // 点击了快捷入口(热启动)  this.shortcutsPushToPage(want);}
复制代码


2、冷启动跳转(APP 进程被杀掉)


EntryAbility.ets文件的onCreate()方法内记录 want 对象,然后在onWindowStageCreate()方法中的windowStage.loadContent()加载内容后处理快捷方式的类型进行业务跳转。


// 快捷入口wantprivate shortcutsWant?: Want;
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { // 冷启动 点击了快捷入口,记录下want对象 if (want) { this.shortcutsWant = want; }}

onWindowStageCreate(windowStage: window.WindowStage): void { // Main window is created, set main page for this ability hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => { if (err.code) { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); // 冷启动 点击了快捷入口,在加载内容后执行跳转页面,这里仅简单实现跳转,真实项目不建议这么写 // 此处做延迟0.5s执行,是为了显示首页后再跳转,否则会先跳转,再显示首页 // 真实项目中可将跳转放到pages/Index页面显示后执行,这里为了方便简单处理 setTimeout(() => { this.shortcutsPushToPage(this.shortcutsWant); }, 500) });}
复制代码


3、处理跳转


/** * 快捷入口跳转页面 * @param want */shortcutsPushToPage(want?: Want) {  if (want) {    let wantParameters = want['parameters'] as object;    if (wantParameters) {      let touchType: string = wantParameters['3DTouchType'];      if (touchType === "scan") {        // 扫一扫        router.pushUrl({url: "pages/HMQRCodeScanPage"});      } else if (touchType === "ride") {        // 乘车码        router.pushUrl({url: "pages/HMQRScanResultPage", params: {"result": "乘车码"}});      }    }    // 跳转后清除记录    this.shortcutsWant = undefined;  }}
复制代码

结尾

如大家发现文章描述有问题或有更好的方案,还请评论回复,一起探讨学习,感谢!官方文档:华为开发者网站:shortcuts

发布于: 刚刚阅读数: 3
用户头像

还未添加个人签名 2025-03-18 加入

还未添加个人简介

评论

发布
暂无评论
【HarmonyOS】鸿蒙原生实现3DTouch快捷方式_鸿蒙_走向菜鸟的菜鸟_InfoQ写作社区