Skip to content

Webview Bridge JavascriptObjects

Ender edited this page Mar 7, 2022 · 1 revision

JavascriptObjects are an easy way to bridge the gap between Java and the webview's Javascript context.

Example Usage:

webview
    .getBridge()
    .defineObject("test", new JavascriptObject() {
        @JavascriptValue
        private int twelve = 12;

        @JavascriptFunction
        public long nanoTime() {
            return System.nanoTime();
        }

        @JavascriptFunction
        public void testThrow() {
            throw new IllegalStateException("Test throw.");
        }
});

Demo

Note that await is required for both functions and properties, since they IPC mechanism is asynchronous. If you wish, you may also pass functions and receive them as JavascriptCallback in Java. Calling JavascriptCallback#invoke(WebviewBridge, Object... args) will allow you to call these functions from Java.

Clone this wiki locally