首页 > 解决方案 > 事件将在 10 秒内从谷歌日历中删除:Android

问题描述

我正在尝试通过以下代码将事件添加到谷歌日历:

        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, 18)
        calendar.set(Calendar.MINUTE, 30)
        val values = ContentValues()
        values.put(CalendarContract.Events.DTSTART, calendar.timeInMillis)
        calendar.add(Calendar.HOUR, 1)
        values.put(CalendarContract.Events.DTEND, calendar.timeInMillis)
        values.put(CalendarContract.Events.TITLE, "meetingInfo.title")
        values.put(CalendarContract.Events.DESCRIPTION, "meetingInfo.description")
        values.put(CalendarContract.Events.EVENT_LOCATION, "meetingInfo.location")
        val timeZone = TimeZone.getDefault()
        values.put(CalendarContract.Events.ALL_DAY, false)
        values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone.id)
        values.put(CalendarContract.Events.RRULE, "FREQ=YEARLY");
        values.put(CalendarContract.Events.CALENDAR_ID, 1)
        contentResolver.insert(CalendarContract.Events.CONTENT_URI, values)

我已经给了两个许可

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

但在添加此事件后,它会在 10 秒后出现一段时间,它会被删除。

谢谢。

标签: androidcalendargoogle-calendar-apiuri

解决方案


请尝试以下代码。这对我来说是工作代码。它在科特林

        val date = it.getDateformated() -> geting date from viewmodel
        val day = it.getDayOfMonth() -> geting day of month from viewmodel
        val month = it.getMonth() -> geting month from viewmodel
        val year = it.getYear() -> geting year from viewmodel

val calendarIntent = Intent(Intent.ACTION_INSERT)
calendarIntent.data = CalendarContract.Events.CONTENT_URI

calendarIntent.type = "vnd.android.cursor.item/event"
calendarIntent.putExtra(Events.TITLE, getString(R.string.signings_calendar_title))
calendarIntent.putExtra(
    Events.DESCRIPTION,
    mViewModel.instructions
)
mViewModel.dateInfo.value?.let {
    calendarIntent.putExtra(
        Events.EVENT_LOCATION,
        it.getAddress()
    )

    it.ViewedDate?.let { ViewedDatetemp->
       

            val date = it.getDateformated()
            val day = it.getDayOfMonth()
            val month = it.getMonth()
            val year = it.getYear()

            val calDate = GregorianCalendar(year!!, month!!, day!!)
            calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, false)
            calendarIntent.putExtra(
                CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                calDate.timeInMillis
            )
            calendarIntent.putExtra(
                CalendarContract.EXTRA_EVENT_END_TIME,
                calDate.timeInMillis + 60 * 60 * 1000
            )
        
    }
}

calendarIntent.putExtra(Events.ALLOWED_REMINDERS, true)

startActivity(calendarIntent)


推荐阅读