在android中打开并显示日历事件
有很多关于如何在android中创建新日历事件的例子,但没有关于如何打开和显示事件的例子。这是到目前为止我的代码在android中打开并显示日历事件
public static void startCalendarMimeType(Context context, CalendarItem item){
//all version of android
Intent i = new Intent();
// mimeType will popup the chooser any for any implementing application (e.g. the built in calendar or applications such as "Business calendar"
i.setType("vnd.android.cursor.item/event");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// the time the event should start in millis. This example uses now as the start time and ends in 1 hour
//i.putExtra("beginTime", item.getBegin());
//i.putExtra("endTime", item.getEnd());
i.putExtra("_id", item.getId());
// the action
//i.setAction(Intent.ACTION_PICK);
context.startActivity(i);
}
日历项目包含使用内容解析器日历已经检索到的信息。当用户点击我的物品时,我希望它打开显示物品的Android日历。
在这一点上,你可以选择一个应用程序打开,如果你选择“显示事件”,它会打开日历应用程序,但得到一个空指针异常,我只是无法弄清楚我在这里做错了什么。我是第一个尝试这样做的人?
任何帮助非常感谢
有关于如何创造这些“榜样”的Android
无新的日历事件的例子很多应该使用,因为没有记录并支持Android中的日历API。
,但没有就如何打开并显示事件
您需要联系的任何第三方日历程序的您要集成并要求他们怎么做整合作者。如果您尝试与作为Android开放源代码项目一部分的日历应用程序集成,则该应用程序没有文档化和受支持的API。
我终于找到了解决办法:
Intent intent = new Intent(Intent.ACTION_VIEW);
//Android 2.2+
intent.setData(Uri.parse("content://com.android.calendar/events/" + String.valueOf(calendarEventID)));
//Android 2.1 and below.
//intent.setData(Uri.parse("content://calendar/events/" + String.valueOf(calendarEventID)));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(intent);
我希望,你们当中有些人觉得这有用。
我还增加了以下几个其他日历意图:
/**
* Add a calendar event.
*/
private void addCalendarEvent(){
Context context = getContext();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(intent);
}
/**
* Edit a calendar event.
*/
private void editCalendarEvent(){
Context context = getContext();
long calendarEventID = .....
intent.setData(Uri.parse("content://com.android.calendar/events/" + String.valueOf(calendarEventID)));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
context.startActivity(intent);
}
让我知道如果任何人有任何问题或有更好的方法来完成相同的任务。
伟大的答案男人。但是,当我运行代码查看事件时,我的日期似乎总是在1969年12月回来。您是否有任何想法可能会导致这种情况? – 2012-11-22 22:16:42
用于编辑您使用calendarEventID,但您不会在创建事件时如何获取该ID – 2016-04-28 10:49:44
由于'FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET'已弃用,我们可以使用'FLAG_ACTIVITY_NEW_DOCUMENT'参考:https://stackoverflow.com/a/33179077/2462531 – 2017-07-04 07:28:18
是真的,但上面的例子对编辑事件有效,但是当我只想查看一个项目时,有些如何给出空指针。这可以做到,只是不知道如何...。 – Alex 2011-03-22 06:30:06