Hi I am developing calendar feature in my application. I have done this in following way:
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)
}
}
}
It generate the event and reminder on google calendar. Only issue is that it shows two entries on calendar. One is with reminder and other one without reminder.
You have two insert statements. Just remove the
contentResolver.insert(CalendarContract.Events.CONTENT_URI, cv)
and your code works fine
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.