2013-02-15 189 views
1

我目前正在嘗試使用我存儲在我的數據庫中的對象向Google calendar添加活動。存儲在數據庫中的日期以日期格式存儲,時間以字符串形式存儲。通過意向向Google日曆添加時間和日期

目前一切正在工作欄事件的時間部分。日期正確傳遞,但時間默認爲0:00。我會如何做到這一點,所以時間過得恰當?

@Override 
public void onClick(View arg0) { 
if(arg0.getId() == R.id.btnAddToCal) 
{ 

    long startTime = 0, endTime=0; 

    String startDate = blankevent.EventDate; 
    try { 
     Date date = new SimpleDateFormat("yyyy-MM-dd").parse(startDate); 
     startTime=date.getTime(); 
    } 
    catch(Exception e){ } 


    Calendar cal = Calendar.getInstance();    
    Intent calintent = new Intent(Intent.ACTION_EDIT); 
    calintent.setType("vnd.android.cursor.item/event"); 
    calintent.putExtra("eventLocation", blankevent.EventLocation); 
    calintent.putExtra("title", blankevent.EventName); 
    calintent.putExtra("description", blankevent.EventDetails); 

    calintent.putExtra("beginTime",startTime); 
    calintent.putExtra("dtstart", blankevent.EventTime); 

    startActivity(calintent); 
    return; 
} 

我附上了實際問題區域的屏幕截圖。日期是正確的,但我錯誤的時間。

enter image description here

解決代碼

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    Log.e("cal","sel2"); 
    if(arg0.getId() == R.id.btnAddToCal) 
    { 

     String startDate = blankevent.EventDate; 
     String startHour = blankevent.EventTime; 
     Date fulldate = null; 

     try { 
      fulldate = new SimpleDateFormat("yyyy-MM-dd-HH:mm").parse(startDate+"-"+startHour); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     Calendar cal = Calendar.getInstance();    
     Intent calintent = new Intent(Intent.ACTION_EDIT); 
     calintent.setType("vnd.android.cursor.item/event"); 
     calintent.putExtra("eventLocation", blankevent.EventLocation); 
     calintent.putExtra("title", blankevent.EventName); 
     calintent.putExtra("description", blankevent.EventDetails); 
     calintent.putExtra("beginTime", fulldate.getTime()); 
     calintent.putExtra("endTime",fulldate.getTime()+60*60*1000); 


     startActivity(calintent); 
     return; 
    } 
+0

應該在哪裏它會越來越'startTime'乾淨的例子嗎?你的'SimpleDateFormat'解析器只會得到年,月和日。 – 2013-02-15 14:37:34

+1

您的日期分析字符串錯誤,yyyy-mm-dd,表示年,月和日。應該是yyyy-MM-dd。 – Kennet 2013-02-15 14:41:11

+0

startTime是EventDate的解析輸出。 EventDate是在我的數據庫中以YYYY-MM-DD格式存儲的字段,我有一個以HH:MM形式存儲爲字符串的EventTime字段。 yyyy-MM-dd錯誤是我自己的錯誤,當輸入時,它在實際代碼中是正確的 – Andrela 2013-02-15 14:59:17

回答

2

你的開始時間必須是毫秒。另外我真的不明白你的意圖額外的字段名稱。

由於您只解析dd - MM - YYYY,所以時間仍然保留在00:00,並且事件的默認持續時間爲1小時。 所以你可能只需要設置Date對象的時間(startTime),然後調用startTime.getMillis();

String startDate = blankevent.EventDate; 
String startHour = blankevent.EventTime; 
Date date = new SimpleDateFormat("yyyy-MM-dd-HH:mm").parse(startDate+"-"+startHour); 

此外, 這裏是www.developer.com By Lauren Darcey & Shane Conder

Intent calIntent = new Intent(Intent.ACTION_INSERT); 
calIntent.setData(Events.CONTENT_URI); 
calIntent.putExtra(Events.TITLE, "Google IO Afterparty"); 
calIntent.putExtra(Events.EVENT_LOCATION, "The W Hotel Bar on Third Street"); 
calIntent.putExtra(Events.DESCRIPTION, "Hang out after Google IO for a drink and geeky conversation."); 
Calendar startTime = Calendar.getInstance(); 
startTime.set(2012, 5, 29, 18, 0); 
Calendar endTime = Calendar.getInstance(); 
endTime.set(2012, 5, 29, 22, 30); 
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, 
startTime.getTimeInMillis()); 
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, 
endTime.getTimeInMillis()); 
startActivity(calIntent); 
+0

額外的字段名稱是來自數據庫檢索對象的字段。這在其他應用程序的背景下是有意義的。 – Andrela 2013-02-15 15:12:10

+1

檢查我添加到我的答案的3行代碼,如果真的唯一的問題是時間不正確,這應該解決它。 – 2013-02-15 15:16:14

+0

我已經嘗試了這3個額外的行。它似乎在工作。我做了一個日誌來檢查格式,它是 02-15 15:28:31.300:E/callog(20057):sel2Thu Jan 10 22:00:00 GMT 2013 但是它並沒有傳遞給實際日曆 – Andrela 2013-02-15 15:29:35