Add Weekly Event to Calendar

You said it showed repeating for Thursday, but what I got was a start day of Thursday with a repeat every Tuesday. So I’m pretty sure the RRULE part is right.

I think all you have to do is set the actual start and end times with Calendar to get the right milliseconds, then user “beginTime” instead of “dtstart” and “endTime” instead of “dtend”.

@Override
public void onClick(View v) {

    // If you want the start times to show up, you have to set them
    Calendar calendar = Calendar.getInstance();

    // Here we set a start time of Tuesday the 17th, 6pm
    calendar.set(2015, Calendar.MARCH, 17, 18, 0, 0);
    calendar.setTimeZone(TimeZone.getDefault());

    long start = calendar.getTimeInMillis();
    // add three hours in milliseconds to get end time of 9pm
    long end = calendar.getTimeInMillis() + 3 * 60 * 60 * 1000;

    Intent intent = new Intent(Intent.ACTION_INSERT)
            .setData(Events.CONTENT_URI)
            .setType("vnd.android.cursor.item/event")
            .putExtra(Events.TITLE, "Tuesdays")
            .putExtra(Events.DESCRIPTION, "Tuesday Specials")
            .putExtra(Events.EVENT_LOCATION, "Lixious Bench")
            .putExtra(Events.RRULE, "FREQ=WEEKLY;BYDAY=TU;UNTIL=20150428")

            // to specify start time use "beginTime" instead of "dtstart"
            //.putExtra(Events.DTSTART, calendar.getTimeInMillis())
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, start)
            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end)

            // if you want to go from 6pm to 9pm, don't specify all day
            //.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true)
            .putExtra(CalendarContract.Events.HAS_ALARM, 1)
            .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);

    startActivity(intent);
 }

Leave a Comment