[Android] Google Calendar API change for ICS

[Android] Google Calendar API change for ICS

Recently I try to create an app which stored event in device calendar, I found this app will crash in Ice Cream Sandwich device. But the same codes in Ginger Bread device will work well. I checked the logcat, and found below logs:

java.lang.IllegalArgumentException: Event values must include an eventTimezone
at com.android.providers.calendar.CalendarProvider2.validateEventData(CalendarProvider2.java:2486)
at com.android.providers.calendar.CalendarProvider2.insertInTransaction(CalendarProvider2.java:2064)
at com.android.providers.calendar.SQLiteContentProvider.insert(SQLiteContentProvider.java:96)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:189)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:152)
at android.os.Binder.execTransact(Binder.java:338)
at dalvik.system.NativeStart.run(Native Method)


and my codes for saving events for device calendar:

ContentValues event = new ContentValues();
event.put("calendar_id", calId);
event.put("title", Title);
event.put("description", Description);
event.put("eventLocation", Location);
event.put("dtstart", startTime);
event.put("dtend", endTime);

Uri eventsUri = Uri.parse("content://com.android.calendar/events");
getContentResolver().insert(eventsUri, event);

well, the log points “java.lang.IllegalArgumentException: Event values must include an eventTimezone”, so I think maybe google calendar API change is cause this exception.

I checked Android Developers and found :

When inserting a new event the following fields must be included:
*dtstart
*dtend if the event is non-recurring
*duration if the event is recurring
*rrule or rdate if the event is recurring
*eventTimezone
*a calendar_id

There are further restrictions on all Updates and Inserts in the Events table:

If allDay is set to 1 eventTimezone must be TIMEZONE_UTC and the time must correspond to a midnight boundary. Exceptions are not allowed to recur. If rrule or rdate is not empty, original_id and original_sync_id must be empty. In general a calendar_id should not be modified after insertion. This is not explicitly forbidden but many sync adapters will not behave in an expected way if the calendar_id is modified.

So I try to revise code for my app:

ContentValues event = new ContentValues();
event.put("calendar_id", calId);
event.put("title", Title);
event.put("description", Description);
event.put("eventLocation", Location);
event.put("dtstart", startTime);
event.put("dtend", endTime);
<span style="color: red;">event.put("eventTimezone", Time.getCurrentTimezone());</span>

Uri eventsUri = Uri.parse("content://com.android.calendar/events");
getContentResolver().insert(eventsUri, event);

And I changed Android SDK from 2.2 to 4.0.3, then built project, BINGO!! I got it, there is no exception occur, and the event was stored in device google calendar successfully.

(Visited 40 time, 1 visit today)
Facebooktwittergoogle_plusredditpinterestlinkedinmail

One thought on “[Android] Google Calendar API change for ICS

Comments are closed.

Comments are closed.