在此之前,你可能需要看另外一篇博客:https://blog.csdn.net/weixin_40420578/article/details/102610618
通过上面博客,得到了hello项目。准备现有的Android项目,我的是project_code,效果图如下(图一是平常练习的demo):
按如下步骤操作:
一:(前面得到的hello项目有用了)把E:\install\cordova\hello\platforms\android\app\src\main\assets下的www复制粘贴到现有项目:app-src-main-assets下,如下图:
二.把hello中的CordovaLib(在E:\install\cordova\hello\platforms\android下)以module方式引入到项目中
右键——>new——>Module——>Import Gradle Project——>Next——>CordovaLib——>Next(我导入过图中才提示)
三.gradle(app)引入CordovaLib
api project(':CordovaLib')
四.ToastPlugins.java
package com.example.dell.myapplication.plugins;
import android.widget.Toast;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
/**
* Created by Administrator on 2019/10/24
*
* @author mcl
*/
public class ToastPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext){
if ("showToast".equals(action)) {//前端回调的方法
return executeShowToast(args, callbackContext);
} else {
callbackContext.error("方法异常");
return false;
}
}
/**
* 显示toast的原生方法
*/
private boolean executeShowToast(JSONArray args, CallbackContext callbackContext) {
try {
CordovaArgs cordovaArgs = new CordovaArgs(args);
String text = cordovaArgs.getJSONObject(0).getString("text");//custom.js中的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.dell.myapplication.plugins.ToastPlugin"/>
</feature>
六.在assets——www——js下建立custom.js
function showToast(){
android.cordova.showToast(function(result){
alert("success:"+result);
},function(err){
alert("error:"+err);
},{
text:'Toast',
});
}
document.getElementById("btToast" ).addEventListener("click", showToast);
七.在assets——www——plugins下建立文件夹cordova-plugin-toast,并在其下建立toast.js
cordova.define( "com.example.dell.myapplication.plugins.ToastPlugin" , function(require, exports, module) {
var exec = require('cordova/exec');
module.exports = {
showToast:function(successCallback,errorCallback,content){
exec(successCallback, errorCallback, "ToastPlugin", "showToast", [content]);
},
};
});
八.在cordova_plugins.js中添加如下代码:
module.exports = [
……………………
,{
"file" : "plugins/cordova-plugin-toast/toast.js",
"id" : "com.example.dell.myapplication.plugins.ToastPlugin",
"clobbers" : ["android.cordova"]//定义调用的方法,跟custom.js对应上
}
];
九.index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<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="btToast" >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>
十.ToastPluginActivity(记得在AndroidManifest.xml注册)
public class ToastPluginActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
}
}