2013-02-15 94 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