Search code examples
javaandroidcalendarandroid-calendarandroid-event

Android calendar deletes calendar and events after 10 minutes


I've been working on an app that involves making events. I've successfully created a calendar on the default calendar on Android, and I've been able to add, delete, or update events on this calendar. However, I noticed the calendar I create gets automatically delete about approximately 10 minutes. Could this be an issue with S planner (default calendar) or Google Calendar? This is how I'm making my calendar:

    Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString());
    target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName)
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, Utils.APP_PACKAGE).build();


    ContentValues values = new ContentValues();
    values.put(CalendarContract.Calendars._ID, MY_CALENDAR_ID);
    values.put(CalendarContract.Calendars.ACCOUNT_NAME, accountName);
    values.put(CalendarContract.Calendars.ACCOUNT_TYPE, Utils.APP_PACKAGE);
    values.put(CalendarContract.Calendars.NAME, "TBDAppName (" + accountName + ")");
    values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, Utils.APP_PACKAGE);
    values.put(CalendarContract.Calendars.CALENDAR_COLOR, 0x00FF00);
    values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
    values.put(CalendarContract.Calendars.OWNER_ACCOUNT, accountName);
    values.put(CalendarContract.Calendars.VISIBLE, 1);
    values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);

    context.getContentResolver().insert(target, values);

and this is how I'm inserting events:

   long startMillis = 0;
   long endMillis = 0;
   Calendar beginTime = Calendar.getInstance();
   beginTime.set(2018, 01, 01, 7, 30);  //random time to test inserting.
   startMillis = beginTime.getTimeInMillis();
   ContentValues values = new ContentValues();
   values.put(Events.CALENDAR_ID, MY_CALENDAR_ID);
   values.put(Events._ID, MY_EVENT_ID);
   values.put(Events.TITLE, MY_EVENT_TITLE);
   values.put(Events.DESCRIPTION, "test description");
   values.put(Events.DTSTART, startMillis);
   values.put(Events.DTEND, startMillis);
   values.put(Events.CALENDAR_ID, MY_CALENDAR_ID);
   TimeZone timeZone = TimeZone.getDefault();
   values.put(Events.EVENT_TIMEZONE, timeZone.getID());
   context.getContentResolver().insert(Events.CONTENT_URI, values);

Solution

  • When you create the calendar, you have to set it as ACCOUNT_TYPE_LOCAL, otherwise things will get deleted. This is described in http://developer.android.com/reference/android/provider/CalendarContract.html as follows;

    public static final String ACCOUNT_TYPE_LOCAL

    A special account type for calendars not associated with any account. Normally calendars that do not match an account on the device will be removed. Setting the account_type on a calendar to this will prevent it from being wiped if it does not match an existing account.