导航:首页 > 操作系统 > 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开发相关的资料

热点内容
手机音频转换app哪个好用 浏览:564
怎么快速展开全部切割清单中文件夹 浏览:670
内网如何打开服务器远程桌面连接 浏览:979
如何使用单片机的蜂鸣器 浏览:367
围棋初级教程pdf 浏览:857
不会夸人的程序员是什么样子 浏览:114
如何通过程序员调查背景 浏览:311
php判断字符串是否在数组中 浏览:888
安卓手机卡发热是什么原因 浏览:150
命令提示符管理员指令 浏览:883
压缩机的拆除 浏览:639
云服务器ip被攻击 浏览:612
天山暴击算法 浏览:833
编译实验输入某段程序C中 浏览:571
pdf阅读进度 浏览:752
phppecl下载 浏览:653
显控编程窗口转换怎么编辑 浏览:966
打印服务器怎么重置 浏览:532
为表格中的行创建文件夹 浏览:108
hadoop解压缩 浏览:425