4

我正在使用以下代码在我的应用程序中获取和加载日历事件。它运行良好。但现在我想获取指定日期范围的事件。我怎么才能得到它..

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

应该是
dtstart = "2013-01-01"
dtend = "2013-01-31"

4

2 回答 2

18
String[] projection = new String[] { CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION };

// 0 = January, 1 = February, ...

Calendar startTime = Calendar.getInstance();
startTime.set(2014,00,01,00,00);

Calendar endTime= Calendar.getInstance();
endTime.set(2015,00,01,00,00);

// the range is all data from 2014

String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ))";

Cursor cursor = this.getBaseContext().getContentResolver().query( CalendarContract.Events.CONTENT_URI, projection, selection, null, null );

// output the events 

if (cursor.moveToFirst()) {
    do {
        Toast.makeText( this.getApplicationContext(), "Title: " + cursor.getString(1) + " Start-Time: " + (new Date(cursor.getLong(3))).toString(), Toast.LENGTH_LONG ).show();
    } while ( cursor.moveToNext());
}
于 2014-11-11T09:15:47.620 回答
3

这是我为检索事件而实施的:

if (Integer.parseInt(Build.VERSION.SDK) >= 8 || Integer.parseInt(Build.VERSION.SDK) <= 13 ) {

            uri = "content://com.android.calendar/events";
            CALENDAR_URI = Uri.parse(uri);

        } else if(Integer.parseInt(Build.VERSION.SDK) >= 14){

             CALENDAR_URI = CalendarContract.Events.CONTENT_URI;

        }
        else {

            uri = "content://calendar/events";
            CALENDAR_URI = Uri.parse(uri);

        }
Cursor cursors = context.getContentResolver().query(CALENDAR_URI, new String[]{ "_id", "title", "description", "dtstart", "dtend", "eventLocation" },
                null,null, null);



 cursors.moveToFirst();
                                    String[] CalNames = new String[cursors.getCount()];
                                    int[] CalIds = new int[cursors.getCount()];
                                    for (int i = 0; i < CalNames.length; i++) {
                                        CalIds[i] = cursors.getInt(0);
                                        CalNames[i] = "Event"+cursors.getInt(0)+": \nTitle: "+ cursors.getString(1)+"\nDescription: "+cursors.getString(2)+"\nStart Date: "+new Date(cursors.getLong(3))+"\nEnd Date : "+new Date(cursors.getLong(4))+"\nLocation : "+cursors.getString(5);   

                                        Date mDate = new Date(cursors.getLong(3));
                                        Date nDate = new Date(cursors.getLong(4));

                                        long mTime = mDate.getTime();
                                        long lTime = nDate.getTime();
                                        if(stTime <= mTime && enTime >= lTime){ 
 String eid = cursors.getString(0);

                                            int eID = Integer.parseInt(eid);

                                            String desc = cursors.getString(2);
                                            String title = cursors.getString(1);

在这种情况下,您的 stTime 将是 yo dtstart,而 enTime 将是您的 dtend。

于 2013-03-11T05:49:24.690 回答