2

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

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

id = -1不工作

如何将事件添加到默认日历。我id应该用什么?

4

2 回答 2

2
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

于 2017-06-30T13:26:00.003 回答
1
int calenderId=-1;
        String calenderEmaillAddress="xxx@gmail.com";
        String[] projection = new String[]{
                CalendarContract.Calendars._ID,
                CalendarContract.Calendars.ACCOUNT_NAME};
        ContentResolver cr = activity.getContentResolver();
        Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), projection,
                CalendarContract.Calendars.ACCOUNT_NAME + "=? and (" +
                        CalendarContract.Calendars.NAME + "=? or " +
                        CalendarContract.Calendars.CALENDAR_DISPLAY_NAME + "=?)",
                new String[]{calenderEmaillAddress, calenderEmaillAddress,
                        calenderEmaillAddress}, null);

        if (cursor.moveToFirst()) {

            if (cursor.getString(1).equals(calenderEmaillAddress))
                 calenderId=cursor.getInt(0); //youre calender id to be insered in above your code


        }
于 2017-07-07T16:27:46.640 回答