鸿蒙与web混合开发双向通信

鸿蒙与web混合开发双向通信用runJavaScript和registerJavaScriptProxy

web

entry/src/main/resources/rawfile/1.html

bash 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>混合开发</title>
</head>
<body>
    <div></div>
    <img src="./a.png" alt="">
    <button onclick="selectImg()">打开相册</button>
</body>
</html>
<script>
    //直接写js代码
    function changeImg(){
        //1.获取img这个元素
        const img = document.querySelector('img')
        //2.修改元素的属性
        img.src = './b.png'
    }
    async function selectImg(){
      //通知鸿蒙,让鸿蒙设备打开相册
       const res = await harmonyDevice.selectImage()
       const div = document.querySelector('div')
       div.innerText = JSON.stringify(res)
    }

</script>

鸿蒙

bash 复制代码
import { webview } from '@kit.ArkWeb'
import { photoAccessHelper } from '@kit.MediaLibraryKit'
import { promptAction } from '@kit.ArkUI'

@Entry
@Component
struct Page {
  webController:WebviewController = new webview.WebviewController()
  selectImg(){
    const picker = new photoAccessHelper.PhotoViewPicker()
    return picker.select()
  }
  build() {
    Column(){
      // 1.想让网页干嘛,直接调网页的JS方法
      Button('鸿蒙-改变网页图片')
        .onClick(()=>{
            // 控制器
            this.webController.runJavaScript('changeImg()')
        })
      // 2.网页控制鸿蒙测
      Button('注入鸿蒙的事件')
        .onClick(()=>{
          promptAction.showToast({
            message:'注入成功'
          })
          // 1.先给网页一个对象,这个对象上包含了所有网页要用到的方法集合
          // 2.给这个对象起一个名字,让网页可以使用这个对象
          // 3.确定网页可以使用的方法集合
          this.webController.registerJavaScriptProxy(
            {
              selectImage:async()=>{
                const res = await this.selectImg()
                return res
              }
            },
            "harmonyDevice",
            ["selectImage"]
          )
        })
      // 1.加载的地址
      // 2.控制
      Button('刷新页面')
        .onClick(()=>{
           this.webController.refresh()
        })
      Web({
        // 本地:
        src:$rawfile('1.html'),
        // src:'https://www.amap.com/',
        controller:this.webController
      })
    }
    .width('100%')
    .height('100%')
  }
}
相关推荐
ShuiShenHuoLe3 小时前
管理数据的状态
harmonyos·鸿蒙
坚果派·白晓明2 天前
三方库ada
harmonyos·鸿蒙·openharmony
云和数据.ChenGuang2 天前
鸿蒙餐饮系统:全场景智慧餐饮新范式
人工智能·机器学习·华为·数据挖掘·harmonyos·鸿蒙·鸿蒙系统
云和数据.ChenGuang3 天前
鸿蒙智联,极智共生:HarmonyOS与MiniMax智能体的融合新纪元
华为·harmonyos·鸿蒙
UnicornDev3 天前
【HarmonyOS 6】今日统计卡片实战:运动记录数据概览
华为·harmonyos·arkts·鸿蒙·鸿蒙系统
ShuiShenHuoLe3 天前
组件的状态ComponentV2
harmonyos·鸿蒙
仓颉编程语言4 天前
CangjieSkills 正式开源:为仓颉 AI 编程打造的“技能增强“方案,实测降低 60% 费用
华为·ai编程·鸿蒙·仓颉编程语言
左手厨刀右手茼蒿5 天前
Flutter 三方库 all_lint_rules_community 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、严谨、基于全量社区 Lint 规则的工业级静态代码质量与安全审计引擎
flutter·harmonyos·鸿蒙·openharmony·all_lint_rules_community
雷帝木木5 天前
Flutter for OpenHarmony:Flutter 三方库 cbor 构建 IoT 设备的极致压缩防窃协议(基于标准二进制 JSON 表达格式)
网络·物联网·flutter·http·json·harmonyos·鸿蒙
王码码20355 天前
Flutter 三方库 servicestack 的鸿蒙化适配指南 - 实现企业级 Message-based 架构集成、支持强类型 JSON 序列化与跨端服务调用同步
flutter·harmonyos·鸿蒙·openharmony·message-based