So, I have a class called JavascriptBridge that I use to communicate between Java and Javascript.
To send commands to javascript, I just use this:
public void sendDataToJs(String command) { webView.loadUrl("javascript:(function() { " + command + "})()"); }
My problem is that I will also need a function that returns a response from Javascript. I am trying to use webView.evaluateJavascript , but it misses the callback since the Javascript evaluation is being done in another thread.
public String getDataFromJs(String command, WebView webView) { String data = null; webView.evaluateJavascript("(function() { return " + command + "; })();", new ValueCallback<String>() { @Override public void onReceiveValue(String s) { Log.d("LogName", s);
Method call:
String result = getDataFromJs("test", webView); // Should return "test" from the webView
I also tried using @JavascriptInterface , but it gives the same result.
source share