无法使用重复规则读取日历事件

时间:2013-12-16 17:52:43

标签: android events calendar recurrence rrule

我创建了一个重复规则为“FREQ = MONTHLY; BYMONTHDAY = 16; COUNT = 10”的事件。我在我的两个Android Calendars应用程序以及Google Calendar网站上看到了该活动。我可以从我的应用程序中读取所有其他事件,但我没有看到此事件。唯一的区别是这个事件有重复规则。这是我的阅读代码。

    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 个答案:

答案 0 :(得分:0)

问题解决了。我在where子句中犯了一个错误。

以下是正确的
        String where = "(" + dtstart + ">" 
                    + calendar1.getTimeInMillis() + " and "
                    + dtstart + "<" + calendar2.getTimeInMillis() + " and "+ 
                    CalendarContract.Events.CALENDAR_ID + " = '" + String.valueOf(calendarID) +"')";