【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"
              }
            ]
          }
        ]
      }
    ]
  } 
}
相关推荐
learning-striving1 小时前
eNSP下载安装(eNsp、WinPcap、Wireshark、VirtualBox下载安装)
网络·计算机网络·华为·路由器·ensp·交换机
陈无左耳、1 小时前
HarmonyOS学习第4天: DevEco Studio初体验
学习·华为·harmonyos
月未央3 小时前
HarmonyOS Next 开发系列:Local 状态管理实践
harmonyos
openinstall全渠道统计16 小时前
免填邀请码工具:赋能六大核心场景,重构App增长新模型
android·ios·harmonyos
shaodong112316 小时前
鸿蒙系统-同应用跨设备数据同步(分布式功能)
分布式·华为·harmonyos
敢嗣先锋16 小时前
鸿蒙5.0实战案例:基于ImageKit对图片进行处理
移动开发·harmonyos·arkui·组件化·鸿蒙开发
陈无左耳、16 小时前
HarmonyOS学习第3天: 环境搭建开启鸿蒙开发新世界
学习·华为·harmonyos
敢嗣先锋17 小时前
鸿蒙5.0实战案例:基于ArkUI的验证码实现
移动开发·harmonyos·openharmony·arkui·鸿蒙开发
别说我什么都不会19 小时前
鸿蒙轻内核M核源码分析系列十二 事件Event
操作系统·harmonyos
Huang兄21 小时前
鸿蒙-canvas-刮刮乐
华为·harmonyos