2012-11-23 48 views
1

這是我的代碼,查看從ListActivity中選擇一個事件(events是包含所有這些事件的ArrayList):Android的Google日曆的重複活動有錯誤的結束日期/時間

Uri viewUri = Uri.parse("content://com.android.calendar/events/" + events.get(position).id); 
Intent l_intent = new Intent(Intent.ACTION_VIEW); 
l_intent.putExtra("beginTime", Long.parseLong(events.get(position).startTime)); 
l_intent.putExtra("endTime", Long.parseLong(events.get(position).endTime)); 
startActivity(l_intent); 

該代碼完全適用於所有事件除了經常性事件。對於任何正在重複發生的事件,endTime返回爲空,導致程序崩潰。任何人都知道如何解決這個問題?還有其他的演員,我應該通過?你可能要解析之前把一個null檢查

回答

4

BEGINTIME和結束時間可以是0/null,因爲你從一個錯誤的數據庫從事件數據庫得到了他們的肯定。 您應該使用實例數據庫(例如:SDK 8上的「content://com.android.calendar/instances/when/」)。

實例 DB中,您將獲得所有「真實」事件:其中,每個循環事件具有所需的實例數量,並具有正確的開始和結束時間戳;其他事件也可見。您只需閱讀這些字段 - event_id,開始,結束 - 並使用它們來打開您的意圖。

0

我的事:

 long endTime = 0L;//use some default value 
     if(events.get(position).endTime != null) 
      endTime = Long.parseLong(events.get(position).endTime); 
     } 
     l_intent.putExtra("endTime", endTime); 
+0

這將避免空錯誤,但它仍然不會讓我準確的重複事件結束時間。 –

+0

@JohnRoberts:我認爲重複發生的事件沒有結束時間,因爲它們正在反覆發生?沒有? –

+0

好吧,假設你每個星期五都有一個小時的會議。這有一個結束時間,並且正在反覆出現。您也可以通過Android日曆確認。 –

2

在處理日曆事件時,我發現calendar.db中的事件表存儲正常事件的信息,正如您使用的那樣。 (我猜你正在使用startTime作爲Dtstart和endTime作爲Dtend)

但是在復發事件的情況下,Dtend將爲空。因此,相反,使用lastDate列,因爲此列不會爲空。無論是復發事件還是正常事件,它都能很好地工作。

但是,如果您需要關於重複事件的更多信息,請使用@GeH建議的實例表(如每個事件的確切開始和結束時間)。

0

重複事件不會在我的應用程序來你能告訴我,我怎麼能做到這一點,我有別像

日期時間的startDate = DateTime.now()withTime(0,0,0,0); DateTime endDate = startDate.plusDays(7);

Uri.Builder eventsUriBuilder = CalendarContract.Instances.CONTENT_URI 
      .buildUpon(); 
    ContentUris.appendId(eventsUriBuilder, startDate.getMillis()); 
    ContentUris.appendId(eventsUriBuilder, endDate.getMillis()); 

    Uri eventsUri = eventsUriBuilder.build(); 
    Cursor cursor = null; 
    cursor = getContentResolver().query(
      eventsUri, 
      new String[] {CalendarContract.Instances.CALENDAR_ID, CalendarContract.Instances.TITLE, 
        CalendarContract.Instances.DESCRIPTION, CalendarContract.Instances.BEGIN, 
        CalendarContract.Instances.END, CalendarContract.Instances.EVENT_LOCATION, 
        CalendarContract.Instances.EVENT_ID}, 
      CalendarContract.Instances.DTSTART + " >= " + startDate.getMillis() + " and " + CalendarContract.Instances.DTSTART + " <= " + endDate.getMillis() + " and " + CalendarContract.Instances.VISIBLE + " = 1", 
      null, 
      CalendarContract.Instances.DTSTART + " ASC"); 
    cursor.moveToFirst(); 
    do{ 
     // final String title = cursor.getString(0); 
     String title = cursor.getString(1); 
     String details = cursor.getString(2); 
     String date = new Date((cursor.getLong(3))).toString(); 

     final Date begin = new Date(cursor.getLong(1)); 
     final Date end = new Date(cursor.getLong(2)); 
     final Boolean allDay = !cursor.getString(3).equals("0"); 

     Log.i("title",title); 
     Log.i("details",details); 
     Log.i("date",date); 
     Log.i("begin",begin+""); 
     Log.i("end",end+""); 
    } while (cursor.moveToNext()); its giving regular events 
相關問題