6

There are many examples on how to create a new calendar event in android but none on how to open and display an event. This is my code so far

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

The Calendar item contains information already retrieved from the calendar using the content resolver. When a user clicks on my item I want it to open the Android calendar displaying the item.

At this point you can select an app to open with, If you choose "Show Event" it does open the calendar app but gets a nullpointer exception and I just can't figure out what I'm doing wrong here. I'm I the first who try's to do this?

Any help very much appreciated

Alex
  • 624
  • 1
  • 9
  • 12

2 Answers2

11

I finally found the solution:

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

I hope that some of you find this usefull.

I have also added a few other calendar Intents below:

/**
 * 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);
}

Let me know if anyone has any questions or has a better way to accomplish the same task.

Camille Sévigny
  • 5,104
  • 4
  • 38
  • 61
  • Great answer man. However, when I run the code to view an event, my dates always seem to come back as December 1969. Do you have any idea what might be causing this? – John Roberts Nov 22 '12 at 22:16
  • for edit you use calendarEventID, but you don't put how to get that ID when creating the event – Jorge Arimany Apr 28 '16 at 10:49
  • As `FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET` is deprecated we can use `FLAG_ACTIVITY_NEW_DOCUMENT ` Ref:https://stackoverflow.com/a/33179077/2462531 – Shailendra Madda Jul 04 '17 at 07:28
2

There are many examples on how to create a new calendar event in android

None of those "examples" should be used, as there is no documented and supported API for a calendar in Android.

but none on how to open and display an event

You would need to contact the authors of whatever third-party calendar program you are trying to integrate with and ask them how to do that integration. If you are trying to integrate with the Calendar application that is part of the Android open source project, there is no documented and supported API for that application.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • true, but the above example does work for the edit event, but some how gives a nullpointer when I just want to view an item. This can be done, just don't know how... yet. – Alex Mar 22 '11 at 06:30