Не удается прочитать записи после вставки в календарь Google - PullRequest
1 голос
/ 29 декабря 2011

Не удается прочитать записи после вставки в календарь Google.

Некоторое время письмо, записи читаются.

Почему?

Если взять Context из Activity, то все работает нормально. Если Context из AndroidTestCase или взять их Application, это не сработает.

Примечание. Календарь с id = calendarId пуст.

public static void test(final Context context, final int calendarId) {
    // insert
    java.util.Calendar calendar = java.util.Calendar.getInstance();

    ContentValues contentValues = new ContentValues();

    contentValues.put("calendar_id", calendarId);
    contentValues.put("dtstart", calendar.getTime().getTime());
    contentValues.put("dtend", calendar.getTime().getTime() + 1L*60*60*1000);
    contentValues.put("title", "test_title");

    Uri uri = Uri.parse("content://com.android.calendar/events");
    context.getContentResolver.insert(uri, contentValues);

    // read
    Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
    ContentUris.appendId(builder, Long.MIN_VALUE);
    ContentUris.appendId(builder, Long.MAX_VALUE);

    Cursor cursor = context.getContentResolver.query(
        builder.build(),
        new String[]  { "event_id", "title" },
        "Calendars._id=" + calendarId,
        null,
        null);
    try {
        if (!cursor.moveToNext()) {
            // fail
            throw new IllegalStateException();
        }
    } finally {
        cursor.close();
    }
}

1 Ответ

0 голосов
/ 21 февраля 2012

Моя ошибка.В коде:

Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
ContentUris.appendId(builder, Long.MIN_VALUE);
ContentUris.appendId(builder, Long.MAX_VALUE);

Невозможно установить дату начала Long.MIN_VALUE значение.

...