Search code examples
androidcalendarcontent-values

Default calendar id for android


I am trying to add event automatically but I need calendar id for this If I add event to calendar with id = 1 sometimes it does not exist or does not depend to event I add

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

id = -1 does not work

how can I add event to default calendar. what id should I use?


Solution

  • 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;}
    

    You can try use this code for get a calendar id.

    But check all guide - how add event to calendar.

    https://stackoverflow.com/a/9749833/7046743