8

I have searched from the internet a lot and tried many examples . I can successfully add event to the calendar through my application but I can't delete this event programatically. Here are the samples I have tried that I cant end up a successful result .

tokens[1] is event id.
1)

  Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
  Uri eventUri = ContentUris.withAppendedId(eventsUri, Long.parseLong(tokens[1]));
  getContentResolver().delete(eventUri, null, null); 

2)

 ContentResolver cr = FlightOperationsCancelTicketFee.this.getContentResolver();
 Uri EVENTS_URI =    Uri.parse("content://com.android.calendar/" + "events");
 deleteEvent(cr, EVENTS_URI, 1);


 private void deleteEvent(ContentResolver resolver, Uri eventsUri, int calendarId) 
 {
     Cursor cursor;
     if (android.os.Build.VERSION.SDK_INT <= 7) 
     { 
         cursor = resolver.query(eventsUri, new String[]{ "_id" }, "Calendars_id=" + calendarId, null, null);
     } 
     else 
     { 
         cursor = resolver.query(eventsUri, new String[]{ "_id" }, "calendar_id=" + calendarId, null, null);
     }
     while(cursor.moveToNext()) 
     {
         long eventId = cursor.getLong(cursor.getColumnIndex("_id"));
         resolver.delete(ContentUris.withAppendedId(eventsUri, eventId), null, null);
     }
     cursor.close();
 }

3)

 ContentResolver cr = getContentResolver();
 String calUriString = "content://com.android.calendar/events";
 Uri cal=Uri.parse(calUriString);
 String[] EVENT_PROJECTION=new String[]{"calendar_id","title","dtstart","_id"};


 Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
 Uri eventUri =ContentUris.withAppendedId(eventsUri, Long.parseLong(tokens[1]));
 String reminderUriString = "content://com.android.calendar/reminders";
 Uri remUri =Uri.parse(reminderUriString);
     cr.delete(remUri, "event_id="+Commons.event_id, null);
 cr.delete(eventUri, null, null);

4)

   Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
   Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, Long.parseLong(tokens[1]));
   getContentResolver().delete(eventUri, null, null);

None of the above does work . I need help . Thank you .. Edit : I think I can't send the right context , is there a way to keep context via shared preferences ? However , it only keeps String and Int values . Is there another way to do something like this ?

sesamoslu
  • 145
  • 2
  • 10

3 Answers3

6

Use this code,It worked for me:

 Uri deleteUri = null;
        deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, Long.parseLong(String.valueOf(eventID)));
        int rows = getContentResolver().delete(deleteUri, null, null);
        Toast.makeText(this, "Event deleted", Toast.LENGTH_LONG).show();
0

The best documentation on the subject is here: http://developer.android.com/guide/topics/providers/calendar-provider.html

But your basically doing it by the book in (1).

My suggestion is to first make sure you have the right id, second test on another device etc. Also test if you can get the event again after you've deleted it.

Warpzit
  • 27,966
  • 19
  • 103
  • 155
0

You should add events to calendar with different _id as calander_id act as a primary key. So if you want a particular record to delete just give that id to delete command.

eltabo
  • 3,749
  • 1
  • 21
  • 33
vik
  • 1
  • 1