Android 移动应用程序中 webview 和本机代码之间的通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21943751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:20:38  来源:igfitidea点击:

Communication between webview and native code in a mobile app

androidioswebviewnative-code

提问by user422688

I have to develop an e-commerce mobile app. In order to show the products into the app, I have to use a webview that point to a mobile web site. Associate to each product there is the “add to basket” button. Also this button is an element of the mobile site. In the tab bar (and not in the webview) of the mobile app there is the icon of the basket with the number of elements of the basket. How the webview can know that the user has added one or more element to the basket and send to this information to the native mobile app that can update the icon of the basket?

我必须开发一个电子商务移动应用程序。为了在应用程序中显示产品,我必须使用指向移动网站的 webview。与每个产品相关联的是“添加到购物篮”按钮。此按钮也是移动站点的一个元素。在移动应用程序的标签栏中(而不是在 webview 中),有一个带有篮子元素数量的篮子图标。webview 如何知道用户已将一个或多个元素添加到购物篮并将此信息发送到可以更新购物篮图标的本机移动应用程序?

回答by David C Adams

Javascript. You bind the WebView to your Android application with the JavascriptInterface. Here are some reference docs: Building Web Apps in WebView

Javascript。您使用 JavascriptInterface 将 WebView 绑定到您的 Android 应用程序。以下是一些参考文档:在 WebView 中构建 Web 应用程序