我正在尝试自动添加事件,但我需要此事件的日历ID
如果用ID=1将事件添加到日历中,有时它不存在或不依赖于i添加的事件

    ContentValues cv = new ContentValues();
    cv.put("calendar_id", 1);

id = -1不工作
如何将事件添加到默认日历。我应该用什么?

最佳答案

public MyCalendar [] getCalendar(Context c) {

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

您可以尝试使用此代码获取日历ID。
但请查看所有指南-如何将事件添加到日历。
https://stackoverflow.com/a/9749833/7046743

10-06 15:01