如何以编程方式添加周期性事件?周期性、事件、方式

2023-09-05 10:23:19 作者:独恋ヽ花尽散

我开发添加事件日历应用程序。我用下面的code插入重复事件,但它的力量关闭与一个错误的应用程序java.lang.IllegalArgumentException异常:DTEND和持续时间不能同时为空的事件

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

在此先感谢

推荐答案

这是我的修正code..working罚款:)

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