4

In my application i'm using the CalendarContract API to manage calendars and events on the Google Calendar. I'm having some problems when adding multiple events to a single calendar. In general, the problem of this method is that whenever i add a new event to the google calendar, the old event is replaced by the new one. Here is the code of my method:

    public int addEvent(Context context, String color, int calendarId, String name, String location, String description, Date dateBegin, Date dateEnd) {
            long startMillisEpoch;
            long endMillisEpoch;

            Calendar beginTime = Calendar.getInstance();
            beginTime.setTime(dateBegin);
            startMillisEpoch = beginTime.getTimeInMillis();

            Calendar endTime = Calendar.getInstance();
            endTime.setTime(dateEnd);
            endMillisEpoch = endTime.getTimeInMillis();

            int eventColor;
            try {
                eventColor = Color.parseColor(color);
            } catch (Exception e) {
                // Get a random color
                Random random = new Random();
                eventColor = MyApp.RANDOM_COLORS[random.nextInt(MyApp.RANDOM_COLORS.length)];
            }
            ContentValues values = new ContentValues();
            values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
            values.put(CalendarContract.Events.DTSTART, startMillisEpoch);
            values.put(CalendarContract.Events.DTEND, endMillisEpoch);
            values.put(CalendarContract.Events.TITLE, name);
            values.put(CalendarContract.Events.EVENT_LOCATION, location);
            values.put(CalendarContract.Events.DESCRIPTION, description);
            values.put(CalendarContract.Events.EVENT_COLOR, eventColor);
            // NOTE: Every event MUST have a timezone. Otherwise,
            // the application will throw an IllegalArgumentException
            values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getDisplayName());
            // Put default values
            values.put(CalendarContract.Events.ALL_DAY, NON_ALL_DAY);
            values.put(CalendarContract.Events.GUESTS_CAN_INVITE_OTHERS, 1);
            Uri calUri = context.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, values);
            if (calUri != null) {
                try {
                    return Integer.parseInt(calUri.getLastPathSegment());
                } catch (Exception e) {
                    return -1;
                }
            }
            return -1;
        }

Any ideas for solving this kind of problem? Kind regards.

Ricardo Ribas
  • 409
  • 4
  • 14

1 Answers1

1

According the API i guess you're missing .execute()

Uri calUri = context.getContentResolver()
                    .insert(CalendarContract.Events.CONTENT_URI, values)
                    .execute();

Also check this question

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Sorry for my unexperience, but the Uri class does not have any method "execute". Thanks for your answer, first of all. – Ricardo Ribas May 14 '15 at 11:57
  • are you sure you need to use `Uri` and not `Event`? Did you checked the related api and questions? – Jordi Castilla May 14 '15 at 12:01
  • I'm using CalendarContract to allow local calendars. That's what i need in my case. – Ricardo Ribas May 14 '15 at 13:12
  • Another thing. There is any known-issue of the google calendar app or the CalendarContract API that if we add an event title with too many characters, that same event is added to all days of the event month? I added an event with a small amount of characters and an event with a large amount of characters. That "bug" didn't happen for the first case. Weird stuff. – Ricardo Ribas May 14 '15 at 16:44