Dima Dehtiarov
Dima Dehtiarov

Reputation: 115

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?

Upvotes: 2

Views: 3146

Answers (2)

Rajesh N
Rajesh N

Reputation: 6683

int calenderId=-1;
        String calenderEmaillAddress="[email protected]";
        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


        }

Upvotes: 1

Alexander Dziadyk
Alexander Dziadyk

Reputation: 96

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

Upvotes: 2

Related Questions