DCloud_UNI_OttoJi
DCloud_UNI_OttoJi
  • 发布:2025-09-09 21:06
  • 更新:2025-09-09 21:06
  • 阅读:21

经验分享 如何在鸿蒙应用中唤起鸿蒙应用、元服务

分类:鸿蒙Next

鸿蒙应用如何注册和声明 DeepLink 和 AppLinking 可参考 《通过 URL Scheme 唤起鸿蒙应用

这里重点介绍如何在应用中唤起其他应用和元服务。这里临时提供 uts 方案,后续会做进一步整理。

可参考下面代码, Vue 页面中调用 UTS 暴露的方法

<template>  
  <view>  
    <button @click="openAppFun">唤起 hellouniapp 应用</button>  
    <button @click="openAppByUrlSchemeFun">url schema 唤起应用</button>  
    <button @click="openMpFun">唤起 openMp</button>  
  </view>  
</template>  

<script setup>  
  import {  
    openLink,  
    openMp,  
    openAppByURLScheme  
  } from '@/uni_modules/otto-open-harmony'  

  const openAppByUrlSchemeFun = () => {  

    try {  
      openAppByURLScheme('hellouniapp://router')  
        .then(res => {  
          console.log('ok', res);  
        })  
        .catch(err => {  
          console.log('err', err);  
        })  

    } catch (error) {  
      //TODO handle the exception  
    }  
  }  

  const openAppFun = async () => {  
    console.log('will open');  
    try {  
      openLink('https://uniapp.dcloud.net.cn/tutorial/harmony/runbuild.html')  
        .then(res => {  
          console.log('ok', res);  
        })  
        .catch(err => {  
          console.log('err', err);  
        })  

    } catch (error) {  
      //TODO handle the exception  
    }  
  }  

  const openMpFun = async () => {  
    console.log('will open');  
    try {  
      openMp()  
        .then(res => {  
          console.log('ok', res);  
        })  
        .catch(err => {  
          console.log('err', err);  
        })  
    } catch (error) {  
      //TODO handle the exception  
    }  
  }  
</script>

创建 api uts 插件,放置到 /uni_modules/otto-open-harmony/utssdk/app-harmony/index.uts


import { BusinessError } from '@kit.BasicServicesKit';  
import { , common, OpenLinkOptions ,AtomicServiceOptions} from '@kit.AbilityKit';  

export const openMp = () => {  

  const appId = '5765880207855932629'  

  const options : AtomicServiceOptions = {  
    displayId: 0  
  };  

  return new Promise((resolve, reject) => {  
    UTSHarmony.getUIAbilityContext().openAtomicService(appId,options)  
       .then((result: common.AbilityResult) => {  
        resolve('')  
      })  
      .catch((err : BusinessError) => {  
        reject(err.message)  
      })  
  })  

  // return UTSHarmony.getUIAbilityContext().openAtomicService(appId,options)  
}  

/**  
 * 使用鸿蒙 DeepLink 唤起应用  
 */  
export const openAppByURLScheme = (link : string) => {  

  // const link = 'hellouniapp://router'  

  const openLinkOptions : OpenLinkOptions = {  
    appLinkingOnly: true  
  };  

  return UTSHarmony.getUIAbilityContext().openLink(link,openLinkOptions)  

}  

export const openLink = (link : string, params : UTSJSONObject) => {  

  // 'https://uniapp.dcloud.net.cn/tutorial/harmony/runbuild.html'  

  const opt : OpenLinkOptions = {  
    // appLinkingOnly: true,  
    // parameters: params as OpenLinkOptions['parameters']  
  }  

  return UTSHarmony.getUIAbilityContext().openLink(  
    link,  
    opt,  
    (err, result) => {  
      console.error(`openLink callback error.code: ${JSON.stringify(err)}`);  
      console.info(`openLink callback result: ${JSON.stringify(result.resultCode)}`);  
      console.info(`openLink callback result data: ${JSON.stringify(result.want)}`);  
    }  
  )  

}
0 关注 分享

要回复文章请先登录注册