Android 带有提醒的事件在日历应用程序中显示两个条目
Android Event with Reminder shows two entries in calendar application
您好,我正在我的应用程序中开发日历功能。我通过以下方式完成了此操作:
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)
并且您的代码工作正常
您好,我正在我的应用程序中开发日历功能。我通过以下方式完成了此操作:
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)
并且您的代码工作正常