1、下载phonegap(cordova),http://pan.baidu.com/s/1c0379Iw。
2、创建工程,进入解压文件夹~\cordova-android-master\bin,执行\cordova-android-master\bin>create<工程存储路径><包名><工程名>,如:create Test com.test AndrdTest
3、将创建的工程导入eclipse中。
4、创建js调用native插件,新建类继承CordovaPlugin,重写execute方法
public class ToastPlugin extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {
Toast.makeText(MyApplication.getContext(), "success", 100).show();
Log.e("********传入的参数*******", args.getString(0));
if(action.equals("false")){
callbackContext.success("success");
return true;
}else{
callbackContext.error("faile");
return false;
}
}
}
5、在res/xml/config.xml文件中注册插件
<feature name="ToastPlugin">
<param name="android-package" value="com.iraid.phonegap.ToastPlugin"/>
</feature>
6、新建js文件,如:ToastPlugin.js
var toastPlugin =
{ createEvent: function(name, notes, successCallback, errorCallback)
{
cordova.exec(
successCallback, // success callback function
errorCallback, // error callback function
'ToastPlugin', // 与config.xml中的name相同
'true', // execute方法中的action参数
[{ // and this array of custom arguments to create our entry
"name": name,
"description": notes
}]
);
}
};
7、在html中引用创建的js文件,并调用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<title>Hello World</title>
</head>
<body>
<input type="button" value="Toast" onClick="send()"/>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/ToastPlugin.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
app.initialize();
function send(){
var notes = "zhou";
var title = "PhoneGap Day";
var success = function(message) { alert( message); };
var error = function(message) { alert(message); };
toastPlugin.createEvent(title,notes, success, error);
}
</script>
</body>
</html>