【API 12 学习笔记】HarmonyOS 鸿蒙Next利用API 12新增的方法canOpenLink可以判断是否安装了某个APP,这里以支付宝、高德地图、知乎为例。

发布于 1周前 作者 sinazl 来自 鸿蒙OS

【API 12 学习笔记】HarmonyOS 鸿蒙Next利用API 12新增的方法canOpenLink可以判断是否安装了某个APP,这里以支付宝、高德地图、知乎为例。

cke_680.png

cke_154.png

src/main/module.json5

{
“module”: {
“querySchemes”: [
“alipays”,
“amapuri”,
“zhihu”,
],
“name”: “entry”,
“type”: “entry”,
“description”: “$string:module_desc”,
“mainElement”: “EntryAbility”,
“deviceTypes”: [
“phone”,
“tablet”,
“2in1”
],

<span class="hljs-string"><span class="hljs-string">"deliveryWithInstall"</span></span>: <span class="hljs-literal"><span class="hljs-literal">true</span></span>,
<span class="hljs-string"><span class="hljs-string">"installationFree"</span></span>: <span class="hljs-literal"><span class="hljs-literal">false</span></span>,
<span class="hljs-string"><span class="hljs-string">"pages"</span></span>: <span class="hljs-string"><span class="hljs-string">"$profile:main_pages"</span></span>,
<span class="hljs-string"><span class="hljs-string">"abilities"</span></span>: [
  {
    <span class="hljs-string"><span class="hljs-string">"name"</span></span>: <span class="hljs-string"><span class="hljs-string">"EntryAbility"</span></span>,
    <span class="hljs-string"><span class="hljs-string">"srcEntry"</span></span>: <span class="hljs-string"><span class="hljs-string">"./ets/entryability/EntryAbility.ets"</span></span>,
    <span class="hljs-string"><span class="hljs-string">"description"</span></span>: <span class="hljs-string"><span class="hljs-string">"$string:EntryAbility_desc"</span></span>,
    <span class="hljs-string"><span class="hljs-string">"icon"</span></span>: <span class="hljs-string"><span class="hljs-string">"$media:layered_image"</span></span>,
    <span class="hljs-string"><span class="hljs-string">"label"</span></span>: <span class="hljs-string"><span class="hljs-string">"$string:EntryAbility_label"</span></span>,
    <span class="hljs-string"><span class="hljs-string">"startWindowIcon"</span></span>: <span class="hljs-string"><span class="hljs-string">"$media:startIcon"</span></span>,
    <span class="hljs-string"><span class="hljs-string">"startWindowBackground"</span></span>: <span class="hljs-string"><span class="hljs-string">"$color:start_window_background"</span></span>,
    <span class="hljs-string"><span class="hljs-string">"exported"</span></span>: <span class="hljs-literal"><span class="hljs-literal">true</span></span>,
    <span class="hljs-string"><span class="hljs-string">"skills"</span></span>: [
      {
        <span class="hljs-string"><span class="hljs-string">"entities"</span></span>: [
          <span class="hljs-string"><span class="hljs-string">"entity.system.home"</span></span>
        ],
        <span class="hljs-string"><span class="hljs-string">"actions"</span></span>: [
          <span class="hljs-string"><span class="hljs-string">"action.system.home"</span></span>
        ]
      }
    ]
  }
]

} }<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

src/main/ets/pages/Page006.ets

import bundleManager from ‘@ohos.bundle.bundleManager’;
import { BusinessError } from ‘@ohos.base’;
import hilog from ‘@ohos.hilog’;
import { promptAction } from ‘@kit.ArkUI’;

@Entry @Component struct DocumentListPage { arr: ESObject[] = [ { appName: ‘支付宝’, link: ‘alipays://’, }, { appName: ‘高德地图’, link: ‘amapuri://’, }, { appName: ‘知乎’, link: ‘zhihu://’, }, ]

checkApp(link: string, appName: string) { try { let data = bundleManager.canOpenLink(link); hilog.info(0x0000, ‘testTag’, ‘canOpenLink successfully: %{public}s’, JSON.stringify(data)); if (data) { promptAction.showToast({ message: ${appName} APP 已安装, duration: 2000, bottom: ‘500lpx’ }); } else { promptAction.showToast({ message: ${appName} APP 未安装, duration: 2000, bottom: ‘500lpx’ }); } } catch (err) { if (err[‘code’] == 17700056) { /{ “module”: { “querySchemes”: [ “alipays”, ],/ promptAction.showToast({ message: ‘请在module.json5配置link,参考注释 querySchemes’, duration: 2000, bottom: ‘500lpx’ }); } else { promptAction.showToast({ message: ‘未知异常’, duration: 2000, bottom: ‘500lpx’ }); } let message = (err as BusinessError).message; hilog.error(0x0000, ‘testTag’, ‘canOpenLink failed: %{public}s’, message); } }

build() { Row() { Column({ space: 10 }) { ForEach(this.arr, (item: ESObject, index: number) => { Button(检查当前手机是否安装了 ${item[<span class="hljs-string"><span class="hljs-string">'appName'</span></span>]} APP)// 添加跳转功能 .onClick(() => { this.checkApp(item[‘link’], item[‘appName’]) }) }) } } } }<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button>

参考:https://developer.huawei.com/consumer/cn/forum/topic/0203154118987747020?fid=0109140870620153026



关于【API 12 学习笔记】HarmonyOS 鸿蒙Next利用API 12新增的方法canOpenLink可以判断是否安装了某个APP,这里以支付宝、高德地图、知乎为例。的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。

15 回复

知乎上架了?

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17

真机 mate 60 pro 系统软件版本 3.0.0.22 (支持API 12) 应用商店可以下载 知乎 。能安装到手机上。 但是,一点击就闪退🤣

我用的笨办法,搜索已上架的android、ios app中对应的scheme是啥,然后在鸿蒙里挨个试一下,赌的就是开发人员懒得换scheme前缀。🤣

好的谢谢我也试试

鸿蒙版云闪付APP的scheme是什么?方便提供一下吗?

没查到,我猜可能是还没正式上线,scheme功能还没写吧。

大佬为啥检测不到
petal maps(华为自带的地图)

不知道华为自带的地图Scheme是多少,你可能需要提工单问问。

大佬,请教一下querySchemes有数量限制吗?iOS 好像是 top50 生效
querySchemes是否需要加上https

不需要

请问知道怎么用支付宝三方登录吗

鸿蒙Next多线程、HarmonyOS Next支付宝支付、HarmonyOS Next消息推送、后端集成推送、指定设备自定义推送学习:https://www.itying.com/goods-1204.html

回到顶部