harmony 鸿蒙(可选)使用canOpenLink判断应用是否可访问

  • 2025-06-12
  • 浏览 (6)

(可选)使用canOpenLink判断应用是否可访问

使用场景

在应用A想要拉起应用B的场景中,应用A可先调用canOpenLink接口判断应用B是否可访问,如果可访问,再拉起应用B。

说明:

canOpenLink接口不支持判断以App Linking方式跳转的目标应用是否安装。

约束限制

在entry模块的module.json5文件中的querySchemes字段中,最多允许配置50个URL scheme。

接口说明

canOpenLink是bundleManager提供的支持判断目标应用是否可访问的接口。 匹配规则请参考显式Want与隐式Want匹配规则

操作步骤

调用方操作步骤

  1. 在entry模块的module.json5文件中配置querySchemes属性,声明想要查询的URL scheme。

    {
      "module": {
        //...
        "querySchemes": [
          "app1Scheme"
        ]
      }
    }
    
  2. 导入ohos.bundle.bundleManager模块。

  3. 调用canOpenLink接口。

    import { bundleManager } from '@kit.AbilityKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    try {
      let link = 'app1Scheme://test.example.com/home';
      let canOpen = bundleManager.canOpenLink(link);
      hilog.info(0x0000, 'testTag', 'canOpenLink successfully: %{public}s', JSON.stringify(canOpen));
    } catch (err) {
      let message = (err as BusinessError).message;
      hilog.error(0x0000, 'testTag', 'canOpenLink failed: %{public}s', message);
    }
    

目标方操作步骤

在module.json5文件中配置uris属性。

{
  "module": {
    //...
    "abilities": [
      {
        //...
        "skills": [
          {
            // actions不能为空,actions为空会造成目标方匹配失败
            "actions": ["ohos.want.action.home"],
            "uris": [
              {
                "scheme": "app1Scheme",
                "host": "test.example.com",
                "pathStartWith": "home"
              }
            ]
          }
        ]
      }
    ]
  } 
}

FAQ

  1. 为什么querySchemes中最多允许配置50个URL scheme?

canOpenLink()接口提供了判断应用是否可以访问的能力。通过该能力,应用可以间接获取到指定应用是否安装等信息。

为了保护系统安全和用户隐私,避免恶意应用扫描应用安装列表等行为,要求开发者在使用canOpenLink()接口时必须配置querySchemes属性,且最多允许配置50个URL scheme。

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit(程序框架服务)

harmony 鸿蒙获取应用异常退出原因

harmony 鸿蒙UIAbility备份恢复

harmony 鸿蒙使用显式Want启动应用组件

harmony 鸿蒙Ability Kit简介

harmony 鸿蒙AbilityStage组件容器

harmony 鸿蒙访问DataAbility

harmony 鸿蒙FA模型访问Stage模型DataShareExtensionAbility

harmony 鸿蒙常见action与entities(不推荐使用)

harmony 鸿蒙API切换概述

0  赞