Skip to content Skip to sidebar Skip to footer

How Do I Make Javascript-java Communication Sync In Android Webview?

I've got WebView, JS code inside it. Also I have interface to allow Java method invocation from WebView to Java code. I need to pass data from JS to Java. To do so: webView.loadUrl

Solution 1:

The only (hacky) solution that I've come across is using a one second delay after calling the loadUrl. You may use the addJavaSriptInterface() to do so.

or if the JS processing takes too long you have to use callbacks

<inputtype="button"value="Say hello"onClick="callYourCallbackFunctionHere('Hello Android!')" /><scripttype="text/javascript">functioncallYourCallbackFunctionHere(toast) {
        Android.callAndroidCallback(toast);
    }
</script>

publicclassJavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */JavaScriptInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */publicvoidcallAndroidCallback(String toast){
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}

Post a Comment for "How Do I Make Javascript-java Communication Sync In Android Webview?"