Skip to content Skip to sidebar Skip to footer

Insert Multiple Events In Android Calendar

I am having trouble in finding a solution to my problem. I am trying to add multiple events into my Android calendar but I do not know how. I gave found this code : Calend

Solution 1:

android SDK does not expose any interfaces to manipulate calendar, but you can find that from android source code. Android stores calendar in an internal sqlite database, though it's protected from direct access but you can still add/delete/update/query calendar data through ContentResolver. A piece of code to insert an event could be like this:

public String addEvent(String calendarId, String title, long startTime,
        long endTime, int allDay) {
    ContentValues event = new ContentValues();
    event.put("calendar_id", calendarId); // "" for insertevent.put("title", title);
    event.put("description", "");
    event.put("eventLocation", "");
    event.put("allDay", allDay);
    event.put("eventStatus", 1);
    event.put("transparency", 0);
    event.put("dtstart", startTime);
    event.put("dtend", endTime);

    ContentResolver contentResolver = this.context.getContentResolver();
    Uri eventsUri = Uri.parse("content://com.android.calendar/calendars");
    Uri url = contentResolver.insert(eventsUri, event);
    String ret = url.toString();
    return ret;
}

When you insert one event successfully, a string represent uri of the event is returned by ContentResolver.insert, you can query, update or delete it later. In earlier SDKs before SDK 8, the content uri of calendar is "content://calendar/calendars" which differs from SDks 8 and after. Meanwhile, be careful with those custom roms. since calendar API is not noted in SDK docs, the calendar provider could be modified or even removed by some vendors and operators, so you may have to test your application against lots of devices. Good luck!

Solution 2:

public Uri createEvent(int calId, ICalEvent event) {
        ContentValues eventValues = new ContentValues();

        eventValues.put("calendar_id", calId);
        eventValues.put("title", event.getSummary());
        eventValues.put("description", event.getDescription());
        eventValues.put("eventLocation", event.getLocation());

// eventValues.put("UID", "ADE52556e6976657273697465323031302f323031312d323637392d302d3131");// originalEvent
        eventValues.put("originalEvent", event.getUid() + ";" + event.getLastModif().getTime() );

        long startTime = event.getStart().getTime();//System.currentTimeMillis() + 1000 * 60 * 60;long endTime = event.getEnd().getTime();//System.currentTimeMillis() + 1000 * 60 * 60 * 2;

        eventValues.put("dtstart", (event.isWholeDayEvent() ? endTime : startTime));
        eventValues.put("dtend", endTime);

        eventValues.put("allDay", (event.isWholeDayEvent() ? 1 : 0)); // 0 for false, 1 for true
        eventValues.put("eventStatus", 1);
        eventValues.put("visibility", 0);
        eventValues.put("transparency", 0);
        eventValues.put("hasAlarm", 0); // 0 for false, 1 for true

        System.out.println("USING SYNC ACCOUNT " + sync_account);

        eventValues.put("_sync_account_type", sync_account);

        Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");

        Uri insertedUri = activity.getContentResolver().insert(eventsUri, eventValues);
        return insertedUri;
    }

Post a Comment for "Insert Multiple Events In Android Calendar"