2

我正在尝试使用日历在我的应用程序中添加事件。根据CalenderContract,每次将事件添加到日历时,我都需要提供一个常量 ID。我不知道该怎么做。

我尝试使用calender_ID =1which 适用于某些设备calender_ID = 3,也适用于某些设备。

我认为会有一些默认 ID 可用于使其正常工作。

谁能告诉我如何做到这一点?

提前致谢。

4

1 回答 1

1

您可以通过以下代码获取日历 ID:

    String projection[] = {"_id", "calendar_displayName"};
    Uri calendars;
    calendars = Uri.parse("content://com.android.calendar/calendars");

    ContentResolver contentResolver = c.getContentResolver();
    Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);

    if (managedCursor.moveToFirst()){
        m_calendars = new MyCalendar[managedCursor.getCount()];
        String calName;
        String calID;
        int cont= 0;
        int nameCol = managedCursor.getColumnIndex(projection[1]);
        int idCol = managedCursor.getColumnIndex(projection[0]);
        do {
            calName = managedCursor.getString(nameCol);
            calID = managedCursor.getString(idCol);
            m_calendars[cont] = new MyCalendar(calName, calID);
            cont++;
        } while(managedCursor.moveToNext());
        managedCursor.close();
    }

因此您可以在运行时获取日历 ID 并使用它。您不需要硬编码的 1 或 3 列 ID。

于 2017-09-25T09:30:10.587 回答