1

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.

助けが必要!

4

5 に答える 5

4

時間、分、秒が 0 に設定されていても、同じ問題に遭遇しました。その後、終日のイベントの場合、時間を UTC で設定する必要があることを発見しました。つまり、カレンダーのタイムゾーンの UTC オフセットを終日イベントの開始時刻に追加する必要があります。

例: (timezone と 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);
于 2011-09-28T16:56:11.223 に答える
1

カレンダーGDataAPIは、開始時刻が日付だけで、終了時刻がイベントの終了日の翌日の終日イベントを定義します。

これは、Googleデータ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の動作方法であるため、常にこの回答が得られます。

于 2010-08-09T14:36:59.130 に答える
1

allDay が 1 に設定されている場合、eventTimezone は TIMEZONE_UTC である必要があり、時間は午前 0 時の境界に対応している必要があります。つまり、カレンダー オブジェクトの hour 、 minutes 、 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);
于 2014-03-18T12:07:54.137 に答える
0

ロブ正解!終日のイベントを 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);
于 2012-03-13T23:19:05.720 に答える
0

「allDay」を true に指定しますが、秒、分、時間で時間を設定します。つまり、システムにとって、それは終日ではありません... allDay または時間設定のいずれかを削除してみてください。たぶん、これらは正反対で矛盾しています。

于 2010-08-09T13:04:34.987 に答える