Android Calendar Provider: How can I delete my own local calendars?
Asked Answered
M

3

5

I am just learning how to work with the Android Calendars. So far, I am able to display the info about existing calendars. I can also create my own local calendars -- the test code like:

private void createCalendarTest()
{
    Uri.Builder builder = Calendars.CONTENT_URI.buildUpon();
    builder.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "true")
           .appendQueryParameter(Calendars.ACCOUNT_NAME, "private")
           .appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);

    Uri uri = builder.build();

    ContentValues values = new ContentValues();
    values.put(Calendars.NAME, "TEST");
    values.put(Calendars.CALENDAR_DISPLAY_NAME, "Calendar named TEST");
    values.put(Calendars.SYNC_EVENTS, false);
    values.put(Calendars.VISIBLE, true);

    getContentResolver().insert(uri, values);
}

Actually, I can create many calendars that differ only in _ID. I have read elsewhere that I can create a calendar only when using the sync adapter. Now, how can I delete the calendar? I expect the URI must also contain the sync adapter info, and the _ID of the deleted calendar. I tried the following code, but I was unsuccessful:

private void deleteCalendarTest()
{
    Uri.Builder builder = Calendars.CONTENT_URI.buildUpon();
    builder.appendPath("6")   // here for testing; I know the calender has this ID
           .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "true")
           .appendQueryParameter(Calendars.ACCOUNT_NAME, "private")
           .appendQueryParameter(Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);

    Uri uri = builder.build();

    getContentResolver().delete(uri, null, null);
    Toast.makeText(this, "??? deleteCalendarTest() not working", Toast.LENGTH_SHORT).show();
}

How can I fix it?

Micaelamicah answered 21/7, 2014 at 13:43 Comment(3)
Did you find any fix for the issue? I'm in the same situation of you!Zaid
No, I did not fix it. That part of the project was stopped, and I did not continue to search for the solution.Micaelamicah
I found a solution and I answered to the question... maybe it will help someone else!Zaid
Z
5

After reading with more attention the documentation, i found out you should add to the content values the following fields too:

values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
values.put(CalendarContract.Calendars.ACCOUNT_NAME, "private");
values.put(CalendarContract.Calendars.ACCOUNT_TYPE,CalendarContract.ACCOUNT_TYPE_LOCAL);

Then everything else should be fine and you should be able to delete the inserted calendar! ;)

Zaid answered 10/6, 2015 at 14:17 Comment(2)
I added all these parameters to create a calender but still not able to delete the local calender which i create programitically from the applicationLaveen
If you use the fields I mentioned for calendar creation and you append to the ContentProvider's URI the three query parameters used by @pepr, you shouldn't have problems. Maybe there's something wrong in your code. Anyway, the documentation helps a lot: developer.android.com/guide/topics/providers/…Zaid
T
3

This Kotlin solution worked for me:

val uri = ContentUris.withAppendedId(CalendarContract.Calendars.CONTENT_URI, calendarId.toLong())

contentResolver.delete(uri, null, null)

I'm removing the calendar with the application (not a SyncAdapter) so there's no need to append any query parameters. Just append the calender's id to the content uri and use a ContentResolver to delete the calendar.

Terraterrace answered 12/11, 2020 at 18:22 Comment(0)
W
0

"andrea-rinaldi" was right. The below code snippet worked for me. The "calendarHandler" is an instance of the helper class that extends AsyncQueryHandler, the one you used to create the calendar.

Uri calUri = CalendarContract.Calendars.CONTENT_URI;
calUri = calUri.buildUpon()
            .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, BuildConfig.APPLICATION_ID)
            .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
            .build();
calendarHandler.startDelete(0,-1,calUri,null,null);
Wellbeloved answered 27/4, 2018 at 14:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.