【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"
              }
            ]
          }
        ]
      }
    ]
  } 
}
相关推荐
lbb 小魔仙41 分钟前
【HarmonyOS实战】OpenHarmony + RN:自定义 useForm 表单管理
harmonyos
早點睡3901 小时前
高级进阶 ReactNative for Harmony 项目鸿蒙化三方库集成实战:react-native-video
react native·华为·harmonyos
开开心心就好1 小时前
发票合并打印工具,多页布局设置实时预览
linux·运维·服务器·windows·pdf·harmonyos·1024程序员节
前端不太难2 小时前
HarmonyOS 游戏项目,从 Demo 到可上线要跨过哪些坑
游戏·状态模式·harmonyos
全栈探索者2 小时前
列表渲染不用 map,用 ForEach!—— React 开发者的鸿蒙入门指南(第 4 期)
react.js·harmonyos·arkts·foreach·列表渲染
打破砂锅问到底0073 小时前
AI 驱动开发实战:10分钟从零构建「微信群相册」小程序
人工智能·微信·小程序·ai编程
试着3 小时前
【huawei】机考整理
学习·华为·面试·机试
一只大侠的侠3 小时前
Flutter开源鸿蒙跨平台训练营 Day8获取轮播图网络数据并实现展示
flutter·开源·harmonyos
Lionel6894 小时前
鸿蒙Flutter跨平台开发:首页特惠推荐模块的实现
华为·harmonyos
盐焗西兰花4 小时前
鸿蒙学习实战之路-Reader Kit自定义页面背景最佳实践
学习·华为·harmonyos