应用中分享链接,将链接复制到手机浏览器之后,如何拉起应用(HarmonyOS 鸿蒙Next),如果应用不存在,如何跳转至应用市场?

应用中分享链接,将链接复制到手机浏览器之后,如何拉起应用(HarmonyOS 鸿蒙Next),如果应用不存在,如何跳转至应用市场? 应用中分享链接,将链接复制到手机浏览器之后,如何拉起应用,如果应用不存在,如何跳转至应用市场?

3 回复

实现方式:

浏览器网页拉起应用,主要通过Deep Linking方式实现。

1、在鸿蒙项目工程中entry工程的module.json5文件下配置skill标签。

{
  "skills": [
    {
      "entities": [
        "entity.system.home"
      ],
      "actions": [
        "action.system.home"
      ]
    },
    // 上面对象是默认配置,下面位Deep Linking配置
    {
      // actions不能为空,actions为空会造成目标方匹配失败。
      "actions": [
        "ohos.want.action.viewData"
      ],
      "uris": [
        {
          // scheme必选,可以自定义,如https、link、1234等,任意字符串均可,需要替换为实际的scheme
          "scheme": "link",
          // host必选,配置待匹配的域名,或则任意唯一的字符串。
          "host": "router/app123"
        }
      ]
    } // 新增一个skill对象,用于跳转场景。如果存在多个跳转场景,需配置多个skill对象
  ]
}

2、将应用安装至手机

3、编写H5网页,发布到公网,让手机浏览器可以访问,打开网页

// index.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>

<body>
    <h1>Hello World</h1>
    <!--方式一、通过绑定事件window.open方法实现跳转-->
    <button class="doOpenLink" onclick="doOpenLink()">打开应用</button>
</body>

<script>
    function doOpenLink() {
        window.open("link://router/app123")
    }
</script>
</html>

4、点击按钮“打开应用”,就会拉起上面安装的应用。

5、如果应用没有安装,window.open应该会拉起失败,捕获异常,跳转至应用市场。在应用市场中,找到目标应用,点击分享,就可以获取到目标应用链接。当拉起应用失败之后,再通过window.open(‘应用应用市场链接’)跳转至应用市场。

上述逻辑,若是有大佬有更好方式,还请留言分享

更多关于应用中分享链接,将链接复制到手机浏览器之后,如何拉起应用(HarmonyOS 鸿蒙Next),如果应用不存在,如何跳转至应用市场?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


这块简单分析,也不属于鸿蒙这边开发,通过浏览器进入链接,H5页面,应该H5识别为鸿蒙设备,然后做对应下载/打开处理吧。

在HarmonyOS(鸿蒙Next)中,通过分享链接拉起应用并处理应用不存在的情况,可以使用IntentDeep Link机制。以下是具体实现步骤:

  1. 配置Deep Link:在应用的config.json文件中,定义uriaction等属性,确保系统能够识别并处理特定的链接格式。

  2. 处理Intent:在应用的主Ability中,重写onStart方法,解析Intent中的uri,并根据uri跳转到相应的页面。

  3. 检查应用是否存在:使用AbilityManagerqueryAbilityInfo方法检查目标应用是否已安装。如果应用不存在,构造一个跳转到应用市场的Intent,并调用startAbility方法启动。

  4. 跳转至应用市场:如果应用不存在,构造一个包含应用包名的Intent,并调用startAbility方法跳转到应用市场。

示例代码片段如下:

import Ability from '@ohos.application.Ability';
import AbilityManager from '@ohos.application.AbilityManager';
import Intent from '@ohos.application.Intent';

export default class MainAbility extends Ability {
    onStart(intent) {
        let uri = intent.uri;
        if (uri) {
            // 处理Deep Link
            this.handleDeepLink(uri);
        }
    }

    handleDeepLink(uri) {
        let targetApp = 'com.example.targetapp';
        let abilityManager = AbilityManager.getAbilityManager();
        abilityManager.queryAbilityInfo({
            bundleName: targetApp
        }, (err, data) => {
            if (err || !data) {
                // 应用不存在,跳转至应用市场
                let marketIntent = new Intent();
                marketIntent.action = 'android.intent.action.VIEW';
                marketIntent.uri = 'market://details?id=' + targetApp;
                this.context.startAbility(marketIntent);
            } else {
                // 应用存在,拉起应用
                let targetIntent = new Intent();
                targetIntent.action = 'android.intent.action.VIEW';
                targetIntent.uri = uri;
                this.context.startAbility(targetIntent);
            }
        });
    }
}

通过以上步骤,可以在HarmonyOS中实现通过链接拉起应用,并在应用不存在时跳转至应用市场。

回到顶部