androidandroid-contentresolverreminders

How to remove reminder from event in android?


When I set a reminder for an event can not remove it! I've already test this code:

Uri reminderUri = ContentUris.withAppendedId(CalendarContract.Reminders.CONTENT_URI, reminder.id);
context.getContentResolver().update(reminderUri, getEmptyReminderContentValues(reminder), null, null);
context.getContentResolver().delete(reminderUri, null, null);

I seen some calendars include google calendar do this but some of them do not. Anyone can help me?


Solution

  • You can use following method to delete the reminder set on event

    public static void deleteReminderOnEvent(Long reminderId) {
        Uri reminderUri = ContentUris.withAppendedId(CalendarContract.Reminders.CONTENT_URI, reminderId);
        int rows = contentResolver.delete(reminderUri, null, null);
    }
    

    You need to use following method before above method to get the reminderId for the event and then pass it to above method.

    private static Long checkIfReminderExist(ContentResolver contentResolver, long eventId) {
        Long reminderId = null;
    
        String[] projection = new String[]{
                CalendarContract.Reminders._ID,
                CalendarContract.Reminders.METHOD,
                CalendarContract.Reminders.MINUTES
        };
    
        Cursor cursor = CalendarContract.Reminders.query(contentResolver, eventId, projection);
    
        while (cursor != null && cursor.moveToNext()) {
            reminderId = cursor.getLong(0);
        }
    
        cursor.close();
    
        return reminderId;
    }