[嗨,我正在我的应用程序中开发日历功能。我通过以下方式完成此操作:
fun generateEvent(context: Context, calendarId: String?,
eventTitle: String, eventDescription: String,
startTime: Long, endTime: Long)
{
if(!calendarId.isNullOrEmpty()){
val cv = ContentValues()
cv.put(CalendarContract.Events.CALENDAR_ID, calendarId)
cv.put(CalendarContract.Events.TITLE, eventTitle)
cv.put(CalendarContract.Events.DESCRIPTION, eventDescription)
cv.put(
CalendarContract.Events.DTSTART,
startTime
)
cv.put(
CalendarContract.Events.DTEND,
endTime
)
cv.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().id)
cv.put(CalendarContract.Events.HAS_ALARM, true)
val contentResolver: ContentResolver = context.getContentResolver()
contentResolver.insert(CalendarContract.Events.CONTENT_URI, cv)
var eventUri = contentResolver.insert(CalendarContract.Events.CONTENT_URI, cv)
var eventID = eventUri?.getLastPathSegment()?.toLong()
if(eventID != null){
val reminderCV = ContentValues()
reminderCV.put(CalendarContract.Reminders.EVENT_ID, eventID)
reminderCV.put(CalendarContract.Reminders.MINUTES, 1)
reminderCV.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT)
contentResolver.insert(CalendarContract.Reminders.CONTENT_URI, reminderCV)
}
}
}
它会在Google日历上生成事件和提醒。唯一的问题是它在日历上显示两个条目。一种是带有提醒,另一种是没有提醒。
您有两个插入语句。只需删除
contentResolver.insert(CalendarContract.Events.CONTENT_URI, cv)
并且您的代码可以正常工作