How to Add Calendar Events in Android

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.

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;
}

How to add calendar events to default calendar, silently without Intent, in android 4?

Here is a working example of what i eventually made it:

ContentResolver cr = ctx.getContentResolver();
ContentValues values = new ContentValues();

values.put(CalendarContract.Events.DTSTART, dtstart);
values.put(CalendarContract.Events.TITLE, title);
values.put(CalendarContract.Events.DESCRIPTION, comment);

TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());

// Default calendar
values.put(CalendarContract.Events.CALENDAR_ID, 1);

values.put(CalendarContract.Events.RRULE, "FREQ=DAILY;UNTIL="
+ dtUntill);
// Set Period for 1 Hour
values.put(CalendarContract.Events.DURATION, "+P1H");

values.put(CalendarContract.Events.HAS_ALARM, 1);

// Insert event to calendar
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

where dtuntil is

SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
Calendar dt = Calendar.getInstance();

// Where untilDate is a date instance of your choice, for example 30/01/2012
dt.setTime(untilDate);

// If you want the event until 30/01/2012, you must add one day from our day because UNTIL in RRule sets events before the last day
dt.add(Calendar.DATE, 1);
String dtUntill = yyyyMMdd.format(dt.getTime());

Ref: Recurrence Rule

Can I add event to calendar without letting user know in android?

You can refer below code to add events in calender.

String GLOBAL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
String gameDate="2015-07-12 01:10:00";
Date startDate = DateConstants.getDateFromString(
gameDate,GLOBAL_DATE_FORMAT);
long endDate = startDate.getTime()
+ (PTGConstantMethod.validateInteger(game
.getGame_duration()) * 1000);

ContentValues event = new ContentValues();
event.put("calendar_id", 1);
event.put("title", "Game#" + game.getId());
event.put("description", game.getLocation());
event.put("eventLocation", game.getLocation());
event.put("eventTimezone", TimeZone.getDefault().getID());
event.put("dtstart", startDate.getTime());
event.put("dtend", endDate);

event.put("allDay", 0); // 0 for false, 1 for true
event.put("eventStatus", 1);
event.put("hasAlarm", 1); // 0 for false, 1 for true

String eventUriString = "content://com.android.calendar/events";
Uri eventUri = context.getApplicationContext()
.getContentResolver()
.insert(Uri.parse(eventUriString), event);
long eventID = Long.parseLong(eventUri.getLastPathSegment());

// if reminder need to set


int minutes=120;


// add reminder for the event
ContentValues reminders = new ContentValues();
reminders.put("event_id", eventID);
reminders.put("method", "1");
reminders.put("minutes", minutes);

String reminderUriString = "content://com.android.calendar/reminders";
context.getApplicationContext().getContentResolver()
.insert(Uri.parse(reminderUriString), reminders);

How can a Web App add a Calendar Event to an Android Phone?

If all you want is to prompt a user to add a single event to a google calendar there is a link template one can use. This tool will generate a link for you. I couldn't easily find a google help post that describes the template. https://decomaan.github.io/google-calendar-link-generator/

If you looking for a more universal cross browser / device solution then note: how an application responds an ics link is up to user and the app, and depends how the user comes to the ics link.

  1. If it's attached to an email, the default behaviour one would expect would be to suggest import into calendar chosen by user. Calendars will not 'sync' it is a one off import.
  2. If it's a link (ics url) then default behaviour one might expect would be to 'subscribe' to the calendar. BUT users can override that EG on my PC i have mine setup to open .ics files in notepad. Subscribed calendars will update at intervals determined by the receiving app and is one-directional - ie not strictly a 'sync' - just an update (like an rss feed)

How to add event in Android calendar, repeated every 3 days?

finally I found the solution.

values.put(Events.DTSTART, startMillis);
values.put(Events.DURATION, "P1800S");
values.put(Events.TITLE, "MyTestNew");
values.put(Events.DESCRIPTION, "Group workout");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "India");
values.put(Events.RRULE, "FREQ=DAILY;INTERVAL=3");
uri = cr.insert(Events.CONTENT_URI, values);


Related Topics



Leave a reply



Submit