Con not save more than one Calendar event on the same day!! Xamarin.Android

EB1234EB1234 USMember

I want to save calendar events on the phone's native calendar app. The first event is saved without problems. But whenever I save another one on the same date with different time range it does not appear in the calendar app even though there are no run-time errors.

this is my code:

public bool SaveEventInCalendar(int ID, string Title, string Description, DateTime StartFrom, DateTime EndAt)
{
ContentValues eventValues = new ContentValues();
Context c = Android.App.Application.Context;
ContentResolver cr = c.ContentResolver;

        var calendarsUri = CalendarContract.Calendars.ContentUri;

        string[] calendarsProjection = {CalendarContract.Calendars.InterfaceConsts.Id,
            CalendarContract.Calendars.InterfaceConsts.CalendarDisplayName,
            CalendarContract.Calendars.InterfaceConsts.AccountName};

        var cursor = c.ContentResolver.Query(calendarsUri, calendarsProjection, null, null, null);

        cursor.MoveToPosition(1);
        int calId = cursor.GetInt(cursor.GetColumnIndex(calendarsProjection[0]));

        eventValues.Put(CalendarContract.Events.InterfaceConsts.CalendarId, calId);
        eventValues.Put(CalendarContract.Events.InterfaceConsts.Title, Title);
        eventValues.Put(CalendarContract.Events.InterfaceConsts.Description, Description + ": " + ID.ToString());
        eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtstart, 
            GetDateTimeMS(StartFrom.Year, StartFrom.Month, StartFrom.Day, StartFrom.Hour, StartFrom.Minute));
        eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtend,
            GetDateTimeMS(EndAt.Year, EndAt.Month, EndAt.Day, EndAt.Hour, EndAt.Minute)); 
        eventValues.Put(CalendarContract.Events.InterfaceConsts.HasAlarm, true);
        eventValues.Put(CalendarContract.Events.InterfaceConsts.EventTimezone, "UTC");

        eventValues.Put(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "UTC");

        try
        {
           var uri = cr.Insert(CalendarContract.Events.ContentUri, eventValues);
           Console.WriteLine("Uri for new event: {0}", uri);
            return true;
        }
        catch(Exception e)
        {
            Console.WriteLine("IOException source: {0}", e.Source);
            return false;
        }
    }

    long GetDateTimeMS(int yr, int month, int day, int hr, int min)
    {
        Calendar c = Calendar.GetInstance(Java.Util.TimeZone.Default);

        c.Set(CalendarField.DayOfMonth, day);
        c.Set(CalendarField.HourOfDay, hr);
        c.Set(CalendarField.Minute, min);
        c.Set(CalendarField.Month, month - 1);
        c.Set(CalendarField.Year, yr);

        return c.TimeInMillis;
    }

Best Answer

Answers

Sign In or Register to comment.