How to Add recurring events programmatically?
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"开发者_C百科);
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);
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);
}
}
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!
I would guess you should set DTEND and DURATION to some valid value, since "DTEND and DURATION cannot both be null for an event."
Add Recurring Event in Android Calendar programatically:
Calendar calStart = Calendar.getInstance();
Calendar calEnd = Calendar.getInstance();
calEnd.add(Calendar.HOUR_OF_DAY, 2);
ContentResolver cr = mCtx.getContentResolver();
ContentValues values = new ContentValues();
TimeZone timeZone = TimeZone.getDefault();
values.put(CalendarContract.Events.DTSTART, calStart.getTimeInMillis());
values.put(CalendarContract.Events.DTEND, calEnd.getTimeInMillis());
values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.getID());
values.put(CalendarContract.Events.TITLE, "Event Title");
values.put(CalendarContract.Events.DESCRIPTION, "Event Description");
values.put(CalendarContract.Events.CALENDAR_ID,Long.parseLong("Your_Calendar_Id"));
if (Your_Event_Is_Daily) {
values.put("rrule", "FREQ=DAILY");
} else if (Your_Event_Is_Weekly) {
values.put("rrule", "FREQ=WEEKLY");
} else if (Your_Event_Is_Monthly) {
values.put("rrule", "FREQ=MONTHLY");
} else if (Your_Event_Is_Yearly) {
values.put("rrule", "FREQ=YEARLY");
}
Uri uri1 = cr.insert(CalendarContract.Events.CONTENT_URI, values);
String eventID = uri1.getLastPathSegment();
Log.i("Event Id", eventID);
Here eventID is Event Reference Id. If you need to update or delete event than eventID will be use.
Done
精彩评论