在 Android 日历中插入事件时出现异常

发布于 2024-09-13 03:40:51 字数 1335 浏览 8 评论 0原文

我正在我的 Android 日历中插入事件。代码如下:

ContentValues event = new ContentValues();
    event.put("calendar_id", calId);
    event.put("title", "Event Title");
    event.put("description", "Event Desc");
    event.put("eventLocation", "Event Location");
    event.put("allDay", 1);
    event.put("eventStatus", 1);
    event.put("visibility", 0);
    event.put("transparency", 0);
    event.put("hasAlarm", 1);

    Date d = new Date();
    d.setHours(8);
    d.setMinutes(30);
    d.setSeconds(30);
    long startTime = d.getTime();
    d.setHours(12);
    d.setMinutes(30);
    d.setSeconds(20);
    long endTime = d.getTime();
    event.put("dtstart", startTime);
    // event.put("dtend", endTime);
    event.put("rrule", "FREQ=DAILY;WKST=SU");
    // event.put("lastDate", endTime);
    // event.put("timezone", "Asia/Karachi");
    //event.put("duration", "P3600S");

    //Calendar gmtC = new GregorianCalendar(TimeZone.getTimeZone("Asia/Karachi"));



    // event.put("transparency", 0);
    // event.put("hasAlarm", 1); // 0 for false, 1 for true
    Uri eventsUri = Uri.parse("content://calendar/events");
    Uri url = getContentResolver().insert(eventsUri, event);

我收到以下异常:

java.lang.IllegalArgumentException: allDay is true but sec, min, hour are not 0.

需要帮助!

i am inserting events in my android calendar. the code is following:

ContentValues event = new ContentValues();
    event.put("calendar_id", calId);
    event.put("title", "Event Title");
    event.put("description", "Event Desc");
    event.put("eventLocation", "Event Location");
    event.put("allDay", 1);
    event.put("eventStatus", 1);
    event.put("visibility", 0);
    event.put("transparency", 0);
    event.put("hasAlarm", 1);

    Date d = new Date();
    d.setHours(8);
    d.setMinutes(30);
    d.setSeconds(30);
    long startTime = d.getTime();
    d.setHours(12);
    d.setMinutes(30);
    d.setSeconds(20);
    long endTime = d.getTime();
    event.put("dtstart", startTime);
    // event.put("dtend", endTime);
    event.put("rrule", "FREQ=DAILY;WKST=SU");
    // event.put("lastDate", endTime);
    // event.put("timezone", "Asia/Karachi");
    //event.put("duration", "P3600S");

    //Calendar gmtC = new GregorianCalendar(TimeZone.getTimeZone("Asia/Karachi"));



    // event.put("transparency", 0);
    // event.put("hasAlarm", 1); // 0 for false, 1 for true
    Uri eventsUri = Uri.parse("content://calendar/events");
    Uri url = getContentResolver().insert(eventsUri, event);

i am getting the following exception:

java.lang.IllegalArgumentException: allDay is true but sec, min, hour are not 0.

need help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾铮苏瑾 2024-09-20 03:40:51

我遇到了同样的问题,即使将小时、分钟和秒设置为 0。然后我发现,对于全天事件,时间必须以 UTC 格式设置。这意味着,您需要将日历时区的 UTC 偏移量添加到全天活动开始时间。

例如:(时区和开始时间只是为了简化目的而硬编码!)

// You should write a method to get the calendar's timezone through a query
String calendarTimezone = "CET";

// Start time of the event. Hours, minutes and seconds have to be 0.
long startTime = 1315087200000L; // equals 2011-09-04 00:00:00

// Get UTC offset for the given timezone and start time. This way DST is accounted for.
int timeZoneOffset = TimeZone.getTimeZone(calendarTimezone).getOffset(startTime);

// Set same time for start and end of the allday event. Add UTC offset.
event.put("dtstart", startTime + timeZoneOffset);
event.put("dtend", startTime + timeZoneOffset);
event.put("allDay", 1);

I ran into the same problem, even with hours, minutes and seconds set to 0. Then I discovered, that for allday events, the times have to be set in UTC. This means, you need to add the UTC offset of the calendar's timezone to your allday event start time.

