Is there any way to call Java from JavaScript in Webview like this
in Android?
Is there any way to call Java from JavaScript in Webview like this
in Android?
Use addJavascriptInterface() to add a Java object to the JavaScript environment of the WebView:
browser.addJavascriptInterface(new Locater(), "locater");
Your JavaScript can then reference methods on the fictitious object you injected:
<script language="javascript">
function whereami() {
var location=JSON.parse(locater.getLocation());
document.getElementById("lat").innerHTML=location.lat;
document.getElementById("lon").innerHTML=location.lon;
}
</script>
where those methods are implemented on the Java object you used with addJavascriptInterface().
Here is a complete sample project from which these bits of code were pulled that demonstrates this.