
And now I would like to understand how this exec is delegated to Java Native API in Android platform.
In folder android I have found this cordova.js:
The exec call is implemented by module defined in cordova.js:
Here means the exec is implemented by androidExec:
1. meaning of parameters success, fail, service, action, args
2. There are two technical approaches for communication from JavaScript to Java in Android platform, see them in constant jsToNativeModes:
Example of JS_OBJECT, or called JavaScript interface:
Java code:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewGUI extends Activity {
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new JavaScriptInterface(), "jsinterface");
mWebView.loadUrl("file:///android_asset/www/index.html");
setContentView(mWebView);
}
final class JavaScriptInterface {
JavaScriptInterface () { }
public String getSomeString() {
return "string";
}
}
}
In JavaScript code, consume Java method getSomeString as below:
<script>
var String = window.jsinterface.getSomeString();
</script>
Back to implementation of AndroidExec:
var msgs = nativeApiProvider.get().exec(bridgeSecret, service, action, callbackId, argsJson);
it will again delegate the call to exec method of instance returned by nativeApiProvider.get().
Again look at implementation in nativeapiprovider.js, from the comment in line 21 we can know the currentApi returned by get comes from either ExposedJsApi.java or PromptBasedNativeAPI.
The ExposedJsApi.java could be found from this location:
It is a Java interface with method exec defined:
The call from JavaScript to Java is done via prompt call:
And the Java class SystemExposedJsApi implements the interface, delegates call to the instance of class CordovaBridge.
CordovaBridge will delegate to PluginManager:
Plugin manager first gets responsible plugin Java class by name, then perform the execute method of plugin.
For example this is OData offline plugin service class, in its execute method we can find there are lots of IF-ELSE branch to implement different offline operation.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
16 | |
14 | |
13 | |
11 | |
11 | |
11 | |
10 | |
8 | |
7 | |
7 |