For example: (timezone and startTime are only hardcoded for simplification purposes!)

// You should write a method to get the calendar's timezone through a query
String calendarTimezone = "CET";

// Start time of the event. Hours, minutes and seconds have to be 0.
long startTime = 1315087200000L; // equals 2011-09-04 00:00:00

// Get UTC offset for the given timezone and start time. This way DST is accounted for.
int timeZoneOffset = TimeZone.getTimeZone(calendarTimezone).getOffset(startTime);

// Set same time for start and end of the allday event. Add UTC offset.
event.put("dtstart", startTime + timeZoneOffset);
event.put("dtend", startTime + timeZoneOffset);
event.put("allDay", 1);
绻影浮沉 2024-09-20 03:40:51

日历 GData API 定义了一个全天事件,其开始时间仅为日期,结束时间为事件结束后的一天。

这是发送到 google data api 的数据。

<entry xmlns='http://www.w3.org/2005/Atom'
    xmlns:gd='http://schemas.google.com/g/2005'>

  <category scheme='http://schemas.google.com/g/2005#kind'
    term='http://schemas.google.com/g/2005#event'></category>
  <title type='text'>Word of the Day</title>
  <gd:when startTime='2007-07-17'
    endTime='2007-07-18'></gd:when>

</entry>

请注意,开始/结束时间不包含任何时间信息。

您不能举办不从午夜开始的全天活动。这就是为什么你会得到例外。在全天活动中,小时、分钟、秒必须为 0。

您可以尝试另一个论坛,但您总会得到这个答案,因为这就是 GData API 的工作原理。

The calendar GData API defines an all day event with a start time of just the date, and the end time of the day after the end of the event.

This is the data sent to google data api

<entry xmlns='http://www.w3.org/2005/Atom'
    xmlns:gd='http://schemas.google.com/g/2005'>

  <category scheme='http://schemas.google.com/g/2005#kind'
    term='http://schemas.google.com/g/2005#event'></category>
  <title type='text'>Word of the Day</title>
  <gd:when startTime='2007-07-17'
    endTime='2007-07-18'></gd:when>

</entry>

Notice the start/end times do not contain any time information in them.

You CANNOT have an all day event that doesn't start at midnight. That is why you are getting the exception. hour, min, sec MUST be 0 for them on an all day event.

You can try another forum, but you will always get this answer because this is how the GData API works.

南薇 2024-09-20 03:40:51

如果 allDay 设置为 1 eventTimezone 必须是 TIMEZONE_UTC 并且时间必须对应于午夜边界,意味着日历对象的 hour 、 分钟 、 Second 应该为零。

请参阅以下链接..

http://developer.android.com/reference/ android/provider/CalendarContract.Events.html

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));           
cal.set(2014, 03, 18, 0, 0,0);

If allDay is set to 1 eventTimezone must be TIMEZONE_UTC and the time must correspond to a midnight boundary, means hour , minute , second should be zero of calendar object.

refer following link..

http://developer.android.com/reference/android/provider/CalendarContract.Events.html

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));           
cal.set(2014, 03, 18, 0, 0,0);
笑忘罢 2024-09-20 03:40:51

您将“allDay”指定为 true,但您设置了秒、分和小时的时间。这对于系统来说意味着,它不是一整天......尝试删除 allDay 或时间设置。也许这些是相反的、矛盾的。

you specify "allDay" as true, but you set a time with sec, min and hour. Which means for the system, taht it's not all day long... try removing either allDay or the time setting. Maybe these are opposite and contradictory.

狼亦尘 2024-09-20 03:40:51

抢劫是正确的!您必须以 UTC 定义全天事件。
下面的代码比 robs 版本好一点:

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));

    long dtstart = cal.getTimeInMillis();

    builder.withValue(Events.DTSTART, dtstart);

rob is correct! You have to define allday events in UTC.
The following code is a little bit nicer than robs version:

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));

    long dtstart = cal.getTimeInMillis();

    builder.withValue(Events.DTSTART, dtstart);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文