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:

longbegin=// starting time in millisecondslongend=// ending time in milliseconds
String[] proj = 
      newString[]{
            Instances._ID, 
            Instances.BEGIN, 
            Instances.END, 
            Instances.EVENT_ID};
Cursorcursor= 
      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.

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

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

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

  while (cursor.moveToNext()) {
    Stringtitle= cursor.getString(0);
    if (eventTitle.equals(title)) {
      cursor.close();
      returntrue;
    }
  }

  cursor.close();
  returnfalse;
}

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....

publicbooleanisEventInCal(Context context, String cal_meeting_id) {

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

           if (cursor.moveToFirst()) {
                   //Yes Event Exist...returntrue;
     }
     returnfalse;
    }

Solution 4:

Please check this, this might help:

privatestaticbooleanisEventInCalendar(Context context, String titleText, long dtStart, long dtEnd) {

    final String[] projection = newString[]{CalendarContract.Instances.BEGIN, CalendarContract.Instances.END, CalendarContract.Instances.TITLE};
    Cursorcursor= 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"