【问题标题】:getting exception when inserting events in android calendar在android日历中插入事件时出现异常
【发布时间】:2011-03-27 07:01:25
【问题描述】:

我在我的 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.

需要帮助!

【问题讨论】:

  • 您使用的内容提供程序不是 Android SDK 的一部分。您的代码将在某些不包含 AOSP 日历应用程序或已对其进行修改的设备上中断。您的代码可能会在未来的 Android 版本中中断。请使用 Google GData API 来操作用户的日历。
  • 亲爱的,我知道所有这些事情,但我目前需要解决这个问题。
  • 要解决此问题,请使用 Google GData API 来操作用户的日历。
  • 到目前为止,Google GData 还不支持 Calendar A?I。您可以查看以下链接:code.google.com/p/android-gdata android gdata 正在开发中。
  • 如果不是从午夜开始,就不是一整天……那么它只是一天的一部分。 8:30 到 12:30 不是全天。

标签: android calendar gdata-api gdata


【解决方案1】:

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

例如:(时区和 startTime 只是为了简化而硬编码!)

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

【讨论】:

    【解决方案2】:

    日历 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 就是这样工作的。

    【讨论】:

    • 我怎样才能使用你上面提到的代码。如果有,请提供示例代码。
    • gdata库2.0版支持android。 code.google.com/p/gdata-java-client 你可以在那里得到它。文档位于code.google.com/apis/calendar/data/2.0/…
    • 感谢您的回复。我在 gdata 库上工作,但从那里我得出结论,直到现在 gdata 在 android 上不支持。你在安卓上试过吗?
    • 什么不起作用?您将库添加到您的项目中,然后使用该库进行“api 调用”。文档中有关于如何使用 api 执行所有操作的示例代码。
    • 您可以查看 gdata 中的示例代码。从那里你会发现它适用于java而不是android。我已经尝试过了。
    【解决方案3】:

    如果 allDay 设置为 1,则 eventTimezone 必须是 TIMEZONE_UTC 并且时间必须对应于午夜边界,这意味着 hour 、 minute 、 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);
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 好的,我希望这个论坛给出同样的答案。我知道我可以在日历中添加简单的事件。但我想添加全天重复事件。现在我将尝试其他一些论坛。
      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多