Adding reminder to event fails in Android

877 views Asked by At

I have a method which adds an reminder to an event, but it fails:

FATAL EXCEPTION: main
android.database.sqlite.SQLiteException
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:184)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
        at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
        at android.content.ContentResolver.insert(ContentResolver.java:864)
        at de.appwege.droid.medwege.navigationdrawer.TerminFragment.insertReminder(TerminFragment.java:848)

The method in question:

public long insertReminder(long eventID, int minutes){
    ContentResolver cr = getActivity().getContentResolver();
    ContentValues values = new ContentValues();
    values.put(CalendarContract.Reminders.MINUTES, minutes);
    values.put(CalendarContract.Reminders.EVENT_ID, eventID);
    values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
    Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
    return Long.valueOf(uri.getLastPathSegment());
}

What I am missing here? both eventID and minutes are defined...

1

There are 1 answers

0
Parth Jadav On

Recently, I also faced same issue. Finally, I found the solution.

First of all, you have to find all logged in gmail id from the device and then select any one gmail account and find its calendar id. After that you have to pass that id to the event query like this....

values.put(Events.CALENDAR_ID, calendarId);

at last call you function

public long insertReminder(long eventID, int minutes){
      ContentResolver cr = getActivity().getContentResolver();
      ContentValues values = new ContentValues();
      values.put(CalendarContract.Reminders.MINUTES, minutes);
      values.put(CalendarContract.Reminders.EVENT_ID, eventID);
      values.put(CalendarContract.Reminders.METHOD, 
      
      CalendarContract.Reminders.METHOD_ALERT);
      Uri uri = cr.insert(CalendarContract.Reminders.CONTENT_URI, values);
      return Long.valueOf(uri.getLastPathSegment());
}

See below method for finding email id's...

 public static Hashtable listCalendarId(Context context) {

    try {
        if (haveCalendarReadWritePermissions((Activity) context)) {

            String projection[] = {"_id", "calendar_displayName"};
            Uri calendars;
            calendars = Uri.parse("content://com.android.calendar/calendars");

            ContentResolver contentResolver = c.getContentResolver();
            Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);

            if (managedCursor.moveToFirst()) {
                String calName;
                String calID;
                int cont = 0;
                int nameCol = managedCursor.getColumnIndex(projection[1]);
                int idCol = managedCursor.getColumnIndex(projection[0]);
                Hashtable<String, String> calendarIdTable = new Hashtable<>();
                do {
                    calName = managedCursor.getString(nameCol);
                    calID = managedCursor.getString(idCol);
                    Log.v(TAG, "CalendarName:" + calName + " ,id:" + calID);
                    calendarIdTable.put(calName, calID);
                    cont++;
                } while (managedCursor.moveToNext());

                managedCursor.close();

                return calendarIdTable;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}