【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 小时前
React Native开发鸿蒙运动健康类应用的项目实践记录
react native·harmonyos·harmony os5·运动健康
二流小码农8 小时前
鸿蒙开发:实现一个标题栏吸顶
android·ios·harmonyos
坚果的博客8 小时前
uniappx插件nutpi-idcard 开发与使用指南(适配鸿蒙)
华为·harmonyos
程序员小刘8 小时前
【HarmonyOS 5】 社交行业详解以及 开发案例
华为·harmonyos
软件测试小仙女8 小时前
鸿蒙APP测试实战:从HDC命令到专项测试
大数据·软件测试·数据库·人工智能·测试工具·华为·harmonyos
Raink老师8 小时前
鸿蒙任务项设置案例实战
harmonyos·鸿蒙·案例实战
程序员小刘8 小时前
【HarmonyOS 5】 影视与直播详以及 开发案例
华为·harmonyos
程序员小刘9 小时前
鸿蒙【HarmonyOS 5】 (React Native)的实战教程
react native·华为·harmonyos
王二蛋与他的张大花9 小时前
HarmonyOS运动语音开发:如何让运动开始时的语音播报更温暖
harmonyos
Android研究员9 小时前
华为仓颉语言初识:并发编程之同步机制(上)
harmonyos