在 Android 日历中插入事件时出现异常
我正在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到了同样的问题,即使将小时、分钟和秒设置为 0。然后我发现,对于全天事件,时间必须以 UTC 格式设置。这意味着,您需要将日历时区的 UTC 偏移量添加到全天活动开始时间。
例如:(时区和开始时间只是为了简化目的而硬编码!)
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!)
日历 GData API 定义了一个全天事件,其开始时间仅为日期,结束时间为事件结束后的一天。
这是发送到 google data api 的数据。
请注意,开始/结束时间不包含任何时间信息。
您不能举办不从午夜开始的全天活动。这就是为什么你会得到例外。在全天活动中,小时、分钟、秒必须为 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
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.
如果 allDay 设置为 1 eventTimezone 必须是 TIMEZONE_UTC 并且时间必须对应于午夜边界,意味着日历对象的 hour 、 分钟 、 Second 应该为零。
请参阅以下链接..
http://developer.android.com/reference/ android/provider/CalendarContract.Events.html
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
您将“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.
抢劫是正确的!您必须以 UTC 定义全天事件。
下面的代码比 robs 版本好一点:
rob is correct! You have to define allday events in UTC.
The following code is a little bit nicer than robs version: