Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open and display calendar event in android

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

like image 573
Alex Avatar asked Mar 20 '11 13:03

Alex


People also ask

How do I show calendar events on Android home screen?

Add the widget to your home screenSearch for the Google Calendar app and tap it. To customize your widget size, swipe left. Tap Add widget. In the top right, tap Done.

Why is my Android calendar not showing events?

First, try these common fixes If you're not connected, make sure that data or Wi-Fi is on, and that you're not in Airplane mode. Next, check your device's app store to make sure the Google Calendar app is up to date. To the left of the calendar's name, make sure the box is checked.

How do I show events on my Samsung calendar?

Navigate to and open the Calendar app, then tap Menu (the three horizontal lines), and then tap Manage calendars. Next, tap the switch(es) next to your desired categories (such as My phone, Samsung account, Google, or App events) to display those calendars.


2 Answers

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.

like image 89
Camille Sévigny Avatar answered Sep 29 '22 07:09

Camille Sévigny


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.

like image 36
CommonsWare Avatar answered Sep 29 '22 08:09

CommonsWare