我正在使用 PhoneGap(又名 Cordova)构建 Android 应用程序,但在日历集成工作方面遇到问题。免责声明:我对 Android 和 PhoneGap 都是菜鸟,请耐心等待。
我想做的就是向用户的日历添加一个事件。下列的本教程 http://www.adobe.com/devnet/html5/articles/extending-phonegap-with-native-plugins-for-android.html,我创建了一个尝试启动日历意图的插件。代码如下所示:
public class CalendarPlugin extends Plugin {
public static final String NATIVE_ACTION_STRING="addToCalendar";
public static final String SUCCESS_PARAMETER="success";
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
if (NATIVE_ACTION_STRING.equals(action)) {
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 6, 19, 7, 30);
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 6, 19, 8, 30);
Intent calIntent = new Intent((Context) this.ctx, CalendarPlugin.class)
.setAction(Intent.ACTION_INSERT)
.putExtra(Events.TITLE, "A new event")
.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
this.ctx.startActivity(calIntent);
return new PluginResult(PluginResult.Status.OK, "Smashing success");
}
return new PluginResult(PluginResult.Status.ERROR, "Didn't work bro");
}
}
调用此插件的 Javascript 代码是标准的:
var CalendarPlugin = {
callNativeFunction: function (success, fail, resultType) {
if (cordova) {
return cordova.exec( success, fail,
"org.myorg.appname.CalendarPlugin",
"addToCalendar", [resultType]);
}
else {
alert("Calendar function is not available here.");
}
}
};
Android 代码正在被调用(使用断点确认)。但发送回 Javascript 代码的结果是一个错误:
无法找到显式活动类 {org.myorg.appname/org.myorg.appname.CalendarPlugin};您是否在 AndroidManifest.xml 中声明了此活动?
本教程中没有提到添加到 AndroidManifest.xml,这让我相信我遗漏了一些东西(另外,日历插件代码调用成功了,怎么会出现这样的错误日历插件找不到类?)。如果我确实需要添加日历插件到清单,我该怎么做?