Reputation: 20794
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?
Upvotes: 5
Views: 2876
Reputation: 396
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.
Upvotes: 3
Reputation: 131
"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);
Upvotes: 0
Reputation: 1187
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! ;)
Upvotes: 5