導航:首頁 > 操作系統 > androidcordova開發

androidcordova開發

發布時間:2023-06-13 23:40:07

android 混合開發 用什麼框架好

Cordova是一個廣泛使用的Hybrid開發框架,它提供了一套js和Native交互規范

在Cordova的 SystemWebViewEngine 類中可以看到

private static void exposeJsInterface(WebView webView, CordovaBridge bridge) {
if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)) {
Log.i(TAG, "Disabled addjavascriptInterface() bridge since Android version is old.");
// Bug being that Java Strings do 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.
return;
}
webView.addJavascriptInterface(new SystemExposedJsApi(bridge), "_cordovaNative");
}

因此當Android系統高於4.2時,Cordova還是使用 addJavascriptInterface 這種方式,因為這個方法在高版本上安全而且簡單,低於4.2的時候,用什麼方法呢?

答案是 WebChromeClient.onJsPrompt 方法

WebView可以設置一個 WebChromeClient 對象,它可以處理js的3個方法

onJsAlert
onJsConfirm
onJsPrompt
這3個方法分別對應js的 alert 、 confirm 、 prompt 方法,因為只有 prompt 接收返回值,所以js調用一個Native方法後可以等待Native返回一個參數。下面是 cordova.js 中的一段代碼:

/**
* Implements the API of ExposedJsApi.java, but uses prompt() to communicate.
* This is used pre-JellyBean, where addJavascriptInterface() is disabled.
*/
mole.exports = {
exec: function(bridgeSecret, service, action, callbackId, argsJson) {
return prompt(argsJson, 'gap:'+JSON.stringify([bridgeSecret, service, action, callbackId]));
},
setNativeToJsBridgeMode: function(bridgeSecret, value) {
prompt(value, 'gap_bridge_mode:' + bridgeSecret);
},
retrieveJsMessages: function(bridgeSecret, fromOnlineEvent) {
return prompt(+fromOnlineEvent, 'gap_poll:' + bridgeSecret);
}
};

然後只要在 onJsPrompt 方法中使用 CordovaBridge 來處理js的prompt調用

/**
* Tell the client to display a prompt dialog to the user. If the client returns true, WebView will assume that the client will handle the prompt dialog and call the appropriate JsPromptResult method.
* <p/>
* Since we are hacking prompts for our own purposes, we should not be using them for this purpose, perhaps we should hack console.log to do this instead!
*/
@Override
public boolean onJsPrompt(WebView view, String origin, String message, String defaultValue, final JsPromptResult result) {
// Unlike the @JavascriptInterface bridge, this method is always called on the UI thread.
String handledRet = parentEngine.bridge.promptOnJsPrompt(origin, message, defaultValue);
if (handledRet != null) {
result.confirm(handledRet);
} else {
dialogsHelper.showPrompt(message, defaultValue, new CordovaDialogsHelper.Result() {
@Override
public void gotResult(boolean success, String value) {
if (success) {
result.confirm(value);
} else {
result.cancel();
}
}
});
}
return true;
}

Ⅱ 已有的android項目如何集成cordova

方法/步驟

創建一個Cordova的工程,創建方式參考:/shuma/1744256uiu.html

啟動eclipse,右擊[package explore]空白處,選擇新建

打開新建窗口後,選擇用已存在代碼創建android項目

選中我們要導入的工程目錄,在導入工程窗口我們會發現有兩個工程,一個是我們創建的工程,另一個是新版Cordova的類庫

導入完成,會發現,hello工程出現在[package explore]工程列表中,可以點擊查看

閱讀全文

與androidcordova開發相關的資料

熱點內容
什麼是金稅盤伺服器 瀏覽:434
phpsocket開發 瀏覽:507
caj文檔轉pdf 瀏覽:550
eclipsemaven命令配置 瀏覽:335
雅安視監控app叫什麼名字 瀏覽:729
東方財富app籌碼藍色和紅色是什麼 瀏覽:564
pr安裝文件夾圖標變白 瀏覽:177
如何用伺服器建設網站 瀏覽:347
定時鬧鍾單片機課程設計 瀏覽:537
腰椎壓縮性骨折症狀 瀏覽:466
絕地求生全軍出擊的伺服器什麼時候開 瀏覽:492
怎麼入侵游戲伺服器篡改數據 瀏覽:736
線性壓縮機原理 瀏覽:542
javanewinstance 瀏覽:817
程序員第一玄學 瀏覽:499
精品源碼怎麼算 瀏覽:493
加密技術在現實中應用體會 瀏覽:182
單片機如何換晶振 瀏覽:208
合並兩個數組java 瀏覽:15
命令標注圓半徑怎麼用 瀏覽:662