Sajith Sajan
Sajith Sajan

Reputation: 137

How to get the default CALENDAR_ID in Android?

I am trying to add Events in my app using a calender.According to CalenderContract,I need to provide a constant ID each time I add an event to the calender.I don't know how to do that.

I tried using calender_ID =1 which worked on some devices and calender_ID = 3 which also worked on some devices.

I think there would be some default ID which can be used to make this work properly.

Can anyone please tell me how this can be done?

Thanks in Advance.

Upvotes: 3

Views: 1834

Answers (1)

chandrakant sharma
chandrakant sharma

Reputation: 1334

you can get the calender id by this following code :

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

So you can get the calender Id at runtime and use it . you don't need hardcoded 1 or 3 column Id.

Upvotes: 1

Related Questions