I'um using CalendarContract to insert a custom local (not synced) calendar and events. It's working on all devices, but not on Sony Xperia (4.4.4). Code for inserting a new local calendar
final ContentValues cv = new ContentValues();
cv.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
cv.put(CalendarContract.Calendars.ACCOUNT_NAME, calendarName);
cv.put(CalendarContract.Calendars.NAME, calendarName);
cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, calendarDisplayName);
cv.put(CalendarContract.Calendars.CALENDAR_COLOR, 0xeeff0000);
cv.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
cv.put(CalendarContract.Calendars.VISIBLE, 1);
//cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
Uri calUri = CalendarContract.Calendars.CONTENT_URI
.buildUpon()
.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, calendarName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL)
.build();
//context.getContentResolver().insert(calUri, cv);
try {
Uri result = context.getContentResolver().insert(calUri, cv);
Long calId = Long.parseLong(result.getLastPathSegment());
Log.d(MainActivity.DEBUG_KEY, "==> add Calendar OK: " + calId);
return calId;
} catch (Exception exception) {
Log.d(MainActivity.DEBUG_KEY, "==> add Calendar FAILED: " + exception.getMessage());
}
The calendarName ist the app package name. Code for inserting Events:
long eventID = 0;
TimeZone timeZone = TimeZone.getDefault();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startDate.getTime());
values.put(CalendarContract.Events.DTEND, endDate.getTime());
values.put(CalendarContract.Events.ALL_DAY, allDay ? 1 : 0);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
try {
Uri uri = context.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, values);
eventID = Long.parseLong(uri.getLastPathSegment());
Log.d(DEBUG_KEY, "EVENT ADDED: " + eventID);
} catch (Exception exception) {
Log.d(DEBUG_KEY, "EVENT-ADDED FAILED: " + exception.getMessage());
}
There are no errors oder exceptions. The debug console display the correct inserted calendar and event id. On all devices i can see, display and edit the events in the calendar app.
On Sony Xperia devices the events are showing in the calendar app. But tapping on the event for showing/editing, the calendar app crahes with the follwing error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.calendar/com.android.calendar.EventInfoActivity}: java.lang.NullPointerException
...
Reading a NULL string not supported here.
Related
I have an issue related to calendar. I am using ContentResolver to add event to calendar. It works pretty well on any devices accept of samsung devices. When I am trying to add event to original samsung calendar nothing happens. But when I downloaded google calendar event added properly to it. Is samsung calendar is regular calendar and how can i use it like primary calendar? Thanks in advance for any help.
private long insertEventTask(Activity activity, CalEvent calEvent) {
ContentResolver contentResolver = activity.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, calEvent.getStartMillis());
values.put(CalendarContract.Events.DTEND, calEvent.getEndMillis());
values.put(CalendarContract.Events.TITLE, calEvent.getTitle());
values.put(CalendarContract.Events.DESCRIPTION, calEvent.getDescription());
values.put(CalendarContract.Events.CALENDAR_ID, calEvent.getCalId());
values.put(CalendarContract.Events.EVENT_TIMEZONE, calEvent.getTimezone());
#SuppressLint("MissingPermission")
Uri uri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, values);
Log.e("xxx", "uri is " + uri);
long eventId = Long.parseLong(uri.getLastPathSegment());
return eventId;
}
I'm adding events to a local calendar, this works fine with API < 24 (KitKat, Lollipop, Marshmallow), but I get issues with Google Calendar not able to open the events from my local calendar and returning "The requested event was not found"
Events are listed into Google Calendar, but cannot be opened, edited or deleted
Code to create the local calendar:
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "My Calendar");
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.NAME, "My Calendar");
values.put(CalendarContract.Calendars.CALENDAR_COLOR, BLACK_COLOR);
Uri updateUri = CalendarContract.Calendars.CONTENT_URI;
updateUri.buildUpon()
.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER, "false")
.build();
Uri uri = cr.insert(updateUri, values);
Code to create an event into the calendar:
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, start);
values.put(CalendarContract.Events.DTEND, end);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, description);
values.put(CalendarContract.Events.CALENDAR_ID, calID); // CalID = My Calendar Id
values.put(CalendarContract.Events.EVENT_TIMEZONE, "Australia/Sydney");
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
You need to provide an account name CalendarContract.Calendars.ACCOUNT_NAME and an account type in the ContentValues when you are creating the calendar
I am developing an android app that programmatically adds some events to the user's default calendar using a ContentResolver.
I also set the color of the events based on the type of the event (in my case: green for holidays, red for workdays). This works fine but after a few minutes all the custom coloring turns to blue (default google calendar event color). I use a Nexus 5X for debugging. Any idea how to solve this?
Screenshots:
Before
After
Code:
// add work day event
if (DateFrom > today.getTimeInMillis() && WorkType == '0') {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, DateFrom);
values.put(CalendarContract.Events.DTEND, DateTo);
values.put(CalendarContract.Events.TITLE, Team + " (Id: " + Id + ")");
values.put(CalendarContract.Events.DESCRIPTION, "Work Day.");
values.put(CalendarContract.Events.CALENDAR_ID, DEFAULT_CALENDAR_ID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
values.put(CalendarContract.Events.EVENT_COLOR, Color.RED);
Uri eventUri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
}
// add day off event
if (DateFrom > today.getTimeInMillis() && WorkType == '2' && IsApproved != null && IsApproved.equals("true")) {
ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.ALL_DAY, 1);
values.put(CalendarContract.Events.DTSTART, DateFrom + 3600000);
values.put(CalendarContract.Events.DURATION, "P23H");
values.put(CalendarContract.Events.TITLE, "Day Off!" + " (Id: " + Id + ")");
values.put(CalendarContract.Events.DESCRIPTION, "Day Off");
values.put(CalendarContract.Events.EVENT_COLOR, Color.GREEN);
values.put(CalendarContract.Events.CALENDAR_ID, DEFAULT_CALENDAR_ID);
values.put(CalendarContract.Events.HAS_ALARM, false);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
Uri eventUri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
}
Documentation says:
EVENT_COLOR
A secondary color for the individual event. This should only be updated by the sync adapter for a given account.
UPD:
The right way is to use EVENT_COLOR_KEY that you can select from CalendarContract.Colors. See documentation
I am using below code to add events to calendar on android
public void addEvent(String datetime) {
String eventdate;
{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
final Calendar cal = Calendar.getInstance();
try {
cal.setTime(formatter.parse(datetime));
eventdate = cal.get(Calendar.YEAR)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);
//Log.e("Event date ", eventdate);
} catch (Exception e) {
Log.e("Catch ", "",e);
}
ContentValues event = new ContentValues();
event.put("calendar_id", 3);
event.put("_id", eventid);
event.put("title", mytitle);
event.put("description", mydescription);
event.put("eventTimezone", TimeZone.getDefault().getID());
event.put("dtstart", cal.getTimeInMillis());
event.put("dtend", cal.getTimeInMillis()+60*60*1000);
event.put("hasAlarm", 1); // 0 for false, 1 for true
String eventUriString = "content://com.android.calendar/events";
Uri eventUri = getApplicationContext()
.getContentResolver()
.insert(Uri.parse(eventUriString), event);
System.out.println("event"+eventUri);
}
the following code adds event to my HTC phone running android lollipop but it returns null on Phones like Micromax and Samsung running android Jellybean. What can be the reason for this behavior? Do I need to turn anything on from settings?
try using the constants provided by the Events class.
ContentResolver cr = yourContext.getContentResolver();
ContentValues event = new ContentValues();
event.put(Events.DTSTART, cal.getTimeInMillis());
event.put(Events.DTEND, cal.getTimeInMillis() + 60 * 60 * 1000);
event.put(Events.TITLE, mytitle);
event.put(Events.DESCRIPTION, mydescription);
event.put(Events.CALENDAR_ID, calID);
event.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
...
Uri uri = cr.insert(Events.CONTENT_URI, event);
The Event id of this inserted event can be get from this method
long eventID = Long.parseLong(uri.getLastPathSegment());
Check the title Adding Events in Calendar Provider
http://developer.android.com/guide/topics/providers/calendar-provider.html
If you want to insert an _id to the Event, you should check if it is already there
Uri event = ContentUris.withAppendedId(Events.CONTENT_URI, _id);
Cursor cursor = managedQuery(event, null, null, null);
if (cursor.getCount() == 1) {
//the event exists.. so may be you want to update it
} else {
// you can insert your id
}
With this code my team mate was adding calendar events. This works without problems. The calendar events are shown in the google calendar.
TimeZone timeZone = TimeZone.getDefault();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
values.put(CalendarContract.Events.DTSTART, startInMillis);
values.put(CalendarContract.Events.DTEND, endInMillis);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.EVENT_LOCATION, "Raum: " + location);
values.put(CalendarContract.Events.DESCRIPTION,
"Dauer der Veranstaltung: " + duration + " min" + "---|---Dozent: "
+ organizer.trim() + "---|---Course added by UNIDATE---|");
values.put(CalendarContract.Events.CALENDAR_ID, 1);
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
ContentUris.withAppendedId(uri, id);
But now I want to delete these events by their id. I've used this code (among others):
long selectedEventId = 1;
String[] selArgs = new String[]{Long.toString(selectedEventId)};
int deleted = getContentResolver().delete(CalendarContract.Events.CONTENT_URI, Events._ID + " =? ", selArgs);
This doesn't work. I also tried to add
values.put(CalendarContract.Events._ID, id);
A simple solution will be to first clear the calendar events, and then add the relevant events.
Clearing the calendar events can be done by calling this:
context.getContentResolver().delete(CalendarContract.Events.CONTENT_URI, CalendarContract.Events.CALENDAR_ID + " = ?", new String[]{String.valueOf(calendarId)});