Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the default calendar id in Android

I have the following code for adding event to calendar.

The problem is that I don't know how to retrieve the default calendar id.

long calID = 3;
long startMillis = 0; 
long endMillis = 0;     
Calendar beginTime = Calendar.getInstance();
beginTime.set(2013, 3, 23, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2013, 3, 24, 8, 45);
endMillis = endTime.getTimeInMillis();

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "My Test");
values.put(Events.DESCRIPTION, "My Calendar Test");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "Israel/tel-aviv");
Uri uri = cr.insert(Events.CONTENT_URI, values);

The line: long calID = 3; is the calendar id

Is it possible to get the default calendar id from Android, or do I need to show a list of calendars and have the user pick one?

If not possible, how to show the list of calendar accounts?

like image 572
Gold Avatar asked Apr 26 '13 17:04

Gold


People also ask

What is the default calendar on Android?

Unfortunately, the default calendar app that comes with your phone hasn't really evolved over the years. For Android users, this is basically your Google Calendar – because it syncs to your account.

What is calendar ID in Google Calendar API?

calendarId is the email address of your calendar. If you're just using your own, then use the string "primary". eventId is the ID of the event that you want to modify.


2 Answers

To get the list of calendars, you need to query the ContentResolver like this:

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;

}
like image 128
Dmitri Avatar answered Oct 12 '22 05:10

Dmitri


I used following code to get the calendar ID.

private fun getCalendarId(context: Context) : Long? {
    val projection = arrayOf(Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME)

    var calCursor = context.contentResolver.query(
        Calendars.CONTENT_URI,
        projection,
        Calendars.VISIBLE + " = 1 AND " + Calendars.IS_PRIMARY + "=1",
        null,
        Calendars._ID + " ASC"
    )

    if (calCursor != null && calCursor.count <= 0) {
        calCursor = context.contentResolver.query(
            Calendars.CONTENT_URI,
            projection,
            Calendars.VISIBLE + " = 1",
            null,
            Calendars._ID + " ASC"
        )
    }

    if (calCursor != null) {
        if (calCursor.moveToFirst()) {
            val calName: String
            val calID: String
            val nameCol = calCursor.getColumnIndex(projection[1])
            val idCol = calCursor.getColumnIndex(projection[0])

            calName = calCursor.getString(nameCol)
            calID = calCursor.getString(idCol)

            Log.d("Calendar name = $calName Calendar ID = $calID")

            calCursor.close()
            return calID.toLong()
        }
    }
    return null
}

So do not pass 0, 1 or 3 as Calendar ID. Use above function instead.

Also, check if Calendar ID is null and do not perform operations with it if it is null.

        val calendarId = getCalendarId(context)
        if (calendarId != null) {
        //Call operations e.g.: Insert operation
        }
like image 8
Malwinder Singh Avatar answered Oct 12 '22 06:10

Malwinder Singh