2013-03-05 173 views
1

我試圖直接使用此代碼添加事件:直接添加事件到日曆

// Construct event details 
long startMillis = 0; 
long endMillis = 0; 
Calendar beginTime = Calendar.getInstance(); 
beginTime.set(2013, 3, 14, 7, 30); 
startMillis = beginTime.getTimeInMillis(); 
Calendar endTime = Calendar.getInstance(); 
endTime.set(2013, 3, 14, 8, 45); 
endMillis = endTime.getTimeInMillis(); 


// Insert Event 
ContentResolver cr = getContentResolver(); 
ContentValues values = new ContentValues(); 
values.put(CalendarContract.Events.DTSTART, startMillis); 
values.put(CalendarContract.Events.DTEND, endMillis); 
values.put(CalendarContract.Events.TITLE, "Walk The Dog"); 
values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!"); 
values.put(CalendarContract.Events.CALENDAR_ID, 3); 
values.put("eventTimezone", "Europe/London"); 
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values); 
// Retrieve ID for new event 
String eventID = uri.getLastPathSegment(); 

此代碼語法是正確的,但不向日曆添加事件。 問題是什麼? 而且Eclipse說

String eventID = uri.getLastPathSegment(); 

未被使用。

回答

0

檢查您是否擁有WRITE_CALENDAR權限以便能夠以此方式創建事件。

此外,在您的示例中,eventID變量確實不被使用 - >您對此無能爲力。

+0

對不起,我已經 <使用許可權的android:NAME = 「android.permission.READ_CALENDAR」/> <使用許可權的android:NAME = 「android.permission.WRITE_CALENDAR」/> – 2013-03-05 20:47:36

+0

我忘了說,我也有一個錯誤: 錯誤打開跟蹤文件:沒有這樣的文件或目錄(2) – 2013-03-05 20:51:11