After I had successfully added the calendar application to my Android emulator running Android 2.2 (Froyo), I soon realized that I wasn’t able to create events in it because I had no calendars. Android gave me this nice message: You have no calendars.
Searching the web for this problem lead me to the need of creating a Google account that then could be synced with the emulator’s calendar. However, the sync did not work. After setting up the account under Settings - Accounts and sync - Add account - Google it didn’t show up in the sync settings and there was still no calendar created for it.
The Android documentation said that it would be possible to create a calendar in an application, so I tried that. Quite a few hours of trying and debugging later, I finally found this post on StackOverflow, that contains a list of all the needed fields and correct values for them: After adding an event to a user-created calendar in android,the event title doesn’t show up.
So here’s the final piece of code that adds a local calendar to the Android emulator:
-
ContentValues vals = new ContentValues();
-
vals.put("_id", 1);
-
vals.put("_sync_account", account.name);
-
vals.put("_sync_account_type", account.type);
-
vals.put("name", account.name);
-
vals.put("displayName", account.name);
-
vals.put("color", 14417920);
-
vals.put("access_level", 700);
-
vals.put("selected", 1);
-
vals.put("ownerAccount", account.name);
-
vals.put("sync_events", 1);
-
vals.put("timezone", "GMT");
-
vals.put("hidden", 0);
-
getContentResolver().insert(calUri, vals);
Where calUri is something like
content://com.android.calendar/calendars?caller_is_syncadapter=true&account_name=local&account_type=LOCAL
and account is an android.accounts.Account.
I’ve put the code into a small application that reads the available accounts and creates a local calendar via a simple click on a button.
Download
Attention: Your emulator has to run Google APIs (Google Inc.) – API Level 8 and not Android 2.2 – API Level 8.