The Blog

Add Events on Google Calendar on Android Froyo and above

09 Ago 10

Since i started developing applications for android i noticed there were some undocumented apis. Google does reccomend to not use these apis but since there are no “other nice ways” to achieve some tasks sometimes they are useful ( but still unreccomended)

It’s the case of the Google Calendar Apis. Out there you can find a lot of docs about these undocumented & unsupported apis but you’ll get some troubles if google decides to change them.

For example if you want to add an “event” to the calendar programmatically you can follow the snippet below which is SDK proof. In fact i did ( It’s not refactored for better reading ) write some code that would work on Sdk from 1.5 to 2.2 ( aka Froyo ) solving the provider issue on froyo and above.


Hope it helps to solve the problem about google calendar in froyo πŸ™‚

Reference to the CP: Xda

Comments

  • I’ve been searching for this for quite awhile. Thanks very much for sharing! πŸ˜€

  • Unfortunately I commented before I tried adding events… it doesn’t work!! πŸ™

  • Droid-b

    Hello,
    I am new to android. I could not figure out how this code will be working! Could someone please share a working version of this code!

    Thanks for your light in the dark!

  • Droid-b

    Oh Sorry, I got it. Its a function that I should call. But, I managed to run the program. On my device, a window appeared with 2 option, name of my 2 calenders! I choose one of them, but nothing happened on my calendar on the online!

    Whats the problem at my side?

  • Hey, I am wondering what’s the SQLLite version of attendees for the meeting

  • Very good tutorial. I would suggest a tip to improve your script; using parameter β€œselected=1β€³ just after the projection String, will get a list of active calendars only.

  • lansher

    Hi, that’s very helpful ! Thanks a lot!
    But I tried it with nexus one 2.2, and the google calendar don’t respond. Do you have an idea about that?

  • Colin

    Looks surprisingly similar to the solution I came up with to support pre-Froyo devices, however instead of checking the android version each time to set the content location why not assign the content location to a string and append it? Also try using Build.version.SDK_INT instead of parsing the string each time.

    e.g.

    String contentLocation = “content://com.android.calendar”;
    if(Build.VERSION.SDK_INT < 8) {
    contentLocation = "content://calendar";
    }

    … then later on…

    Uri.parse(contentLocation + "/calendar");

  • Ranjit

    please change the line number 40 to this

    newEvent =
    cr.insert(Uri.parse(“content://calendar/events”), cv);

    so this will work for all device.

  • Saif

    hi,
    i am working on one app, which required to add a event in phone calender. i used your code but its crashing.

    06-23 17:10:57.590: ERROR/AndroidRuntime(14567): Uncaught handler: thread main exiting due to uncaught exception
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): java.lang.NullPointerException
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at com.saif.practise.CalenderApp.MyActivity.addToCalendar(MyActivity.java:166)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at com.saif.practise.CalenderApp.MyActivity.onClick(MyActivity.java:105)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.view.View.performClick(View.java:2367)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.view.View.onTouchEvent(View.java:4202)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.widget.TextView.onTouchEvent(TextView.java:6686)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.view.View.dispatchTouchEvent(View.java:3732)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1732)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1122)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1716)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.os.Handler.dispatchMessage(Handler.java:99)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.os.Looper.loop(Looper.java:123)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at android.app.ActivityThread.main(ActivityThread.java:4363)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at java.lang.reflect.Method.invokeNative(Native Method)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at java.lang.reflect.Method.invoke(Method.java:521)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
    06-23 17:10:57.610: ERROR/AndroidRuntime(14567): at dalvik.system.NativeStart.main(Native Method)

    it crashing on else part only. im executing this code on 2.1 OS.