无法以编程方式从Android日历中读取重复发生的事件

时间:2011-08-20 06:00:42

标签: android

我已经按照此链接中的教程 - http://jimblackler.net/blog/?p=151&cpage=2#comment-52767来访问内部的android日历数据库(即使它没有得到SDK的正式支持)。它适用于除重复活动之外的所有条目。光标根本不会返回任何重复发生的事件。有人可以帮助我吗?以下是我的游标声明 -

    String[] projection = new String[] { "title", "description", "dtstart", "eventLocation" };
    String selection = "(calendar_id=" + calID + ")AND " + (now - window)
            + "<dtstart AND dtstart< " + (now + (window));
    String sortorder = "dtstart ASC";

    Cursor managedCursor = getCalendarManagedCursor(projection, selection,
            "events", sortorder);

    private Cursor getCalendarManagedCursor(String[] projection,
        String selection, String path, String sort) {
    Uri calendars = Uri.parse("content://calendar/" + path);
    Cursor managedCursor = null;
    try {
        managedCursor = getContentResolver().query(calendars, projection,
                selection, null, sort);
    } catch (IllegalArgumentException e) {
        Log.w(DEBUG_TAG,
                "Failed to get provider at [" + calendars.toString() + "]");
    }

    if (managedCursor == null) {
        // try again
        calendars = Uri.parse("content://com.android.calendar/" + path);
        try {
            managedCursor = getContentResolver().query(calendars,
                    projection, selection, null, sort);
        } catch (IllegalArgumentException e) {
            Log.w(DEBUG_TAG,
                    "Failed to get provider at [" + calendars.toString()
                            + "]");
        }`

2 个答案:

答案 0 :(得分:16)

如果您需要查找定期事件,请使用Instances表。

查询它的URI是:

  • instances/when/*/* - 两次(毫秒)之间的所有实例
  • instances/whenbyday/*/* - 两次(天)之间的所有实例
  • instances/groupbyday/*/* - 与whenbyday相同,但按开始日分组

该表中的列列表是:

  • _id - 此实例的ID
  • event_id - 从
  • 创建的事件
  • begin - 开始时间(毫秒)
  • end - 结束时间(毫秒)
  • startDay - 实例开始日
  • endDay - 实例的结束日
  • startMinute - 从午夜开始的分钟(0..1440)
  • endMinute - 从午夜开始的分钟

您还可以使用EventsCalendar表中的列。

您可以在链接的同一页面上看到一个示例:http://jimblackler.net/blog/?p=151

示例:

String[] projection = new String[] {
        "title", "description", "begin", "eventLocation"
};
String selection = "calendar_id = " + calID;
String path = "instances/when/" + (now - window) + "/" + (now + window);
String sortOrder = "begin DESC";
Cursor managedCursor = getCalendarManagedCursor(
        projection, selection, path, sortorder);

修改:Google似乎开始记录日历提供商。浏览Calendar Providier | Google Developers以获取更多信息。

答案 1 :(得分:1)

我终于以下列方式完成了这项工作。记住DTSTART,DTEND,_ID不适用于Instances表,即使您可以访问它们。请改用BEGIN,END和Event_ID。请参考以下2个链接: How to get calendar events with title including recurring eventsAndroid Calendar Recurring Events Have Wrong End Date/Time

        long now = System.currentTimeMillis();

        Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI.buildUpon();
        ContentUris.appendId(eventsUriBuilder, Long.MIN_VALUE);
        ContentUris.appendId(eventsUriBuilder, Long.MAX_VALUE);

        Uri eventsUri = eventsUriBuilder.build();
        Cursor 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.EVENT_ID},
            CalendarContract.Instances.BEGIN + " >= " + now + " and " + CalendarContract.Instances.BEGIN 
                    + " <= " + (now + 2592000000L) + " and " + CalendarContract.Instances.VISIBLE + " = 1",
            null,
            CalendarContract.Instances.BEGIN + " ASC");