javascript - 有没有办法在Webview中从JavaScript调用Java?

标签 javascript android

有没有办法像这样在Webview中从JavaScript调用Java

https://github.com/ochameau/NativeBridge

在 Android 中?

最佳答案

使用addJavascriptInterface()将Java对象添加到WebView的JavaScript环境中:

browser.addJavascriptInterface(new Locater(), "locater");

然后,您的 JavaScript 可以引用您注入(inject)的虚构对象上的方法:

<script language="javascript">
    function whereami() {
        var location=JSON.parse(locater.getLocation());

        document.getElementById("lat").innerHTML=location.lat;
        document.getElementById("lon").innerHTML=location.lon;
    }
</script>

这些方法是在您与 addJavascriptInterface() 一起使用的 Java 对象上实现的。

Here is a complete sample project从中提取的这些代码 fragment 演示了这一点。

关于javascript - 有没有办法在Webview中从JavaScript调用Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11954169/

相关文章:

javascript - 查找与 IE6/7/8 不兼容的核心 javascript 调用

javascript - 按负整数过滤 jQuery 数据表

android - ClassNotFoundException : Didn't find class "com.example.blogapp.whateverString"

android - 显示 DialogFragment 动画从一个点开始

javascript - InDesign 脚本 : How to print specific spot color in separations?

javascript - Jquery Hover 和 Click 在 IE 中不起作用

javascript - 通过 setTimeOut 再次调用 setState 来响应更改状态

android - GoogleMaps setMyLocationEnabled(true) 不更新 (Genymotion)

java - Android 通知生成器 setSmallIcon()

android - 推送通知 - Firebase Control Messaging (FCM) - 无法运行该应用程序(未找到默认 Activity )