Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add recurring events programmatically?

Tags:

android

I am developing an application for adding events to calendar. I am using following code to insert recurring event but it force closes the application with an error:

"java.lang.IllegalArgumentException: DTEND and DURATION cannot both be null for an event."

code:

ContentValues event = new ContentValues();
event.put("calendar_id", 1);
event.put("title", "Event Title");
event.put("description", "Event Desc");
event.put("eventLocation", "Event Location");
event.put("dtstart", Long.parseLong("1315432844000"));
event.put("rrule", "FREQ=WEEKLY;WKST=SU;BYDAY=WE");
event.put("allDay", 1);   // 0 for false, 1 for true
event.put("eventStatus", 1);
event.put("hasAlarm", 1); // 0 for false, 1 for true
Uri url = getContentResolver().insert(eventsUri, event);
like image 608
najhi ullah Avatar asked Sep 10 '11 18:09

najhi ullah


People also ask

How do I create a recurring event in squarespace?

Creating recurring events in Squarespace is easy. Simply go to the Events section of your website, and click on the “Add Event” button. From there, you'll be able to select “Recurring” from the event type dropdown menu. Once you've done that, you can fill out the rest of the event details as usual.


2 Answers

this is my corrected code..working fine :)

public class mainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Uri eventsUri;
        if (android.os.Build.VERSION.SDK_INT <= 7) {

            eventsUri = Uri.parse("content://calendar/events");
        } else {

            eventsUri = Uri.parse("content://com.android.calendar/events");
        }

        Calendar cal = Calendar.getInstance();  
        ContentValues event = new ContentValues();
        event.put("calendar_id", 1);
        event.put("title", "Event Title");
        event.put("description", "Event Desc");
        event.put("eventLocation", "Event Location");
        event.put("dtstart",cal.getTimeInMillis());
        event.put("rrule", "FREQ=WEEKLY;WKST=SU;BYDAY=WE");
        event.put("allDay", 1);   // 0 for false, 1 for true
        event.put("eventStatus", 1);
        event.put("hasAlarm", 1); // 0 for false, 1 for true
        event.put("duration","P3600S");
        Uri url = getContentResolver().insert(eventsUri, event);
    }
}
like image 168
najhi ullah Avatar answered Sep 30 '22 18:09

najhi ullah


From CalendarContract.Events ...

Insert

When inserting a new event the following fields must be included:

dtstart

dtend if the event is non-recurring

duration if the event is recurring

rrule or rdate if the event is recurring

eventTimezone

a calendar_id


So for a recurring event you must have dtstart,duration,rrule/rdate,eventTimezone,calendar_id.

So in your case

remove dtend!

like image 29
oikonomopo Avatar answered Sep 30 '22 16:09

oikonomopo