android - 在android中打开和显示日历事件

标签 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 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 日历。

此时您可以选择要打开的应用程序,如果您选择“显示事件”,它确实会打开日历应用程序,但会出现空指针异常,我无法弄清楚我在这里做错了什么。我是第一个尝试这样做的人吗?

非常感谢任何帮助

最佳答案

我终于找到了解决方案:

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);

我希望你们中的一些人觉得这很有用。

我还在下面添加了一些其他日历 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);
}

如果有人有任何问题或有更好的方法来完成相同的任务,请告诉我。

关于android - 在android中打开和显示日历事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5368769/

相关文章:

events - angulardart 组件 - 调度自定义事件

iOS - 允许用户添加日历事件并填写一些字段

angularjs - 如何使用 "controller as"语法通过 AngularJS ui-calendar 获取日历对象?

Android:Fragment 的 onOptionsItemSelected 没有被调用

javascript - 控制事件处理程序/监听器的执行顺序

android - 将操作栏添加到 FragmentActivity

jquery - 未输入时的按键事件

java - 我如何将用户输入的出生日期转换为日历单元类型以添加​​到参数化构造函数?

android - 如何动态移动 ImageButton?

android - 如何从 TextInputLayout 中删除水平填充?