Here is my Code and I want to know what is value for "CalendarContract.Events.CALENDAR_ID"? This code is working on Pre Android N Os but in Nougat if I put "1" as value it creates an event in specific Field(Reminder, Birthday, Holiday etc) and it varies user to user. So if I pass "5" as value in some nougat device then I might get proper output in one device but in another phone that event might gets created with reminder or holiday and gets deleted automatically with in few seconds.
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.CALENDAR_ID, 1);
values.put(CalendarContract.Events.EVENT_TIMEZONE,
Calendar.getInstance().getTimeZone().getID());
Uri uri=cr.insert(CalendarContract.Events.CONTENT_URI,
values);
long eventId = Long.parseLong(uri.getLastPathSegment());
ID
depends on quantity of calendars added by device manufacturer. But you can use only user calendar. If you add event, for example, to the holidays calendar it will be removed on sync. Also user can have several Google users added to phone, so in this case the better option is to ask user what calendar he want to use:
public class CalendarItem{
private int id;
private String name;
... getters and setters ...
}
public List<CalendarItem> getCalendars(Context ctx){
if (ActivityCompat.checkSelfPermission(ctx, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
return null;
}
List<CalendarItem> result = new ArrayList();
final String[] EVENT_PROJECTION = new String[]{
CalendarContract.Calendars._ID,
CalendarContract.Calendars.NAME
};
final ContentResolver cr = ctx.getContentResolver();
final Uri uri = CalendarContract.Calendars.CONTENT_URI;
Cursor cur = cr.query(uri, EVENT_PROJECTION, null, null, null);
while (cur.moveToNext()) {
CalendarItem item = new CalendarItem();
item.setId(cur.getInt(cur.getColumnIndex(CalendarContract.Calendars._ID)));
item.setName(cur.getString(cur.getColumnIndex(CalendarContract.Calendars.NAME)));
if (isEmailValid(item.getName)){
result.add(item);
}
}
cur.close();
return result;
}
//user calendar name == email
private boolean isEmailValid(String email) {
if (TextUtils.isEmpty(email)) return false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
Setting value for CALENDAR_ID as 3 for Android M and above devices and value as 1 for versions lower than M works in most of the devices.
But it creates problem for few devcives. Like currently I am facing problem for Lenove devices with android verion 6.0 which takes the value 1 for proper event creation.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
values.put(Events.CALENDAR_ID, 3);
}else{
values.put(Events.CALENDAR_ID, 1);
}