0
我嘗試在與我共享的日曆上設置事件。我知道我應該使用日曆的ID。但是我有問題。這裏是我的代碼:Android,如果共享日曆獲取ID
@OnClick(R.id.add_event_button)
public void addEvent() {
CalendarDB calendar = (CalendarDB) mCalendarSpinner.getSelectedItem();
Long id = calendar.getId();
ContentValues event = new ContentValues();
event.put("calendar_id", 1);
event.put(CalendarContract.Events.EVENT_TIMEZONE, "Europe/Berlin"); // Here choose your location time zone
event.put("title", "test application");
event.put("description", "This is test event");
event.put("eventLocation", "School");
event.put("dtstart", System.currentTimeMillis());
event.put("dtend", System.currentTimeMillis() + 1800 * 1000);
event.put("allDay", 0);
// status: 0~ tentative; 1~ confirmed; 2~ canceled
// event.put("eventStatus", 1);
event.put(CalendarContract.Events.CALENDAR_ID,8);
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);
Toast.makeText(this, "SUCCESS", Toast.LENGTH_SHORT).show();
}
這一工程,並不過被添加的事件,日曆的ID是未知的 - 我的意思是我嘗試手動設置 - 1,2,3等,並弄明白上哪個日曆事件將被添加。谷歌API網站說,ID應該是長的,當我得到日曆列表時,我得到字符串作爲id - 日曆的名稱。同樣的情況在android.provider CalendarContract.class:
public static final String CALENDAR_ID = "calendar_id";
當我設置ID作爲參數的應用程序崩潰 - 它需要長期爭論。那麼,如何獲得共享日曆的ID?預先感謝幫助:)