I have a strange issue when i create a calendar event programmatically its always noted as Birthday Calendar (type) i don\'t have any clue why its noted like that.
M
Some devices uses calendar id = 1 for birthdays but generally not. So to get the correct calendar id for particular device (corresponding to the email id configured with calendar app), use below code:
private int getCalendarId(Context context){
Cursor cursor = null;
ContentResolver contentResolver = context.getContentResolver();
Uri calendars = CalendarContract.Calendars.CONTENT_URI;
String[] EVENT_PROJECTION = new String[] {
CalendarContract.Calendars._ID, // 0
CalendarContract.Calendars.ACCOUNT_NAME, // 1
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, // 2
CalendarContract.Calendars.OWNER_ACCOUNT, // 3
CalendarContract.Calendars.IS_PRIMARY // 4
};
int PROJECTION_ID_INDEX = 0;
int PROJECTION_ACCOUNT_NAME_INDEX = 1;
int PROJECTION_DISPLAY_NAME_INDEX = 2;
int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
int PROJECTION_VISIBLE = 4;
cursor = contentResolver.query(calendars, EVENT_PROJECTION, null, null, null);
if (cursor.moveToFirst()) {
String calName;
long calId = 0;
String visible;
do {
calName = cursor.getString(PROJECTION_DISPLAY_NAME_INDEX);
calId = cursor.getLong(PROJECTION_ID_INDEX);
visible = cursor.getString(PROJECTION_VISIBLE);
if(visible.equals("1")){
return (int)calId;
}
Log.e("Calendar Id : ", "" + calId + " : " + calName + " : " + visible);
} while (cursor.moveToNext());
return (int)calId;
}
return 1;
}
Point to note : IS_PRIMARY_COLOUM which returns 1 in case of email id and not for bithdays and holidays.
Please refer: https://developer.android.com/reference/android/provider/CalendarContract.CalendarColumns.html#IS_PRIMARY for details.
The question answered below is old please refer to Pkosta's answer which provides more accurate answer ...
You have to put CalendarId value as 3 instead of 1 which is default birthday calendar. e.g.
values.Put(CalendarContract.Events.InterfaceConsts.CalendarId, 1);
change it to
values.Put(CalendarContract.Events.InterfaceConsts.CalendarId, 3);
It solved the same issue for me.
Pkosta is pointing in the right direction.
You have to take the first calendar that is both VISIBLE
and IS_PRIMARY
.
long calId = 0;
String selection = CalendarContract.Calendars.VISIBLE + " = 1 AND "
+ CalendarContract.Calendars.IS_PRIMARY + " = 1";
Uri calendarUri = CalendarContract.Calendars.CONTENT_URI;
Cursor cur = cr.query(calendarUri, null, selection, null, null);
if (cur != null && cur.moveToFirst()) {
Get the field values
calID = cur.getLong(CalendarContract.Calendars._ID);
}
if (cur != null) {
cur.close();
}
return calId;