How can I add event to the calendar automatically?

If you use Intents to communicate with the calendar provider, then, as you've noticed, the calendar app asks for confirmation from the user. This is because using Intents means you don't need to request any particular permissions.

By the way, Google recommends using Intents for interacting with the calendar.

However, if you do want automatic insertion of events, your app must have the WRITE_CALENDAR permission in the manifest. Instead of using Intents, you use a ContentResolver. You can see example code at https://developer.android.com/guide/topics/providers/calendar-provider.html in the section Adding Events.


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

I have used Following code to add event in my own created calendar

public void SyncEvent(long id, int meeting_id, String EventName,
   String Stime, String Etime, String Description) {

 Calendar cal = Calendar.getInstance();
 cal.setTimeZone(TimeZone.getTimeZone("GMT-1"));
 Date dt = null;
 Date dt1 = null;
 try {
  dt = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(Stime);
  dt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(Etime);

  Calendar beginTime = Calendar.getInstance();
  cal.setTime(dt);

  // beginTime.set(2013, 7, 25, 7, 30);
  beginTime.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
  cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY),
  cal.get(Calendar.MINUTE));

  Calendar endTime = Calendar.getInstance();
  cal.setTime(dt1);

  // endTime.set(2013, 7, 25, 14, 30);
  // endTime.set(year, month, day, hourOfDay, minute);
  endTime.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
  cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY),
  cal.get(Calendar.MINUTE));

  ContentResolver cr = this.mContext.getContentResolver();
  ContentValues values = new ContentValues();

  values.put(Events.DTSTART, beginTime.getTimeInMillis());
  values.put(Events.DTEND, endTime.getTimeInMillis());
  values.put(Events.TITLE, EventName);
  values.put(Events.DESCRIPTION, Description);
  values.put(Events.CALENDAR_ID, id);
  // values.put(Events._ID, meeting_id);
  values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());

  Uri uri = cr.insert(Events.CONTENT_URI, values);
  long eventID = Long.parseLong(uri.getLastPathSegment());
 } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
}