将日历活动一天带入Android应用程序
作者:互联网
我想知道如何从特定日期的日历中获取活动我只想阅读所选日期的活动?
这样读取所有日历事件:
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()));
}
}
解决方法:
您应该在查询中使用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);
标签:android-calendar,android,android-contentprovider 来源: https://codeday.me/bug/20190825/1715438.html