Last post I wrote about Interfacing Javascript from Webview to native Android was about one way communication from Javascript to Android activity.
Later I tried a duplex communication between Activity and Javascript. That means -
- Call Javascript methods from Android activity
- Call Android activity methods from Javascript
are possible.
Android to Javascript
In MainActivity.java
String data = "Hello!";
webview.loadUrl("javascript:setData(\"" + data + "\");");
In index.html
function setData(data) {
alert(data);
}
Javascript to Android
In MainActivity.java
public class JSInterface {
JSInterface() {}
@JavascriptInterface
public String getData() {
return "Hello";
}
}
webview.addJavascriptInterface(new JSInterface(), "JSInterface");
In index.html
var data = JSInterface.getData();
alert(data);
Here is a preview of my setup where a javacript function is called when a button is clicked, and the javascrip call activity's function for current date string.
Check my repository Android-Javascript-Bridge for full project source.