So I have a class named JavascriptBridge that I use to communicate between Java and Javascript.
To send commands to javascript, I simply use this:
public void sendDataToJs(String command) {
webView.loadUrl("javascript:(function() { " + command + "})()");
}
My problem is that I would also need a function that return a response from the Javascript. I try using webView.evaluateJavascript, but it skip the callback, as evaluateJavascript is done on 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); // Print "test"
// data = s; // The value that I would like to return
}
});
return data; // Return null, not "test"
}
The call to the method:
String result = getDataFromJs("test", webView); // Should return "test" from the webView
I've also tried using a @JavascriptInterface, but it gives the same result.