在插入的android日历事件时得到的异常 [英] getting exception when inserting events in android calendar

查看:248
本文介绍了在插入的android日历事件时得到的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在插入在我的Andr​​oid日历事件。在code为以下内容:

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.

需要帮助!

推荐答案

我遇到了同样的问题,即使小时,分钟和设置为0秒,后来我发现,对于allday事件,时代已经被设置在UTC。这意味着,你需要将UTC日历的时区偏移量添加到您的allday活动开始时间。

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.

例如:(时区和startTime时仅用于简化的目的很难codeD!)

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

这篇关于在插入的android日历事件时得到的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