【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"
              }
            ]
          }
        ]
      }
    ]
  } 
}
相关推荐
安卓开发者5 小时前
鸿蒙NEXT网络通信进阶:全方位优化HTTP传输性能
http·华为·harmonyos
爱笑的眼睛118 小时前
HarmonyOS 应用开发深度解析:ArkTS 状态管理与渲染控制的艺术
华为·harmonyos
爱笑的眼睛118 小时前
深入理解HarmonyOS ArkTS语法:从基础到高级应用开发
华为·harmonyos
yenggd21 小时前
sr mpls te隧道配置案例
网络·华为
爱笑的眼睛111 天前
深入解析HarmonyOS应用开发:ArkTS语法精要与UI组件实践
华为·harmonyos
爱笑的眼睛111 天前
深入浅出 ArkTS:构建响应式 HarmonyOS 应用的现代语法与实践
华为·harmonyos
优质网络系统领域创作者1 天前
华为AC+AP无线网络组网与配置指南
华为
爱笑的眼睛111 天前
深入浅出 ArkTS:HarmonyOS 应用开发的语言基石
华为·harmonyos
安卓开发者1 天前
鸿蒙Next中使用Socket进行网络通信:完整指南与实战
华为·harmonyos
A懿轩A1 天前
【HarmonyOS应用】《账理通》更新啦!
华为·harmonyos