How can I add event to the calendar automatically?
I post you this code that is used in one of my app in the market. It adds automatically event to the user calendar. It doesn't use Intent that requires an user action.
public void addEvent(CalendarEvent evt) {
//Log.d(Params.LOG_APP, "Insert event ["+evt+"]");
try {
Uri evtUri = ctx.getContentResolver().insert(getCalendarUri("events"), CalendarEvent.toContentValues(evt));
Log.d(Params.LOG_APP, "" + evtUri);
}
catch(Throwable t) {
//Log.e(Params.LOG_APP, "", t);
}
}
public void setContext(Context context) {
this.ctx = context;
this.baseUri = getCalendarUriBase();
}
private Uri getCalendarUri(String path) {
return Uri.parse(baseUri + "/" + path);
}
private String getCalendarUriBase() {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = ctx.getContentResolver().query(calendars, null, null, null, null);
} catch (Exception e) {
// e.printStackTrace();
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = ctx.getContentResolver().query(calendars, null, null, null, null);
} catch (Exception e) {
// e.printStackTrace();
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
Log.d(Params.LOG_APP, "URI ["+calendarUriBase+"]");
return calendarUriBase;
}
And for ICS and later
public void addEvent(CalendarEvent evt) {
ContentResolver cr = context.getContentResolver();
Uri uri = cr.insert(Events.CONTENT_URI, CalendarEvent.toICSContentValues(evt));
System.out.println("Event URI ["+uri+"]");
}
CalendarEvent is like
public static ContentValues toContentValues(CalendarEvent evt) {
ContentValues cv = new ContentValues();
cv.put("calendar_id", evt.getIdCalendar());
cv.put("title", evt.getTitle());
cv.put("description", evt.getDescr());
cv.put("eventLocation", evt.getLocation());
cv.put("dtstart", evt.getStartTime());
cv.put("dtend", evt.getEndTime());
cv.put("eventStatus", 1);
cv.put("visibility", 0);
cv.put("transparency", 0);
return cv;
}
public static ContentValues toICSContentValues(CalendarEvent evt) {
ContentValues cv = new ContentValues();
cv.put(Events.CALENDAR_ID, evt.getIdCalendar());
cv.put(Events.TITLE, evt.getTitle());
cv.put(Events.DESCRIPTION, evt.getDescr());
cv.put(Events.EVENT_LOCATION, evt.getLocation());
cv.put(Events.DTSTART, evt.getStartTime());
cv.put(Events.DTEND, evt.getEndTime());
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
cv.put(Events.EVENT_TIMEZONE, tz.getDisplayName());
/*
cv.put(Events.STATUS, 1);
cv.put(Events.VISIBLE, 0);
cv.put("transparency", 0);
return cv;
*/
return cv;
}
Update Android calendar events
Here's the solution that worked for me :
The eventID that I used was the result of the query with column CalendarContract.Events._ID
but I had to used the ID provided by CalendarContract.Instances.EVENT_ID
.
Indeed if I create a new event (the first one) it has _id = 1 and event_id = 1, and if I delete it from Android Calendar App and I create a new one with my app, it has also _id = 1 but event_id = 2.
So the update query failed if I use _id instead of event_id with withAppendedID()
.
Here's the code that worked for me. startMillis
and endMillis
are respectively your meeting start time and end time in milliseconds.
Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI
.buildUpon();
ContentUris.appendId(eventsUriBuilder, startMillis);
ContentUris.appendId(eventsUriBuilder, endMillis);
Uri eventsUri = eventsUriBuilder.build();
String[] column = {CalendarContract.Instances.EVENT_ID, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.TITLE};
Cursor cursor;
cursor = c.getContentResolver().query(eventsUri, column, CalendarContract.Events.CALENDAR_ID+"="+Constant.ID_CALENDAR, null, CalendarContract.Instances.DTSTART + " ASC");
if (cursor.moveToFirst()) {
do {
uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI,cursor.getLong(0));
} while (cursor.moveToNext());
}
cursor.close();
c.getContentResolver().delete(uri, null, null);
Creating All-Day Events Using Android Calendar Provider
DTEND
is not redundant as it specifies the end of the event. all-day doesn't mean that it's just one day long. It could span any number of days. all-day means start and end of the event have no time component. In particular, the event always begins when the day begins, no matter in which time zone you're actually located at that time. A common example is your birthday. You usually celebrate it on a specific calendar day, no matter where exactly you are.
Note that in Android's calendar database DTSTART
and DTEND
should represent midnight of that specific day in UTC.
So a better way of deriving the start and end dates would be:
TimeZone utc = TimeZone.getTimeZone("UTC");
Calendar beginTime = Calendar.getInstance(utc);
// set all values to 0
beginTime.clear();
beginTime.set(2012, 9, 14);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance(utc);
// the event ends right before the next day begins
endTime.clear();
endTime.set(2012, 9, 15);
endMillis = endTime.getTimeInMillis();
Also note that DTEND
is non-inclusive (it points to the first moment after the event). For a one day all-day event that means, DTEND
is the start of the next day.
Another way of determining the end of the event is by adding the duration of the event to DTSTART
like so:
// Add the duration of 1 day to startMillis to get the end
endMillis = startMillis + TimeUnit.DAYS.toMillis(1);
The result is the same as above.
Be aware that this is how to create non-recurring events. If you create a recurring event you must not set DTEND
but DURATION
.
How to add calendar events in Android?
how do I programmatically add an event to the user's calendar?
Which calendar?
Is there a common API they all share?
No, no more than there is a "common API they all share" for Windows calendar apps. There are some common data formats (e.g., iCalendar) and Internet protocols (e.g., CalDAV), but no common API. Some calendar apps don't even offer an API.
If there are specific calendar applications you wish to integrate with, contact their developers and determine if they offer an API. So, for example, the Calendar application from the Android open source project, that Mayra cites, offers no documented and supported APIs. Google has even explicitly told developers to not use the techniques outlined in the tutorial Mayra cites.
Another option is for you to add events to the Internet calendar in question. For example, the best way to add events to the Calendar application from the Android open source project is to add the event to the user's Google Calendar via the appropriate GData APIs.
UPDATE
Android 4.0 (API Level 14) added a CalendarContract
ContentProvider
.
Related Topics
Android Draw Route on a Mapview with Twoo Poi-S
How to Read Text File in Android
Cannot Call Getsupportfragmentmanager() from Activity
Notifydatasetchanged Not Working on Recyclerview
Broadcast Receiver Not Working in Ics If the App Is Not Started Atleast Once
Android.Os.Networkonmainthreadexception . Need to Use Async Task
Error Building Player: Commandinvokationfailure: Unable to Convert Classes into Dex Format
Progress Bar While Loading Image Using Glide
Android Navigation Drawer on Top Actionbar
Android: How Handle Message Error from the Server Using Volley
Android - Way to Appear Bordered Text on the Textview
Android: Get Facebook Friends List
The Specified Child Already Has a Parent. You Must Call Removeview() on the Child's Parent First
Android Emulator Not Receiving Push Notifications
Android Marshmallow: Changing Permissions at Run Time Crashes App
Handling Keepsynced() While on Background on Android and with Fcm
How to Fill Recyclerview with Gridlayoutmanager from Right to Left