现在我们就可以在工程下建一个文件夹,专门管理我们自定义的插件,进行开发。 cordova下自定义插件 以定义一个Toast的插件为例 在src下创建ToastPlugin.java的文件执行android原生的调用 /** * 显示土司插件 * @author yuhailong * */ public class ToastPlugin extends CordovaPlugin { @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { //前端回调的方法名 if ("showToast".equals(action)) { return executeShowToast(args, callbackContext); } else { callbackContext.error("发生异常,请检查API使用是否正确"); return false; } } /** * 显示toast的原生方法 */ private void executeShowToast() { try { CordovaArgs cordovaArgs = new CordovaArgs(args); String text = cordovaArgs.getJSONObject(0).getString("text"); android.widget.Toast.makeText(cordova.getActivity(), text,Toast.LENGTH_LONG).show(); callbackContext.success(); return true; } catch (Exception e) { e.printStackTrace(); callbackContext.error("显示toast异常"); return false; } } } 在config.xml中配置插件路径 <feature name="ToastPlugin"> <param name="android-package" value="com.example.test.plugins.ToastPlugin" /> </feature> 在asserts/www/plugins/目录下插件自己的插件js文件 如:asserts/www/platform/android/plugins/cordova-plugin-toast/toast.js cordova.define( "com.example.test.plugins.ToastPlugin" , function(require, exports, module) { var exec = require('cordova/exec'); /** * Provides access to notifications on the device. */ module.exports = { /** * Causes the device to beep. * On Android, the default notification ringtone is played "count" times. * * @param {Integer} type The Toast type. */ showToast:function(successCallback,errorCallback,content){ exec(successCallback, errorCallback, "ToastPlugin", "showToast", [content]); }, }; }); 添加土司插件js配置信息,配置调用的方法名 在assets/www/platform/android/cordova_plugins.js文件中添加如下信息: ,{ "file" : "plugins/cordova-plugin-toast/toast.js", "id" : "com.example.test.plugins.ToastPlugin", "clobbers" : ["navigator.webtoast"]//定义调用的方法 } 前端插件中方法代码调用(本地的调试) 在index.html文件中 <html> <head> <!-- Customize this policy to fit your own app's needs. For more guidance, see: https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy Some notes: * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this: * Enable inline JS: add 'unsafe-inline' to default-src --> <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media- *"> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <meta name="format-detection" content="telephone=no"> <meta name="msapplication-tap-highlight" content="no"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> <link rel="stylesheet" type="text/css" href="css/index.css"> <title>Hello World</title> </head> <body> <div class="app"> <h1>Apache Cordova</h1> <div id="deviceready" class="blink"> <p class="event listening">Connecting to Device</p> <p class="event received">Device is Ready</p> <button id="myBtnOne" >Toast</button> </div> </div> <script type= "text/javascript" src="cordova.js"></script> <script type= "text/javascript" src="js/index.js"></script> <script type= "text/javascript" src="js/custom.js"></script> </body> </html> 类似在index.js文件夹下创建一个custom.js的文件,将js代码写在里面,Toast这里要注意的是弹框只能 通过id的查找了进行编辑; custom.js的文件如下: function showToast(){ navigator.webtoast.showToast(function(result){ alert("success:"+result); },function(err){ alert("error:"+err); },{ text:'你好,我是 Toast~', }); } document.getElementById("myBtnOne" ).addEventListener("click", showToast); 结束 整个cordova在android studio中的导入和自定义插件的开发流程,大致如上。具体的详情可以参照cordova的官方给的文档和网上的一下资料。 个人理解cordova相比于RN还是要容易上手些,cordova的核心还是一个H5,类似原生是个浏览器的壳;RN是通过js映射原生的组件。所以感觉交互上RN是要比cordova要好的,对于初学者要学的更多。当然两者都有其优势,要想深入其核心都是要下一点功夫的。 --------------------- 版权声明:本文为博主「那一年的梧桐雨」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/liugang921118/article/details/82345435
转载于:https://www.cnblogs.com/Jeely/p/11320155.html
相关资源:JAVA上百实例源码以及开源项目