【问题标题】:Open and display calendar event in android在android中打开并显示日历事件
【发布时间】:2011-07-19 03:12:10
【问题描述】:

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


    【解决方案1】:

    我终于找到了解决办法:

    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_ACTIVITY_CLEAR_WHEN_TASK_RESET 已被弃用,我们可以使用 FLAG_ACTIVITY_NEW_DOCUMENT Ref:stackoverflow.com/a/33179077/2462531
    【解决方案2】:

    关于如何在android中创建一个新的日历事件有很多例子

    不应使用这些“示例”,因为 Android 中没有记录和支持的日历 API。

    但没有关于如何打开和显示事件

    您需要联系您尝试与之集成的任何第三方日历程序的作者,并询问他们如何进行集成。如果您尝试与作为 Android 开源项目一部分的日历应用程序集成,则该应用程序没有文档化和受支持的 API。

    【讨论】:

    • true,但是上面的示例确实适用于编辑事件,但是当我只想查看一个项目时,有些方法会给出一个空指针。这可以做到,只是不知道如何......还没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多