如何获取包含重复活动的标题的日历活动

时间:2014-10-20 19:06:38

标签: android calendar

我需要做一件并不复杂的事,人们可以相信。我想从手机上的日历中获取事件实例以及事件的标题。我在Stack Overflow上找到了几个答案告诉查询Instances表以获取重复事件,我做了,但我无法弄清楚如何从此查询中获取事件名称,因为它们位于{{1} }表。我没有找到如何做到这一点的例子。我尝试通过将事件与实例相结合来创建自定义查询,但我不知道是否甚至可以像这样查询Calendar表(绕过API)。

有人知道如何在给定的开始和结束日期简单地获取所有EventsEvent.TITLE吗?

2 个答案:

答案 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");