Skip to content Skip to sidebar Skip to footer

Check If The Event Exists Before Adding It To The Android Calender

I have a list of events in my app. A button on the side lets the user add the event date and time to his/her calender. I use a calender intent to redirect the user to the android c

Solution 1:

You should test, if an instance for this event exists. See the documentation of the Android's CalendarContract.Instances class.

Especially the second query method should be helpful in this case.

This examples is some code, I posted on my blog post about the CalendarContract provider - slightly altered for your needs:

long begin = // starting time in milliseconds
long end = // ending time in milliseconds
String[] proj = 
      new String[]{
            Instances._ID, 
            Instances.BEGIN, 
            Instances.END, 
            Instances.EVENT_ID};
Cursor cursor = 
      Instances.query(getContentResolver(), proj, begin, end, "\"Your event title\"");
if (cursor.getCount() > 0) {
   // deal with conflict
}

Be aware: The time is always in UTC millis since the epoch. So you might have to adjust given the user's timezone.

And the last parameter should contain the title of the event you have added to the calendar. Keep the quotes - otherwise Android looks for "your" or "event" or "title"!

And do not forget to include the necessary permissions.


Solution 2:

Instances.query is not recommended to be run on the UI thread, but can be done efficiently by ensuring start and end time duration is minimized.

The search string will search all values, not just title, so adding a loop to check for that an exact field value is necessary.

public boolean eventExistsOnCalendar(String eventTitle, long startTimeMs, long endTimeMs) {
  if (eventTitle == null || "".equals(eventTitle)) {
    return false;
  }
  if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
    return false;
  }
  // If no end time, use start + 1 hour or = 1 day. Query is slow if searching a huge time range
  if (endTimeMs <= 0) {
    endTimeMs = startTimeMs + 1000 * 60 * 60; // + 1 hour
  }

  final ContentResolver resolver = mContext.getContentResolver();
  final String[] duplicateProjection = {CalendarContract.Events.TITLE}; // Can change to whatever unique param you are searching for
  Cursor cursor =
      CalendarContract.Instances.query(
          resolver,
          duplicateProjection,
          startTimeMs,
          endTimeMs,
          '"' + eventTitle + '"');

  if (cursor == null) {
    return false;
  }
  if (cursor.getCount() == 0) {
    cursor.close();
    return false;
  }

  while (cursor.moveToNext()) {
    String title = cursor.getString(0);
    if (eventTitle.equals(title)) {
      cursor.close();
      return true;
    }
  }

  cursor.close();
  return false;
}

Solution 3:

I have used following way to check it ...what i am passing event_id to check whether is it in calendar or not....

public boolean isEventInCal(Context context, String cal_meeting_id) {

     Cursor cursor = context.getContentResolver().query(
     Uri.parse("content://com.android.calendar/events"),
       new String[] { "_id" }, " _id = ? ",
       new String[] { cal_meeting_id }, null);

           if (cursor.moveToFirst()) {
                   //Yes Event Exist...
      return true;
     }
     return false;
    }

Solution 4:

Please check this, this might help:

private static boolean isEventInCalendar(Context context, String titleText, long dtStart, long dtEnd) {

    final String[] projection = new String[]{CalendarContract.Instances.BEGIN, CalendarContract.Instances.END, CalendarContract.Instances.TITLE};
    Cursor cursor = CalendarContract.Instances.query(context.getContentResolver(), projection, dtStart, dtEnd);
    return cursor != null && cursor.moveToFirst() && cursor.getString(cursor.getColumnIndex(CalendarContract.Instances.TITLE)).equalsIgnoreCase(titleText);
}

Post a Comment for "Check If The Event Exists Before Adding It To The Android Calender"