2016-12-01 111 views
1

我嘗試將新事件插入到Google日曆中。我的代碼如下所示:Android - 插入日曆事件

@OnClick(R.id.add_event_button) 
    public void addEvent() { 
     CalendarDB calendar = (CalendarDB) mCalendarSpinner.getSelectedItem(); 
     String id = calendar.calendarId; 
     ContentValues event = new ContentValues(); 
     event.put(CalendarContract.Events.EVENT_TIMEZONE, "Europe/Berlin"); // Here choose your location time zone 

     event.put("calendar_id", id); 
     event.put("title", "barteq calendar tutorial test"); 
     event.put("description", "This is a simple test for calendar api"); 
     event.put("dtstart", System.currentTimeMillis()); 
     event.put("dtend", System.currentTimeMillis() + 1800*1000); 
     event.put("allDay", 0); 
     event.put("eventStatus", 3); 

     event.put("hasAlarm", 1); 
     Uri l_eventUri; 
     if (Build.VERSION.SDK_INT >= 8) { 
      l_eventUri = Uri.parse("content://com.android.calendar/events"); 
     } else { 
      l_eventUri = Uri.parse("content://calendar/events"); 
     } 
     Uri l_uri = this.getContentResolver().insert(l_eventUri, event); 
     } 

但是我越來越問題ID:

java.lang.IllegalArgumentException: New events must specify a calendar id 

我檢查了它 - 我送正確的ID - 例如[email protected]所以爲什麼谷歌有問題呢?有任何想法嗎 ?在此先感謝

回答

1

加入此列指定的問題依然存在日曆ID

event.put(CalendarContract.Events.CALENDAR_ID,1); 
+0

:/ – Bartos