android - 事件将在 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 秒后出现一段时间,它会被删除。
谢谢。
解决方案
请尝试以下代码。这对我来说是工作代码。它在科特林
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)
推荐阅读
- vue.js - VUEJS“Vue Hotel Datepicker”默认显示日历折叠打开
- javascript - 如何根据日出和日落时间戳确定白天或夜间
- angular - 使用管理站点布局和前端站点布局 angular 8
- macos - 使用 automator 和 AppleScript 根据文件的名称和内容将文件移动到不同的文件夹和子文件夹
- c# - C#如何在DLL中记录文件?
- c - codeforces 中的运行时错误,但在 Dev-C++ 中运行良好
- r - 只有一条胡须线的箱线图?
- python - 计算引擎上的 Python3 多处理池不平衡 CPU 使用率
- jmeter - 如何将我从一次性设置线程组获得的基本授权传递给主线程组
- javascript - 复选框下拉菜单未正确显示