ContentResolver cr = getCurrentContext().getContentResolver();
ContentValues values = new ContentValues();
Calendar startDate = Calendar.getInstance();
startDate.setTimeInMillis(contentDatum.getGist().getScheduleStartDate());
Calendar endDate = Calendar.getInstance();
endDate.setTimeInMillis(contentDatum.getGist().getScheduleEndDate());
values.put(CalendarContract.Events.DTSTART, startDate.getTimeInMillis());
values.put(CalendarContract.Events.DTEND, endDate.getTimeInMillis());
values.put(CalendarContract.Events.TITLE, contentDatum.getGist().getTitle());
values.put(CalendarContract.Events.DESCRIPTION, contentDatum.getGist().getDescription());
values.put(CalendarContract.Events.CALENDAR_ID, getCalendarId());
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
Log.e("event", "" + eventID);
setCalendarEventId(eventID);
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID));
getCurrentActivity().startActivityForResult(intent, 0);
我正在保存插入时返回的eventID。
为了根据事件id检索数据,我使用下面的代码
for (int i = 0; i < calendarEventIds.length; i++) {
long eventID = Long.parseLong(calendarEventIds[i]);
Uri event = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
Cursor cursor;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
cursor = getCurrentContext().getContentResolver().query(event, null, null, null);
} else {
cursor = getCurrentActivity().managedQuery(event, null, null, null, null);
}
if (cursor.getCount() == 1) {
if (cursor.moveToFirst()) {
if (cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTSTART)) == contentDatum.getGist().getScheduleStartDate()
&& cursor.getLong(cursor.getColumnIndex(CalendarContract.Events.DTEND)) == contentDatum.getGist().getScheduleEndDate()
&& cursor.getString(cursor.getColumnIndex(CalendarContract.Events.TITLE)).equalsIgnoreCase(contentDatum.getGist().getTitle())
&& cursor.getString(cursor.getColumnIndex(CalendarContract.Events.DESCRIPTION)).equalsIgnoreCase(contentDatum.getGist().getDescription())) {
Toast.makeText(getCurrentContext(), "Event already exists in your calendar.", Toast.LENGTH_SHORT).show();
calendarEventExist = true;
}
}
}
}
不过,我不知道我补充的事件的细节。