Last Updated: February 25, 2016
·
2.789K
· inuyasha82

Add calendar event in android before ICS

In android before Ice Cream Sandwich there isn't an official api to handle calendar event programmatically. But handling them is not very difficult, here a little tip (you can find that also in stackoverflow http://stackoverflow.com/questions/9482514/android-calendar-get-event-id/9749833#9749833 i'm the author of both question and answer).

It is good to know that the calendar is based on a sqlite.
First I extracted a list of columns used to store events into android calendar. Here the list:

[0] "originalEvent" (id=830007842672)

[1] "availabilityStatus" (id=830007842752)

[2] "ownerAccount" (id=830007842840)

[3] "syncaccount_type" (id=830007842920)

[4] "visibility" (id=830007843008)

[5] "rrule" (id=830007843080)

[6] "lastDate" (id=830007843144)

[7] "hasAlarm" (id=830007843216)

[8] "guestsCanModify" (id=830007843288) [9] "guestsCanSeeGuests" (id=830007843376)

[10] "exrule" (id=830007843464)

[11] "rdate" (id=830007843528)

[12] "transparency" (id=830007843592)

[13] "timezone" (id=830007843672)

[14] "selected" (id=830007843744)

[15] "dtstart" (id=830007843816) [16] "title" (id=830007843888)

[17] "synctime" (id=830007843952)

[18] "_id" (id=830007844024) [19] "hasAttendeeData" (id=830007844088)

[20] "syncid" (id=830007844176)

[21] "commentsUri" (id=830007844248)

[22] "description" (id=830007844328)

[23] "htmlUri" (id=830007844408)

[24] "syncaccount" (id=830007844480)

[25] "syncversion" (id=830007844560)

[26] "hasExtendedProperties" (id=830007844640)

[27] "calendar_id" (id=830007844736)

Then if i want to get the new event id for my event:

public static long getNewEventId(ContentResolver cr, Uri cal_uri){      
    Uri local_uri = cal_uri;
    if(cal_uri == null){
        local_uri = Uri.parse(calendar_uri+"events");
    } 
    Cursor cursor = cr.query(local_uri, new String [] {"MAX(_id) as max_id"}, null, null, "_id");
    cursor.moveToFirst();
    long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));     
    return max_val+1;
}

And for insert event:

public void insertDomainEntry(Date exp_date, String name, long event_id){
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("exp_date", exp_date.getTime()/1000);
    values.put("event_id", event_id);
    values.put("domainname", name);
    db.insertOrThrow("domains_events", null, values);
}

That solution seems to work, even if probably this is not a very good solution.