1

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();
    }
}
4

1 に答える 1

0

私の間違い。コード内:

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値を設定できません。

于 2012-02-21T10:54:41.893 に答える