我需要做一件并不复杂的事,人们可以相信。我想从手机上的日历中获取事件实例以及事件的标题。我在Stack Overflow上找到了几个答案告诉查询Instances
表以获取重复事件,我做了,但我无法弄清楚如何从此查询中获取事件名称,因为它们位于{{1} }表。我没有找到如何做到这一点的例子。我尝试通过将事件与实例相结合来创建自定义查询,但我不知道是否甚至可以像这样查询Calendar表(绕过API)。
有人知道如何在给定的开始和结束日期简单地获取所有Events
,Event.TITLE
吗?
答案 0 :(得分:1)
好的,我可能需要睡个好觉......
Instances
表的列CalendarContract.Instances.TITLE
保留了标题,即使它不在Instances
表中。查询命中某些View
而不是Instances
表。我发誓我试过这种方式而无法在intellisense中找到TITLE
列。
这里有完整的解决方案可供任何可能会觉得有用的人使用:
DateTime startDate = DateTime.now().withTime(0, 0, 0, 0);
DateTime endDate = startDate.plusDays(7);
Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI
.buildUpon();
ContentUris.appendId(eventsUriBuilder, startDate.getMillis());
ContentUris.appendId(eventsUriBuilder, endDate.getMillis());
Uri eventsUri = eventsUriBuilder.build();
Cursor cursor = null;
cursor = getContentResolver().query(
eventsUri,
new String[] {CalendarContract.Instances.DTSTART, CalendarContract.Instances.TITLE},
CalendarContract.Instances.DTSTART + " >= " + startDate.getMillis() + " and " + CalendarContract.Instances.DTSTART + " <= " + endDate.getMillis() + " and " + CalendarContract.Instances.VISIBLE + " = 1",
null,
CalendarContract.Instances.DTSTART + " ASC");
此代码正在使用Joda time library
答案 1 :(得分:0)
DateTime startDate = DateTime.now()。withTime(0,0,0,0); DateTime endDate = startDate.plusDays(360);
Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI
.buildUpon();
ContentUris.appendId(eventsUriBuilder,startOfDay.getTimeInMillis());
ContentUris.appendId(eventsUriBuilder, endDate.getMillis());
Uri eventsUri = eventsUriBuilder.build();
Cursor cursor = null;
cursor = context.getContentResolver().query(
eventsUri,
new String[]{CalendarContract.Instances.CALENDAR_ID, CalendarContract.Instances.TITLE, CalendarContract.Instances.DESCRIPTION, CalendarContract.Instances.BEGIN, CalendarContract.Instances.END, CalendarContract.Instances.EVENT_LOCATION, CalendarContract.Instances.ORIGINAL_ID, CalendarContract.Instances.ORGANIZER, CalendarContract.Instances.OWNER_ACCOUNT},
CalendarContract.Instances.BEGIN + " >= " + .getTimeInMillis()+ " and "+CalendarContract.Instances.BEGIN + " <= " + endDate.getMillis()+" and " + CalendarContract.Instances.VISIBLE + " = 0" +" or "+CalendarContract.Instances.VISIBLE + " = 1",
null,
CalendarContract.Instances.BEGIN + " ASC");