在android中打开并显示日历事件

在android中打开并显示日历事件,android,events,calendar,Android,Events,Calendar,有很多关于如何在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

有很多关于如何在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日历

此时,您可以选择要打开的应用程序,如果您选择“Show Event”,它确实会打开日历应用程序,但会出现nullpointer异常,我无法找出我在这里做错了什么。我是第一个尝试这么做的人

非常感谢您的帮助

有许多关于如何在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月。你知道这可能是什么原因吗?对于编辑,你使用calendarEventID,但在创建事件时你没有说明如何获取该ID,因为当任务重置被否决时,我们可以使用新文档
FLAG\u ACTIVITY\u Ref: