phonegap是web app 下調用移動OS原生API的庫。在整個壓縮包結構中主要分三塊:前端
一、cordova.js,前端的js庫;功能是提供js的API接口,最終調用的是promp方法,以下:java
/** * Implements the API of ExposedJsApi.java, but uses prompt() to communicate. * This is used only on the 2.3 simulator, where addJavascriptInterface() is broken. */ module.exports = { exec: function(service, action, callbackId, argsJson) { return prompt(argsJson, 'gap:'+JSON.stringify([service, action, callbackId])); }, setNativeToJsBridgeMode: function(value) { prompt(value, 'gap_bridge_mode:'); }, retrieveJsMessages: function() { return prompt('', 'gap_poll:'); } };
二、cordova jar包android
以android下爲例,基本原理是經過重寫webview下的onJsConfirm接口,以下:web
public class CordovaChromeClient extends WebChromeClient
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) { boolean reqOk = false; if ((url.startsWith("file://")) || (Config.isUrlWhiteListed(url))) { reqOk = true; } if ((reqOk) && (defaultValue != null) && (defaultValue.length() > 3) && (defaultValue.substring(0, 4).equals("gap:"))) { try { JSONArray array = new JSONArray(defaultValue.substring(4)); String service = array.getString(0); String action = array.getString(1); String callbackId = array.getString(2); String r = this.appView.exposedJsApi.exec(service, action, callbackId, message); result.confirm(r == null ? "" : r); } catch (JSONException e) { e.printStackTrace(); return false; } }
經過exposedJsApi對象,以相似MML的設計模式,將前端不一樣的JS腳本轉換成action,經過對應plugin處理。三、config.xml 配置文件用以配置須要用的插件,好比攝像頭、文件、事件等等。