将日历活动一天带入Android应用程序

时间:2014-05-14 21:44:30

标签: android android-contentprovider android-calendar

我想知道如何从特定日期的日历中获取活动,我只想阅读所选日期的活动?

通过这种方式阅读所有日历活动:

while (cur.moveToNext()) {
    String title = null;
    long eventID = 0;
    long beginVal = 0;    

    // Get the field values
    eventID = cur.getLong(PROJECTION_ID_INDEX);
    beginVal = cur.getLong(PROJECTION_BEGIN_INDEX);
    title = cur.getString(PROJECTION_TITLE_INDEX);

    // Do something with the values. 
    Log.i(DEBUG_TAG, "Event:  " + title); 
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(beginVal);  
    DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
    Log.i(DEBUG_TAG, "Date: " + formatter.format(calendar.getTime()));    
    }
 }

1 个答案:

答案 0 :(得分:6)

您应该在查询中使用selection按开始日期过滤事件。例如。

Calendar calendar = Calendar.getInstance();
calendar.set(2014, Calendar.MAY, 14, 0, 0, 0);
long startDay = calendar.getTimeInMillis();
calendar.set(2014, Calendar.MAY, 14, 23, 59, 59);
long endDay = calendar.getTimeInMillis();

String[] projection = new String[] { BaseColumns._ID, CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART };
String selection = CalendarContract.Events.DTSTART + " >= ? AND " + CalendarContract.Events.DTSTART + "<= ?";
String[] selectionArgs = new String[] { Long.toString(startDay), Long.toString(endDay) }; 

Cursor cur = getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, selectionArgs, null);