Answer the question
In order to leave comments, you need to log in
Problem with CAlendar API?
Hello. I'm having a problem with the calendar API. The problem is: I am creating a new calendar in the CalendarContract.Calendars table using CALLER_IS_SYNC_ADAPTER. The calendar is created without problems
public void createCalendarWithId(int userId) {
Uri createUri = asSyncAdapter(CalendarContract.Calendars.CONTENT_URI);
ContentValues cv = new ContentValues();
cv.put(CalendarContract.Calendars.NAME, "Calendar " + userId);
cv.put(CalendarContract.Calendars.CALENDAR_COLOR, "");
cv.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
cv.put(CalendarContract.Calendars.OWNER_ACCOUNT, "Owner");
cv.put(CalendarContract.Calendars.ACCOUNT_NAME, mContext.getString(R.string.app_name));
cv.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
mContext.getContentResolver().insert(createUri, cv);
}
private Uri asSyncAdapter(Uri uri) {
return uri.buildUpon()
.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, mContext.getString(R.string.app_name))
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL).build();
}
public void createEventForCalendarWithId(String calendarId, long startTime, String description, String title) {
ContentValues cv = new ContentValues();
cv.put(CalendarContract.Events.DTSTART, startTime);
cv.put(CalendarContract.Events.DTEND, startTime);
cv.put(CalendarContract.Events.TITLE, title);
cv.put(CalendarContract.Events.DESCRIPTION, description);
cv.put(CalendarContract.Events.CALENDAR_ID, calendarId);
cv.put(CalendarContract.Events.EVENT_TIMEZONE, String.valueOf(TimeZone.getDefault()));
Uri insertedEvent = mContext.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, cv);
String eventId = String.valueOf(ContentUris.parseId(insertedEvent));
createReminderForEventWithId(eventId, 2);
}
public void createReminderForEventWithId(String eventId, int minutes) {
ContentValues cv = new ContentValues();
cv.put(CalendarContract.Reminders.MINUTES, minutes);
cv.put(CalendarContract.Reminders.EVENT_ID, eventId);
cv.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
mContext.getContentResolver().insert(CalendarContract.Reminders.CONTENT_URI, cv);
}
public void createEventForCalendarWithId(String calendarId, long startTime, String description, String title) {
Uri anyChanges = asSyncAdapterForEvents(CalendarContract.Events.CONTENT_URI);
ContentValues cv = new ContentValues();
cv.put(CalendarContract.Events.DTSTART, startTime);
cv.put(CalendarContract.Events.DTEND, startTime);
cv.put(CalendarContract.Events.TITLE, title);
cv.put(CalendarContract.Events.DESCRIPTION, description);
cv.put(CalendarContract.Events.CALENDAR_ID, calendarId);
cv.put(CalendarContract.Events.EVENT_TIMEZONE, String.valueOf(TimeZone.getDefault()));
Uri insertedEvent = mContext.getContentResolver().insert(anyChanges, cv);
String eventId = String.valueOf(ContentUris.parseId(insertedEvent));
createReminderForEventWithId(eventId, 2);
}
private Uri asSyncAdapterForEvents(Uri uri) {
return uri.buildUpon()
.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
.appendQueryParameter(CalendarContract.Events.ACCOUNT_NAME, mContext.getString(R.string.app_name))
.appendQueryParameter(CalendarContract.Events.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL).build();
}
public void createEventForCalendarWithId(String calendarId, long startTime, String description, String title) {
ContentValues cv = new ContentValues();
cv.put(CalendarContract.Events.ACCOUNT_NAME, mContext.getString(R.string.app_name));
cv.put(CalendarContract.Events.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
cv.put(CalendarContract.Events.DTSTART, startTime);
cv.put(CalendarContract.Events.DTEND, startTime);
cv.put(CalendarContract.Events.TITLE, title);
cv.put(CalendarContract.Events.DESCRIPTION, description);
cv.put(CalendarContract.Events.CALENDAR_ID, calendarId);
cv.put(CalendarContract.Events.EVENT_TIMEZONE, String.valueOf(TimeZone.getDefault()));
Uri insertedEvent = mContext.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, cv);
String eventId = String.valueOf(ContentUris.parseId(insertedEvent));
createReminderForEventWithId(eventId, 2);
}
Answer the question
In order to leave comments, you need to log in
Met the same problem. You are doing everything right, but you forgot to specify when creating the calendar, the CalendarContract.Calendars.CALENDAR_DISPLAY_NAME
Calendar field, when opening an event, shows which calendar it belongs to, for this it takes calendar_displayName
, but if it does not find it, it simply falls. Add it and it will work
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question