Baccega Andrea Blog

The future is web3.0

Posted on by veke87


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.

/**
 * Adds the event to a calendar. It lets the user choose the calendar
 * @param ctx Context ( Please use the application context )
 * @param title title of the event
 * @param dtstart Start time: The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.
 * @param dtend End time: The value is the number of milliseconds since Jan. 1, 1970, midnight GMT.
 */
private static void addToCalendar(Context ctx, final String title, final long dtstart, final long dtend) {
    final ContentResolver cr = ctx.getContentResolver();
    Cursor cursor ;
    if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
        cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    else
        cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    if ( cursor.moveToFirst() ) {
        final String[] calNames = new String[cursor.getCount()];
        final int[] calIds = new int[cursor.getCount()];
        for (int i = 0; i < calNames.length; i++) {
            calIds[i] = cursor.getInt(0);
            calNames[i] = cursor.getString(1);
            cursor.moveToNext();
        }
 
        AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
        builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() {
 
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ContentValues cv = new ContentValues();
                cv.put("calendar_id", calIds[which]);
                cv.put("title", title);
                cv.put("dtstart", dtstart );
                cv.put("hasAlarm", 1);
                cv.put("dtend", dtend);
 
                Uri newEvent ;
                if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                    newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
                else
                    newEvent = cr.insert(Uri.parse("content://calendar/events"), cv);
 
                if (newEvent != null) {
                    long id = Long.parseLong( newEvent.getLastPathSegment() );
                    ContentValues values = new ContentValues();
                    values.put( "event_id", id );
                    values.put( "method", 1 );
                    values.put( "minutes", 15 ); // 15 minutes
                    if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                        cr.insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
                    else
                        cr.insert( Uri.parse( "content://calendar/reminders" ), values );
 
                }
                dialog.cancel();
            }
 
        });
 
        builder.create().show();
    }
    cursor.close();
}

Hope it helps to solve the problem about google calendar in froyo :)

Reference to the CP: Xda

Posted on by veke87 | Posted in Android, Development | Tagged , ,


  • Maurilios

    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.

  • Guillaume

    Good tutorial indeed, used the calendar before Froyo and it worked but not anymore, didn’t know why so thanks (didn’t try it yet though)!

    I think there is an error on line 40 though, as it should be:

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

  • Guillaume

    Sorry, as it is:

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

    but should be:

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

  • http://www.andreabaccega.com veke87

    I’m not sure it would work…. Have you tried it?

  • Guillaume

    Not yet, but it seems logical when reading the code (Froyo seems to remove the com.android. from the content address), otherwise why put an if – else with both the same instruction?

  • http://bside.extremeambient.net T

    @Guillaume : You are right. It is newEvent = cr.insert(Uri.parse(“content://calendar/events”), cv);, tested on 1.6 and it works.

    Tahnk you Andrea for such a great entry. It helps me a lot.

  • http://www.androidiani.com Andrea Baccega

    Lol @Guillame you’re right :) my fault i’ll clean the code asap.

    @T, you’re welcome :)

  • http://youshouldseo.com Dallas SEM

    Glad I found this tidbit on bing when I was perusing the web. Good STUFF!

  • http://blog.c-real.net c-real

    Thank you
    This is a very helpfull tutorial.

  • MissLulu

    Hy, Good tutorial and its very usefull !
    I have a question about the parameters of the function. Could you please explain me, what it is context and the the number of milliseconds since Jan. 1, 1970, midnight GMT.
    Could you send me an example?

    thanks a lot