Android webapp 中调用 JS 的原生代码
我正在编写一个Android“网络”应用程序(我不会将应用程序上传到网络,而是在应用程序资源中设置HTML + JS)。这意味着,GUI 将是 HTML5,我将使用另一个“本机”线程从麦克风读取数据,并希望将“解析后的文本”发送到 HTML5/JS 代码。
这可能吗?在 Android 代码中,我看到如何从 JS 调用本机代码(我想要相反的)。
I am writing an Android "web" app (I will not upload the APP to the web, but set the HTML+JS inside the application resources). That means, the GUI will be HTML5, and I will use another "native" thread to read from the microphone and hopefully send the "parsed text" to the HTML5/JS code.
Is this possible? In the Android code, I see how can I call native code from JS (and I want the opposite).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,Android 在这方面确实很出色。
我正在发布来自我制作的随机示例应用程序的示例代码,但这应该可以帮助您继续。
首先,假设您必须为 WebView 控制类添加全局变量:
在控制 WebView 的 Android 类中(在
onCreate()
期间或设置 WebView 时):WebView 控制类的内部类:
Android 端就是这样,现在通过 Javascript 使用它:
通过 JavaScript 获取名字和姓氏:
如果你想举起吐司,设置一个 javascript,其功能为:
编辑:
我还没有尝试过这个,但是搜索产生了这个,尝试一下,它应该可以工作。
如果你想用在另一个方向:
JS:
Android:
Yes, Android rocks at this actually.
I'm posting example code from a random example app that I made but this should get you going.
Lets start by supposing you have to global variable to your webview controlling class:
In the Android class controlling the webview (during
onCreate()
or when you setup your webview):Inner class to your webview controlling class:
Thats it for the Android side, now to use it through Javascript:
To get the name and lastname through JavaScript:
If you want to raise the toast, set a javascript whose function would be:
Edit:
I havent tried this, but a search yielded this, try it out, it should work.
If you want to use it in the other direction:
JS:
Android: