如何以编程方式添加重复事件?

时间:2011-09-10 18:16:24

标签: android

我正在开发一个向日历添加事件的应用程序。我使用以下代码来插入重复事件,但强制关闭应用程序时出错:

  

“java.lang.IllegalArgumentException:对于某个事件,DTEND和DURATION都不能为空。”

代码:

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

4 个答案:

答案 0 :(得分:6)

这是我更正后的代码。正常工作:)

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

答案 1 :(得分:2)

来自CalendarContract.Events ...

插入

插入新事件时,必须包含以下字段:

  

DTSTART

     

如果事件是非经常性的,则为dtend

     

如果事件重复发生的持续时间

     

如果事件重复发生则为rrule或rdate

     

eventTimezone

     

calendar_id


  

因此,对于经常发生的事件,您必须拥有   DTSTART,持续时间,RRULE / RDATE,eventTimezone,CALENDAR_ID。

所以在你的情况下

  

删除dtend!

答案 2 :(得分:0)

我猜你应该将DTEND和DURATION设置为某个有效值,因为“对于某个事件,DTEND和DURATION都不能为空。”

答案 3 :(得分:0)

以编程方式在Android 日历中添加重复活动

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

此处 eventID 活动参考ID 。如果您需要使用更新删除事件而不是 eventID

完成