Cordova Android Framework

Source: Internet
Author: User

First, Cordova core Java class description

cordovaactivity: Cordova activity Portal, has implemented PlugInManager, WebView related initialization work, just inherit cordovaactivity to achieve their business needs.

PlugInManager: Plugin Manager

Exposedjsapi : JavaScript calls native through the plug-in manager PlugInManager to find the specific implementation class based on the service.

nativetojsmessagequeue: Native calls JavaScript, mainly consists of three ways: Loadurl, polling, reflection Webviewcore execution js

Second, Cordova frame class diagram

Iii. Cordova Framework Launch

The Loadurl framework is initiated when the Cordova method of Droidgap is called in the OnCreate method that implements the activity of the Droidgap or Cordovainterface interface.

Cordova provides a class (droidgap extends cordovaactivity) and a interface (cordovainterface ) to enable Android developers to develop Cordova.

In general, the implementation of DROIDGAP can be, because the Droidgap class has done a lot of preparation, it can be said that Droidgap class is an important part of the Cordova framework, if necessary to implement the Cordovainterface interface, So many droidgap functions in this class need to be implemented on their own. The activity that inherits Droidgap or Cordovainterface is an independent Cordova module, The independent Cordova module refers to each activity that implements the Droidgap or Cordovainterface interface corresponds to a set of independent Webview,plugin,pluginmanager, which is not shared.

Call Cordovawebview.loadurl () after initialization of Cordovawebview. This completes the start of the Cordova.


1.initialization of Cordova associated objects

When instantiating Cordovawebview, the Cordovawebview object will create a plug-in Manager PlugInManager object that belongs to the current Cordovawebview object. A message Queue Nativetojsmessagequeue object, a Javascriptinterface object Exposedjsapi, and a Exposedjsapi object added to the Cordovawebview, Javascriptinterface name is: _cordovanative.


2. Cordova's Javascriptinterface
Cordovawebview PlugInManager objects and Nativetojsmessagequeue objects are required when creating EXPOSEDJSAPI. Because all JS ends interact with Android native code through the Exec method of the Exposedjsapi object. In the Exec method, execute the PlugInManager exec method, PlugInManager to find the specific plugin and instantiate and then execute the plugin execute method, and according to the synchronous identity is the synchronous return to the JS message or asynchronous. The message returned to JS by Nativetojsmessagequeue Unified Management.


3. When to load plugin, how to load
Cordova loads all the plugin in the configuration file into PlugInManager when each activity is started. So when did you load these plugin into PlugInManager? In b the Last Call Cordovawebview.loadurl (), yes, at this time will initialize PlugInManager and load plugin. PlugInManager not immediately instantiate the plugin object when loading the plugin, but simply saves the plugin class name to a hashmap, using the service name as the key value.
When the JS side requests Android through the Exposedjsapi object of the Javascriptinterface interface, PlugInManager will find plugin from HashMap, if the plugin has not yet been instantiated, Instantiate the plugin with the Java reflection mechanism and execute the plugin Execute method.


4. Cordova of data returned

In Cordova, the Android plugin is requested via the Exec () function, and the return of the data can be synchronous or asynchronous to the Exec () function request. When developing an Android plugin, you can override the public boolean issynch (String action) method to determine whether it is synchronous or asynchronous. Cordova uses a queue (Nativetojsmessagequeue) on the Android side to specifically manage the data returned to JS.


1) Sync
Cordova after executing exec (), Android will return data immediately, but not necessarily the data for that request, possibly the data from a previous request, because when the exec () request plugin is allowed to return data synchronously, Cordova is also the pop header data from the Nativetojsmessagequeue queue header and is returned. Then, according to Callbackid, reverse lookup a JS request, and return the data to the request's success function.

2) asynchronous
Cordova does not synchronize to get a return data after executing exec (). Cordova executes exec () and starts a XMLHttpRequest object or prompt () function to get the data in the Nativetojsmessagequeue queue without stopping. And according to Callbackid reverse lookup to the corresponding JS request, and the data to the success function.
Note: Cordova uses XMLHttpRequest to get the return data for local HTML files (URLs that start with file://) or for mobile phones, while others use the prompt () function to retrieve the returned data.

5, Webview.sendjavascript sent to the JS queue, onnativetojsmessageavailable responsible for the execution of JS.

Native invoke JS execution method has three kinds of implementation loadurlbridgemode, Onlineeventsbridgemode, Privateapibridgemode

1, Webview.sendjavascript Send JS method to JS queue

2, Onjsprompt method interception, get call mode

"If it is Gap_bridge_mode, execute AppView.exposedJsApi.setNativeToJsBridgeMode (integer.parseint (message));
"If it is Gap_poll, execute appView.exposedJsApi.retrieveJsMessages (" 1 ". Equals (message));

3. Call Setbridgemode method call Onnativetojsmessageavailable execute JavaScript call

Four, native call JavaScript way: Nativetojsmessagequeue


1. Loadurl JavaScript Call mode

Private class Loadurlbridgemode extends Bridgemode

if (Url.startswith ("file://") | | | Url.startswith ("javascript:") | | config.isurlwhitelisted (URL)) {

}


2. Navitive event notifies JavaScript to poll for navitive data

Private class Onlineeventsbridgemode extends Bridgemode

3, through the Java reflection Get WebView SendMessage method to execute JS, support Android 3.2.4 (included)

---can solve the problem of Loadurl hidden keyboard: When your focus is in the input, if this is called by Loadurl JS, it will cause the keyboard to hide

Private class Privateapibridgemode extends Bridgemode

Field f = Webviewclass.getdeclaredfield ("Mprovider");
F.setaccessible (TRUE);
Webviewobject = F.get (WebView);
Webviewclass = Webviewobject.getclass ();

Field f = Webviewclass.getdeclaredfield ("Mwebviewcore");
F.setaccessible (TRUE);
Webviewcore = F.get (Webviewobject);

if (Webviewcore! = null) {
Sendmessagemethod = Webviewcore.getclass (). Getdeclaredmethod ("SendMessage", Message.class);
Sendmessagemethod.setaccessible (TRUE);
}

Message execjsmessage = Message.obtain (null, EXECUTE_JS, URL);
Sendmessagemethod.invoke (Webviewcore, execjsmessage);


4. Native register JavaScript interface _cordovanative


Boolean Ishoneycomb = (Sdk_int >= build.version_codes. Honeycomb && sdk_int <= build.version_codes. HONEYCOMB_MR2);
Bug being that Java Strings does not get converted to JS Strings automatically. This isn't hard-to-work-around on the JS side, but it's easier to just use the prompt bridge instead.
if (Ishoneycomb | | (Sdk_int < Build.version_codes. Gingerbread)) {
LOG.I (TAG, "Disabled Addjavascriptinterface () bridge since Android version is old.");
Return
} else if (Sdk_int < build.version_codes. Honeycomb && Build.MANUFACTURER.equals ("Unknown")) {
Addjavascriptinterface crashes on the 2.3 emulator.
LOG.I (TAG, "Disabled Addjavascriptinterface () bridge callback due to a bug on the 2.3 emulator");
Return
}
This.addjavascriptinterface (Exposedjsapi, "_cordovanative");

Cordova Android Framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.