【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"
              }
            ]
          }
        ]
      }
    ]
  } 
}
相关推荐
智驾1 小时前
什么是鸿蒙南向开发?什么是北向开发?
harmonyos·南向开发·北向开发
周胡杰1 小时前
weibo_har鸿蒙微博分享,单例二次封装,鸿蒙微博,微博登录
华为·harmonyos
塞尔维亚大汉2 小时前
【鸿蒙南向开发】OpenHarmony小型系统内核(LiteOS-A)【扩展组件】下
物联网·嵌入式·harmonyos
智驾3 小时前
Openharmony 和 HarmonyOS 区别?
harmonyos·openharmony
脑极体3 小时前
寻找AI大模型时代的存力破壁人:华为的行与思
人工智能·华为
别说我什么都不会3 小时前
【仓颉三方库】工具类——zip4cj & zlib4cj
harmonyos
ghjhjjjbjibh7 小时前
AI大模型 —— 国产大模型 —— 华为大模型
人工智能·华为
高木的小天才15 小时前
鸿蒙中的并发线程间通信、线程间通信对象
前端·华为·typescript·harmonyos
脑极体19 小时前
开源鸿蒙,给机器人带来了什么?
华为·机器人·开源·harmonyos
Aqua Cheng.19 小时前
25.4.22华为--算法真题整理(2025年4月22日)
java·算法·leetcode·华为·面试