How to update and remove calendar event in android

2019-09-19 08:03发布

问题:

When you add event. I have this code but I think that I must put id

Calendar cal = Calendar.getInstance();              
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", cal.getTimeInMillis());
    intent.putExtra("allDay", true);
    intent.putExtra("rrule", "FREQ=YEARLY");
    intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
    intent.putExtra("title", "A Test Event from android app");
    intent.putExtra("color", mCalendarPrivate.getColor());
    startActivity(intent);

How Can I update and remove events? I hope that somebody can help me.

回答1:

For update a remove an event you to have eventId ...

Update:

  private int UpdateCalendarEntry(int entryID) {
    int iNumRowsUpdated = 0;

    ContentValues event = new ContentValues();

    event.put("title", "Changed Event Title");
    event.put("hasAlarm", 1); // 0 for false, 1 for true

    Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
    Uri eventUri = ContentUris.withAppendedId(eventsUri, entryID);

    iNumRowsUpdated = getContentResolver().update(eventUri, event, null,
            null);

    Log.i(DEBUG_TAG, "Updated " + iNumRowsUpdated + " calendar entry.");

    return iNumRowsUpdated;
}

Remove:

private int DeleteCalendarEntry(int entryID) {
  int iNumRowsDeleted = 0;

  Uri eventsUri = Uri.parse(getCalendarUriBase()+"events");
  Uri eventUri = ContentUris.withAppendedId(eventsUri, entryID);
  iNumRowsDeleted = getContentResolver().delete(eventUri, null, null);

  Log.i(DEBUG_TAG, "Deleted " + iNumRowsDeleted + " calendar entry.");

  return iNumRowsDeleted;
}