Cannot Read Calendar Event with Recurrence Rule

997 views Asked by At

I created an event with repeat rule "FREQ=MONTHLY;BYMONTHDAY=16;COUNT=10". I saw the event on my two Android Calendars apps, and also on Google Calendar web site. I can read all other events from my app, but I do not see this event. Only difference is that this event has repeat rule. Here is my code for reading.

    public static MyEvents readCalendarMyEvent(Context context,Date date1, Date date2, int calendarID) 
{
    try
    {
            Calendar calendar1 = Calendar.getInstance();
            calendar1.setTime(date1);
            Calendar calendar2 = Calendar.getInstance();
            calendar2.setTime(date2);
            Uri l_eventUri;

            if (Build.VERSION.SDK_INT >= 8) {
                l_eventUri = Uri.parse("content://com.android.calendar/events");
            } else {
                l_eventUri = Uri.parse("content://calendar/events");
            }
            ContentResolver contentResolver = context.getContentResolver();
            String dtstart = "dtstart";
            String dtend = "dtend";

            String[] l_projection = new String[] { CalendarContract.Events.CALENDAR_ID,
                    CalendarContract.Events.TITLE, //1
                    CalendarContract.Events.DESCRIPTION,
                    CalendarContract.Events.DTSTART,
                    CalendarContract.Events.DTEND,
                    CalendarContract.Events.EVENT_LOCATION,
                    CalendarContract.Events.EVENT_TIMEZONE,//6
                    CalendarContract.Events.HAS_ALARM,
                    CalendarContract.Events.RRULE,
                    CalendarContract.Events.RDATE,
                    CalendarContract.Events.ORGANIZER,
                    CalendarContract.Events.SELF_ATTENDEE_STATUS,//11
                    CalendarContract.Events.GUESTS_CAN_MODIFY,
                    CalendarContract.Events._ID,                
                    CalendarContract.Events.ALL_DAY,
                    CalendarContract.Events.DELETED,
                    CalendarContract.Events.GUESTS_CAN_INVITE_OTHERS,//16
                    CalendarContract.Events.GUESTS_CAN_SEE_GUESTS,
                    CalendarContract.Events.ALL_DAY,
                    CalendarContract.Events.ACCESS_LEVEL,
                    CalendarContract.Events.AVAILABILITY,
                    CalendarContract.Events.EXRULE,
                    CalendarContract.Events.EXDATE
                    };
            String where = "(" + dtstart + ">" 
                    + calendar1.getTimeInMillis() + " and "
                    + dtend + "<" + calendar2.getTimeInMillis() + " and "+ 
                    CalendarContract.Events.CALENDAR_ID + " = '" + String.valueOf(calendarID) +"')";
            Cursor  cursor= contentResolver.query(l_eventUri, l_projection, where, null,
                    "dtstart ASC");
            cursor.moveToFirst();
            // fetching calendars id
            String str="";
            MyEvents myEvents = new MyEvents();
            for (int i = 0; i < cursor.getCount(); i++) {
                int delete = cursor.getInt(15); 
                if( delete == 1)
                {
                    cursor.moveToNext();
                    continue;
                }
                MyEvent myEvent = new MyEvent(); 
                myEvent.ID = cursor.getInt(0);
                myEvent.TITLE = cursor.getString(1);            
                myEvent.Note = cursor.getString(2);
                str = cursor.getString(3);
                if(str != null || str.length() <1)
                {
                    myEvent.START_DATE = MillisecondsToDateTime(Long.parseLong(str));
                    myEvent.StartDate = getDate(Long.parseLong(str));
                }
                else
                {
                    cursor.moveToNext();
                    continue;               
                }
                str = cursor.getString(4);
                if(str != null || str.length() <1)
                    myEvent.END_DATE = MillisecondsToDateTime(Long.parseLong(str));
                myEvent.EVENT_LOCATION =cursor.getString(5);
                myEvent.EVENT_TIMEZONE =cursor.getString(6);
                myEvent.HAS_ALARM = cursor.getInt(7);
                myEvent.RRULE =  cursor.getString(8);
                myEvent.RDATE = cursor.getString(9);
                myEvent.ORGANIZER  =cursor.getString(10);           
                myEvent.SELF_ATTENDEE_STATUS =  cursor.getInt(11);          
                myEvent.GUESTS_CAN_MODIFY = cursor.getInt(12);
                myEvent.EVENT_ID = cursor.getInt(13);
                myEvent.ALL_DAY = cursor.getInt(14);
                myEvent.GUESTS_CAN_INVITE_OTHERS= cursor.getInt(16);
                myEvent.GUESTS_CAN_SEE_GUESTS= cursor.getInt(17);
                myEvent.ALL_DAY= cursor.getInt(18);
                myEvent.ACCESS_LEVEL= cursor.getInt(19);
                myEvent.AVAILABILITY= cursor.getInt(20);
                myEvent.EXRULE =  cursor.getString(21);
                myEvent.EXDATE = cursor.getString(22);          
                myEvents.Add(myEvent);
                cursor.moveToNext();
            }
            return myEvents;
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    return new MyEvents();
}
1

There are 1 answers

0
codingForFun On BEST ANSWER

The problem was resolved. I made a mistake in the where clause. The following is correct on

        String where = "(" + dtstart + ">" 
                    + calendar1.getTimeInMillis() + " and "
                    + dtstart + "<" + calendar2.getTimeInMillis() + " and "+ 
                    CalendarContract.Events.CALENDAR_ID + " = '" + String.valueOf(calendarID) +"')";