How to add a local calendar to the Android 2.2 Froyo emulator

URL dieses Beitrags: http://blog.stefan-macke.com/2011/12/10/how-to-add-a-local-calendar-to-the-android-2-2-froyo-emulator/

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.

Android 2.2 Froyo: 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:

  1. ContentValues vals = new ContentValues();
  2. vals.put("_id", 1);
  3. vals.put("_sync_account", account.name);
  4. vals.put("_sync_account_type", account.type);
  5. vals.put("name", account.name);
  6. vals.put("displayName", account.name);
  7. vals.put("color", 14417920);
  8. vals.put("access_level", 700);
  9. vals.put("selected", 1);
  10. vals.put("ownerAccount", account.name);
  11. vals.put("sync_events", 1);
  12. vals.put("timezone", "GMT");
  13. vals.put("hidden", 0);
  14. 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.

Android Emulator running Google API 8

Kommentare zu diesem Beitrag

  1. Gravatar Stuart
    Am 15. Januar 2012 um 23:36 Uhr

    Thanks – works a treat! I have a wm8605 netbook that arrived with a broken Android installation.. no calendar sync; I cant install the apk because I need root, and I cannot root because the USB drivers dont work! All I want to do is get the Calendar app working, and your blog post got me there.

    Hurray! Thx.

  2. Gravatar Shilo Haendler
    Am 25. Februar 2012 um 18:46 Uhr

    Hello Stefan,

    I’m new to android development, and started my experience with developing an application to help manage the Android’s calendar app.

    Thank you for the sharing – this is what I’m looking for.
    My implementation is much similar to yours.

    However, I cannot delete a local calendar created by the application.
    using the same content Uri + the query parameters seems not to do the work.

    Did you get the chance to extend your researches regarding also deletions?
    If you have some insites, I’d like very much to be shared.

    Sincerely,
    Shilo

  3. Gravatar Stefan
    Am 27. Februar 2012 um 13:43 Uhr

    Hello Shilo. As far as I know, I was able to delete the local calendar. I would have to take a look at my sourcecode to find out how I did it. If I find the time to look it up, I’ll inform you.

  4. Gravatar Jesus
    Am 15. April 2012 um 19:16 Uhr

    Hello Stefan,
    I do not speak very good English but I try to explain my problem.
    I’m trying to create a calendar in my application following your code. And when I create a calendar I get the following error message:

    Error while creating calendar
    the name must no be empty: null

    What happens? I need to add anything?

    Thanks!

  5. Gravatar Stefan
    Am 19. April 2012 um 08:26 Uhr

    Hi Jesus. Did you run my APK or did you just use the code snippet above? If the latter is the case, please take a look at the complete source code in the APK.

Einen Kommentar schreiben

XHTML: Diese Tags sind erlaubt: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>