传递并返回来自 javascript 和 android 的值,并用作手机间隙插件
我想为手机创建一个插件,它可以在 javascript 和 android 之间传递并返回值。
有人可以提出关于如何做到这一点的任何想法吗?
I want to create a plugin for phone which pass and returns the value between javascript and android.
Can anybody suggest any ideas on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其实,这并不是很难。在这里,我将向您展示如何从页面内的 javascript 调用本机代码,反之亦然:
从 Web 视图中调用本机代码:
创建 Web 视图时添加 javascript 接口(基本上是 java 类,其方法将在 Web 视图中通过 javascript 公开调用。javascript
接口类本身的定义(这是我从我的另一个答案中获取的示例类,并打开视频在本机意图中)
现在,如果您想从页面的 HTML 中调用此代码,您可以提供以下方法:
很简单,不是吗?
从本机代码调用 javascript 代码
这也很简单,假设在 WebView 中加载的 HTML 代码中定义了 javascript 函数:
然后通过本机代码中的 WebView 调用此函数,如下所示:
Actually, this is not very difficult. Here, I will show you how to call native code from javascript within the page and vice-versa:
Calling native code from within web view:
When creating the web view add javascript interface (basically java class whose methods will be exposed to be called via javascript in the web view.
The definition of the javascript interface class itself (this is exemplary class I took from another answer of mine and opens video in native intent)
Now if you want to call this code form the HTML of the page you provide the following method:
Easy isn't it?
Calling javascript code from native code:
This is also simple suppose in the code of the HTML loaded in WebView you have javascript function defined:
Then you call this function through the WebView in the native code like that:
这是教程用于创建 PhoneGap 插件。此外,ChildBrowser 插件的说明特别重要好的。
Here's a tutorial for creating a PhoneGap Plugin. Also the instructions for the ChildBrowser plugin are especially good.