我正在使用 PhoneGap(又名 Cordova)构建一个 Android 应用程序,但在使日历集成正常工作时遇到了问题。 免责声明:我是 Android 和 PhoneGap 的菜鸟,请多多包涵。
我想要做的就是向用户的日历中添加一个事件。正在关注this tutorial ,我创建了一个试图启动日历 Intent 的插件。代码如下所示:
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 代码的结果是错误的:
Unable to find explicit activity class {org.myorg.appname/org.myorg.appname.CalendarPlugin}; have you declared this activity in your AndroidManifest.xml?
教程中没有提到添加到 AndroidManifest.xml,这让我相信我遗漏了一些东西(另外,CalendarPlugin 代码被成功调用,所以怎么会有错误提示找不到 CalendarPlugin 类?)。如果我确实需要将 CalendarPlugin 添加到 list 中,我该怎么做呢?
最佳答案
引用的教程未涵盖 Intent 。您将数据发送到的 Intent 是您自己的 CalendarPlugIn 类,这不是您想要的,因为它不处理 Intent 。
参见 http://developer.android.com/guide/topics/intents/intents-filters.html关于 Intent 。
此外,如果您搜索 SO,您会发现在 ICS 之前,甚至没有一种方法可以在不使用网络服务的情况下正式将内容添加到 Google 日历。有一些非正式的方法可以做到这一点,但要受制于 Google 或 ODM 本身的突发奇想。
更新:
您应该能够在 Phonegap 中使用 Intent (通过插件)。我只是添加了评论以注意如果您打算在您的应用程序中集成日历,如果您想要支持大多数 Android 设备,您可能需要做一些研究。如果您有兴趣在 ICS 中添加日历事件,请查看:http://developer.android.com/reference/android/provider/CalendarContract.html
Op 的编辑
我只需要修复 Intent 构造函数,它按预期工作:
Uri uri = Uri.parse("content://com.android.calendar/events");
Intent calIntent = new Intent("android.intent.action.INSERT", uri)
关于android - PhoneGap/Cordova 日历集成 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10873997/