cordova.exec 怎么用
2个回答
展开全部
在实际的项目开发中,需要用到 cordova 的插件,进行各个不同平台的辅助开发,我们可以通过 plugins.cordova.io 对现有插件的搜索,以及安装。 插件的安装和使用,详细可以通过查阅 Cordova 的官网文档 The Command-Line Interface
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-03-27
展开全部
Cordova 核心java类说明:
1、CordovaActivity:Cordova Activity入口,已实现PluginManager、WebView的相关初始化工作, 只需继承CordovaActivity实现自己的业务需求。
2、PluginManager: 插件管理器
3、ExposedJsApi :javascript调用Native, 通过插件管理器PluginManager 根据service找到具体实现类。
4、NativeToJsMessageQueue:Native调用javascript,主要包括三种方式:loadUrl 、 轮询、反射WebView
Native调用javascript 方式:NativeToJsMessageQueue
1、loadUrl javascript 调用方式
private class LoadUrlBridgeMode extends BridgeMode
if (url.startsWith("file://") || url.startsWith("javascript:") || Config.isUrlWhiteListed(url)) {
}
2、Navitive事件通知javascript轮询获取Navitive数据
private class OnlineEventsBridgeMode extends BridgeMode
3、通过Java反射获取webview 的sendMessage 方法执行js, 支持 Android 3.2.4之上(包含)
---可以解决loadUrl 隐藏键盘的问题:当你的焦点在输入,如果这通过loadUrl调用js,会导致键盘隐藏
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注册javascript接口 _cordovaNative
boolean isHoneycomb = (SDK_INT >= Build.VERSION_CODES.HONEYCOMB && SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2);
// 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.
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");
1、CordovaActivity:Cordova Activity入口,已实现PluginManager、WebView的相关初始化工作, 只需继承CordovaActivity实现自己的业务需求。
2、PluginManager: 插件管理器
3、ExposedJsApi :javascript调用Native, 通过插件管理器PluginManager 根据service找到具体实现类。
4、NativeToJsMessageQueue:Native调用javascript,主要包括三种方式:loadUrl 、 轮询、反射WebView
Native调用javascript 方式:NativeToJsMessageQueue
1、loadUrl javascript 调用方式
private class LoadUrlBridgeMode extends BridgeMode
if (url.startsWith("file://") || url.startsWith("javascript:") || Config.isUrlWhiteListed(url)) {
}
2、Navitive事件通知javascript轮询获取Navitive数据
private class OnlineEventsBridgeMode extends BridgeMode
3、通过Java反射获取webview 的sendMessage 方法执行js, 支持 Android 3.2.4之上(包含)
---可以解决loadUrl 隐藏键盘的问题:当你的焦点在输入,如果这通过loadUrl调用js,会导致键盘隐藏
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注册javascript接口 _cordovaNative
boolean isHoneycomb = (SDK_INT >= Build.VERSION_CODES.HONEYCOMB && SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2);
// 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.
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");
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询