Events add in my calendar programatically using content provider gets deleted after few seconds

Viewed 736

Events get removed automatically from the calendar after few seconds. Here is the code i have used to add events.I think it may be due to syncing of the event. Searched much but didn't get a solution.

ContentValues event = new ContentValues();
event.put(CalendarContract.Events.CALENDAR_ID,3 );
java.util.TimeZone timeZone = java.util.TimeZone.getDefault();
event.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
event.put(CalendarContract.Events.TITLE, "DUMMY Event");
event.put(CalendarContract.Events.DESCRIPTION, "Birthday");
event.put(CalendarContract.Events.EVENT_LOCATION, "My Guest House");
event.put(CalendarContract.Events.DTSTART, ((Long.parseLong(System.currentTimeMillis()+3600000);
event.put(CalendarContract.Events.DTEND, (Long.parseLong(System.currentTimeMillis()+4200000);
event.put(CalendarContract.Events.ALL_DAY, 0); 
event.put(CalendarContract.Events.HAS_ALARM, 1); event.put(CalendarContract.Events.CALENDAR_TIME_ZONE, timeZone);
Uri baseUri;
if (Build.VERSION.SDK_INT >= 8) {
     baseUri = Uri.parse("content://com.android.calendar/events");
} else {
     baseUri = Uri.parse("content://calendar/events");
}
Uri uri=getContentResolver().insert(baseUri, event);

//Getting Gmail Calendar

public static String getGmailCalendarId(Context c) {

        String projection[] = {"_id", "calendar_displayName"};
        Uri calendars;
        calendars = 
Uri.parse("content://com.android.calendar/calendars");
        ContentResolver contentResolver = c.getContentResolver();
        Cursor managedCursor = contentResolver.query(calendars, 
   projection, null, null, null);
        if (managedCursor.moveToFirst())
        {
            String calName;
            String calID;
            int nameCol = managedCursor.getColumnIndex(projection[1]);
            int idCol = managedCursor.getColumnIndex(projection[0]);
            do
            {
                calName = managedCursor.getString(nameCol);
                calID = managedCursor.getString(idCol);
                if(calName.contains("@gmail")){
                    calenderId = calID;
                }


            } while (managedCursor.moveToNext());
            managedCursor.close();
            return calenderId;
        }

    return calenderId;

}
0 Answers
Related