SET 一 导入项目:

将andorid项目导入到eclipse,在src目录下添加class等,如图:

abbfg001

添加后:

khjashdas002

SET 二 创建Echo插件:

[section style=”]package com.bbcvision.multiscreen.plugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

public class Echo extends CordovaPlugin {

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals(“echo”)) {
String message = args.getString(0);
this.echo(message, callbackContext);
return true;
}
return false;
}

private void echo(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
callbackContext.success(“33333333333333”);
} else {
callbackContext.error(“Expected one non-empty string argument.”);
}
}
}

[/section]

SET 三  配置Plugin

这里需要注意,请直接在phonegap目录下的config.xml文件添加:

[section style=”]<feature name=”Echo”>
<param name=”android-package” value=”org.apache.cordova.plugin.Echo”/>
</feature> [/section]

如果直接在android目录下配置,当使用Cordova run andorid 命令时,andorid目录下会被覆盖。

SET 四  编写echo.js方法:

同样也需要在phongap的www目录下创建,不然build或run的时候也被覆盖。

[section style=”]

function echoPlugin(title, location, notes, startDate, endDate, successCallback, errorCallback){
cordova.exec(
successCallback, // success callback function
errorCallback, // error callback function
‘Echo’, // mapped to our native Java class called “CalendarPlugin”
‘echo’, // with this action name
[{                  // and this array of custom arguments to create our entry
“title”: title,
“description”: notes,
“eventLocation”: location,
“startTimeMillis”: startDate.getTime(),
“endTimeMillis”: endDate.getTime()
}]
);
}

[/section]

创建一个调用方法:

[section style=”]

function addToCal(){
var startDate = new Date(“July 19, 2013 8:00:00”);
var endDate = new Date(“July 19, 2013 18:00:00”);
var notes = “Arrive on time, don’t want to miss out (from Android)”;
var title = “PhoneGap Day”;
var location = “Portland, OR”;
var notes = “Arrive on time, don’t want to miss out!”;
var success = function(message) { alert(message); };
var error = function(message) { alert(message); };
echoPlugin(title, location, notes, startDate, endDate, success, error);
}

[/section]

SET 五  编写HTML文件:

[section style=”]

<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″ />
<meta name=”format-detection” content=”telephone=no” />
<meta name=”viewport” content=”width=device-width, initial-scale=1, user-scalable=no”>
<link rel=”stylesheet” type=”text/css” href=”css/basics.css” />
<script type=”text/javascript” src=”cordova.js”></script>
<script type=”text/javascript” src=”js/echo.js”></script>
<script type=”text/javascript” src=”js/zepto.js”></script>
<script type=”text/javascript”>
document.addEventListener(“deviceready”, onDeviceReady, true);
function onDeviceReady() {
$(function(){
$(“#testID”).on(‘click’,addToCal);
});
};
</script>
<title>UI</title>
</head>

<body>
<input type=”button” id=”testID” value=”调用 JAVA Echo方法” />
</body>
</html>

[/section]

注意:需要引入Cordova.js文件;需要写onDeviceReady方法确认Cordova初始化完毕。

在手机上测试成功!不发截图了。