0

Is there any way to call Java from JavaScript in Webview like this

https://github.com/ochameau/NativeBridge

in Android?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.