Skip to content

Sync-ing with Android’s Calendar

June 20, 2010

Hey everyone!

Glad to be back now that school’s out! Expect more posts over the summer.

One feature which I’ve implemented into several of my applications already is the ability to add events from within the application directly into the user’s calendar on his/her Android device. I’m not sure why there still exists close to no documentation on dealing with Android’s Calendars, but there do exist a few good articles out there (i.e. one from Shane Conder) that give good examples of how to deal with the calendar programmatically.

Here’s my spin on it and how I use it in my applications.

Before we start, make sure you include the following two permissions in your manifest:

<uses-permission android:name="android.permission.WRITE_CALENDAR"></uses-permission>
<uses-permission android:name="android.permission.READ_CALENDAR"></uses-permission>

Now, the first step is we need to check that the user has a calendar. Typically phones come pre-installed with a calendar application, but just in case the user has removed the calendar we don’t want to make any unnecessary assumptions. Here’s how:

// query for the user's available and selected calendars
// note the URI
Cursor cursor = getContentResolver().query(Uri.parse("content://calendar/calendars"),
                new String[] { "_id", "displayName" }, "selected=1", null, null);
if (cursor != null && cursor.moveToFirst()) {
     String[] calNames = new String[cursor.getCount()];
     int[] calIds = new int[cursor.getCount()];
     for (int i = 0; i < calNames.length; i++) {
          // retrieve the calendar names and ids
          // at this stage you can print out the display names to get an idea of what calendars the user has
          calIds[i] = cursor.getInt(0);
          calNames[i] = cursor.getString(1);
      if (calIds.length > 0) {
           // we're safe here to do any further work

At this point, all that’s left to do is to actually add it into the phone’s database. This can be done using a regular ContentValue object, and the only thing hard about this was figuring out what the table structure of the Android Calendar database looked like. Below are all the commonly used fields of the Calendar (other more abstract and less widely used fields exist as well – see above link):

// grab calendar id from above
int cal_id = calIds[0];

// set the content value
ContentValues cv = new ContentValues();

// make sure you add it to the right calendar
cv.put("calendar_id", cal_id);

// set the title of the event
cv.put("title", "jwei's birthday");

// set the description of the event
cv.put("description", "Time to celebrate jwei's birthday.");

// set the event's physical location
cv.put("eventLocation", "Palo Alto, CA");

// set the start and end time
// note: you're going to need to convert the desired date into milliseconds
cv.put("dtstart", System.currentTimeMillis());
cv.put("dtend", System.currentTimeMillis() + DateUtils.DAY_IN_MILLIS);

// let the calendar know whether this event goes on all day or not
// true = 1, false = 0
cv.put("allDay", 1);

// let the calendar know whether an alarm should go off for this event
cv.put("hasAlarm", 1);

// once desired fields are set, insert it into the table
getContentResolver().insert(Uri.parse("content://calendar/events"), cv);

One more thing to note is that not all of the fields must be included! Just set the ones that you need, and the others will get set to the default value automatically.

And that’s it! Hope this helped, and let me know if I missed anything!

Happy coding.

– jwei


Thanks to Mark Murphy for catching these warnings:

1) This example will only work on the native Calendar application or any application that uses the provided URIs.

2) This example has been tested on Android devices running 2.1 and under. I hear Android 2.2 will contain a new set of URIs.

10 Comments leave one →
  1. June 20, 2010 5:08 pm

    The Calendar content provider is not part of the Android SDK. Your code will break on any devices that have replaced the Calendar with another application not supporting this content provider. Your code will break on Android 2.2, since they changed the name of the content provider. Please see Google’s recommendations regarding undocumented content providers:

    • June 20, 2010 5:15 pm

      Hey Mark,

      Thanks for the comment – I’ll make sure I add an addendum that warns users this only works for the native Calendar app and that it’s only been tested and known to work on Android 2.1 and lower.

      – jwei

  2. June 23, 2010 10:22 am

    Hey Sidharth!

    Thanks for the catch – edit has been made.

    – jwei

  3. WeldFire permalink
    June 23, 2010 8:34 pm

    I was just wanting to make a note that whenever you are querying the start or endtimes for a event to call the cursor.getLong() function instead of getInt(). That solves much confusion.
    Thanks for the great post!

  4. October 12, 2010 2:55 am

    Great job. i bookmarked this website i will keep visiting this one. thank you..(:

  5. Mediha permalink
    January 19, 2011 5:10 pm

    this was helpfull, thanks:)

  6. Md Maidul Islam permalink
    March 14, 2011 11:17 pm

    How can I create multiple notification.I trigger multiple notification according to time.
    Catch all notification and the latest one.after click this just open previous one. Is there any method to handle this action.

    plz help
    thanks a lot

  7. Md Maidul Islam permalink
    March 14, 2011 11:17 pm

    How can I create multiple notification.I trigger multiple notification according to time.
    Catch all notification and the latest one.after click this just open previous one. Is there any method to handle this action.

    Please help me
    thanks a lot

  8. Lacra permalink
    January 1, 2012 1:02 pm

    Very good article but what if I want to add an recurring event? I have tried contentvalues.put(“rrule”, “FREQ=YEARLY”); but it doesn’t work properly…any ideas?

    Thank you for any help provided!


  1. Tweets that mention Sync-ing with Android’s Calendar « Think Android --

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: