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.
l
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;
}
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
}
This code worked for me
private fun calendarId(): Long? {
val uri = CalendarContract.Calendars.CONTENT_URI
var cursor: Cursor?= null
//Submit calendar query
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
cursor = contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null)
} else{
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.WRITE_CALENDAR), PERMISSION_REQUEST_WRITE_CALENDAR) }
if (cursor != null) {
Log.d("MainActivity", "Cursor is present")
if(cursor.moveToFirst()) {
//Get required fields
val calID = cursor.getLong(CAL_ID)
val calName = cursor.getLong(CAL_NAME)
Log.d("MainActivity", "Calendar ID: $calID, Calendar Name: $calName")
cursor.close()
return calID
}
}
//return null
return null
}
There's a IS_PRIMARY column in CalendarContract.CalendarColumns
. You query with selection:
CalendarContract.CalendarColumns.IS_PRIMARY + "=1"
However, this is since SDK 17
Some latest versions have issue, there is different visible calendar list so below is the code to select PRIMARY calendar and in older devices this query return 0 record, so used second one if 1st one return 0 records.
Cursor calCursor = mContext.getContentResolver().query(CalendarContract.Calendars.CONTENT_URI, projection, CalendarContract.Calendars.VISIBLE + " = 1 AND " + CalendarContract.Calendars.IS_PRIMARY + "=1", null, CalendarContract.Calendars._ID + " ASC");
if(calCursor.getCount() <= 0){
calCursor = mContext.getContentResolver().query(CalendarContract.Calendars.CONTENT_URI, projection, CalendarContract.Calendars.VISIBLE + " = 1", null, CalendarContract.Calendars._ID + " ASC");
}