【HarmonyOS】判断应用是否已安装

【HarmonyOS】判断应用是否已安装

前言

在鸿蒙中判断应用是否已安全,只是通过包名是无法判断应用安装与否。在鸿蒙里新增了一种判断应用安装的工具方法,即:canOpenLink。

使用该工具函数的前提是,本应用配置了查询标签querySchemes,并且目标应用设置了skills-uris后,会有link完成的串儿,例如:targetAppxx://test.example.com/open

所以需要知道目标方应用是否安装,需要知道对方完整的link串才行。

本应用设置

dart 复制代码
{
  "module": {
    "querySchemes": [
      "targetAPPxx" // 替换为目标应用的scheme
    ]
  }
}

判断安装与否代码示例:

dart 复制代码
import { hilog } from '@kit.PerformanceAnalysisKit';
import { bundleManager } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Index {

  onClickCheckApp = ()=>{
    this.canOpenApp();
  }

  private canOpenApp(){
    try {
      let link = 'targetAPPxx://test.example.com/open'; // 替换成你目标应用的link串儿
      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);
    }
  }

  build() {
    RelativeContainer() {
      Text("点击检查是否安装")
        .id('IndexHelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .onClick(this.onClickCheckApp)
    }
    .height('100%')
    .width('100%')
  }
}

目标应用设置

所以link串儿是由 scheme://host/pathStartWith 组成。例如:targetAppxx://test.example.com/open

dart 复制代码
{
  "module": {
    //...
    "abilities": [
      {
        //...
        "skills": [
          {
            "uris": [
              {
                "scheme": "targetAppxx",
                "host": "test.example.com",
                "pathStartWith": "oepn"
              }
            ]
          }
        ]
      }
    ]
  } 
}
相关推荐
ajassi200028 分钟前
开源 Arkts 鸿蒙应用 开发(六)数据持久--文件和首选项存储
linux·开源·harmonyos
夏至春来-美美41 分钟前
微信获取access_token授权的两种不同情况
微信·php·微信公众平台
塞尔维亚大汉1 小时前
鸿蒙内核源码分析(共享内存) | 进程间最快通讯方式
harmonyos·源码阅读
mon_star°2 小时前
搭建一款结合传统黄历功能的日历小程序
微信·微信小程序·小程序·微信公众平台
生如夏花℡5 小时前
HarmonyOS学习记录4
学习·华为·harmonyos
九章云极AladdinEdu5 小时前
华为昇腾NPU与NVIDIA CUDA生态兼容层开发实录:手写算子自动转换工具链(AST级代码迁移方案)
人工智能·深度学习·opencv·机器学习·华为·数据挖掘·gpu算力
xq95275 小时前
编程之路2025年中总结,勇往直前 再战江湖
harmonyos
枫叶丹46 小时前
【HarmonyOS Next之旅】DevEco Studio使用指南(四十二) -> 动态修改编译配置
华为·harmonyos·deveco studio·harmonyos next
众乐 认证6 小时前
HUAWEI HiCar6.0的新变化
华为·hicar
前端世界17 小时前
鸿蒙UI开发全解:JS与Java双引擎实战指南
javascript·ui·harmonyos