You have two ways to comunicate to your javascript code. The first one is injecting code to webview via .loadUrl(...) method. The second one is via a callback in response to a javascript->native-plugin(java) call.
You can see callback response in execYourJavaMethod() and injecting in sendcommand()
private class sendJS extends CordovaActivity {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
// Implement here your calls from javascript
boolean result = false;
if ("yourJavaMethod".equals(action)) {
JSONObject options = args.optJSONObject(0);
result = execYourJavaMethod(options, callbackContext);
}
return result;
}
public boolean execYourJavaMethod(JSONObject options, CallbackContext callbackContext) {
// This will inject an event to your javascript code
this.sendcommand();
boolean iWantToCallSuccessCallbackWithData = false;
if (iWantToCallSuccessCallbackWithData) {
// This will call your success callback with some data
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "Your data back to javascript"));
} else {
// This will call the success callback with no data
callbackContext.success();
}
return true;
}
public void sendcommand() {
String event = String.format("javascript:cordova.fireDocumentEvent('yourEventHere', { 'param1': '%s' });", "some string for param1");
this.webView.loadUrl(event);
}
}
From javascript side you should register the listener for your event:
document.addEventListener('yourEventHere', function(e) {
alert(JSON.stringify(e));
});
To comunicate to your Java plugin:
myPlugin.doSomethingInJava = function (successCallback, failureCallback) {
cordova.exec(successCallback, failureCallback, 'sendJS', 'yourJavaMethod', []);
};