6

Hi I have the following code to add events on my calendar:

public String addCalendarEntry(CalendarDTO calendar) {

        ContentValues event = new ContentValues();
        ContentResolver cr = getContentResolver();

        long startMillis = calendar.getStartDate().getTimeInMillis();
        long endMillis = calendar.getEndDate().getTimeInMillis();
        String timeZone = TimeZone.getDefault().getID();

        event.put(CalendarContract.Events.CALENDAR_ID, calendar.getId());
        event.put(CalendarContract.Events.TITLE, calendar.getTitle());
        event.put(CalendarContract.Events.DESCRIPTION, calendar.getDescription());
        event.put(CalendarContract.Events.EVENT_LOCATION, calendar.getLocation());
        event.put(CalendarContract.Events.DTSTART, startMillis);
        event.put(CalendarContract.Events.DTEND, endMillis);
        event.put(CalendarContract.Events.ALL_DAY, Boolean.getBoolean(String.valueOf(calendar.isAllDay())));   // 0 for false, 1 for true
        event.put(CalendarContract.Events.HAS_ALARM, 1); // 0 for false, 1 for true
        event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);

        Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, event);

        return uri.getLastPathSegment();
}

This code was working fine until the other day, and all of a sudden doesn't add any more events into my calendar. It doesn't error either, so I'm at loss here.

CalendarDTO is just a data transport class as such:

public class CalendarDTO {
    int id;
    String title;
    String description;
    String location;
    Calendar startDate;
    Calendar endDate;
    boolean isAllDay;

// getters and setters removed for brevity
}

Any clues highly appreciated.

Marcos Placona
  • 21,468
  • 11
  • 68
  • 93
  • I did, but this doesn't necessarily answer my question. You're only suggesting an alternative. I;m not gonna downvote as you tried to help, but an alternative is not the answer, unless you're sure the Android version is broken. – Marcos Placona Jul 08 '15 at 13:08
  • ok sir I deleted my answer – Maveňツ Jul 09 '15 at 04:50

1 Answers1

6

Do you check that there is a calendar with calendar.getId()?

And from the docs: http://developer.android.com/reference/android/provider/CalendarContract.Events.html

Writing to Events There are further restrictions on all Updates and Inserts in the Events table:

If allDay is set to 1 eventTimezone must be TIMEZONE_UTC and the time must correspond to a midnight boundary.

Try with something like this:

event.put(CalendarContract.Events.CALENDAR_ID, calendar.getId());
    event.put(CalendarContract.Events.TITLE, calendar.getTitle());
    event.put(CalendarContract.Events.DESCRIPTION, calendar.getDescription());
    event.put(CalendarContract.Events.EVENT_LOCATION, calendar.getLocation());
    if(calendar.isAllDay){
        Calendar start=calendar.getStartDate();
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);
        Calendar end=start;
        end.add(Calendar.DAY_OF_MONTH,1);
        event.put(CalendarContract.Events.DTSTART, start.getTimeInMillis());
        event.put(CalendarContract.Events.DTEND, end.getTimeInMillis());
        event.put(CalendarContract.Events.EVENT_TIMEZONE, Time.TIMEZONE_UTC);
    }else{
        event.put(CalendarContract.Events.DTSTART, startMillis);
        event.put(CalendarContract.Events.DTEND, endMillis);
        event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);
    }
    event.put(CalendarContract.Events.HAS_ALARM, 1);
    event.put(CalendarContract.Events.ALL_DAY, calendar.isAllDay()?1:0);
isma3l
  • 3,833
  • 1
  • 22
  • 28