h5想要唤起鸿蒙app如何处理?

h5想要唤起鸿蒙app如何处理?想要通过分享链接,打开后跳转h5进行唤醒和跳转至app. 有相关文档吗?

阅读 734
1 个回答

你可以使用javaScriptProxy能够实现在前端页面调度应用侧函数,在应用侧通过startAbility拉起指定的Ability 前端页面调用参考如下代码:

import Want from '@ohos.app.ability.Want'; 
import common from '@ohos.app.ability.common'; 
import web_webview from '@ohos.web.webview'; 
import { hilog } from '@kit.PerformanceAnalysisKit'; 
 
@Entry 
@Component 
struct Index { 
 
  controller: TextInputController = new TextInputController(); 
 
  webviewController: web_webview.WebviewController = new web_webview.WebviewController(); 
 
  aboutToAppear() { 
    // 配置Web开启调试模式 
    web_webview.WebviewController.setWebDebuggingAccess(true); 
  } 
 
  build() { 
    Row() { 
      Column() { 
        //h5跳转 
        Web({ src: $rawfile('index.html'), controller: this.webviewController }).onControllerAttached(() => { 
          // 传递runJavaScript侧代码方法。 
          this.webviewController.registerJavaScriptProxy(new JSImpl(), 'android', ['shareAction']) 
          this.webviewController.refresh(); 
        }).onPageEnd(() => { 
          hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
        }) 
          .onConsole((event) => { 
            if (event) { 
              console.log('getMessage:' + event.message.getMessage()) 
            } 
            return false 
          }) 
 
      }.width('100%') 
    }.height('100%') 
  } 
} 
class JSImpl { 
  /** 
   * JS调用端侧方法 
   * 分享 
   */ 
  shareAction() { 
    console.log('跳转应用商店') 
    const appId: string = 'xxx'; 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
    const want: Want = { uri: `store://appgallery.huawei.com/app/detail?id=${appId}` }; 
    const context = getContext(this) as common.UIAbilityContext; 
    context.startAbility(want) 
      .then(() => { //拉起成功 
      }).catch(() => { // 拉起失败 
    }); 
  } 
}
<!-- index.html --> 
<!DOCTYPE html> 
<html> 
<body> 
<button type="button" onclick="callArkTS()">h5点击跳转到HarmonyOS版应用市场详情页面</button> 
<script> 
    // Click Me!触发前端页面callArkTS()函数执行JavaScript传递的代码。 
    function callArkTS() { 
    const shareAndroid = window.android.shareAction(); 
    shareAndroid(); 
    } 
</script> 
</body> 
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进