首页 > 解决方案 > 如何修复日历的意图而不是将其添加为全天事件

问题描述

我正在尝试向我的应用程序添加一个按钮,该按钮允许某人将任务添加到他们的日历中。我正在使用 Intent 和 CalendarContract 执行此操作。一切正常,因为它以添加事件状态启动日历应用程序,并且所有信息都在正确的字段中并正确显示。我唯一遇到的问题是

CalendarContract.EXTRA_EVENT_ALL_DAY to true

没有使其默认为全天活动。

我已经尝试将它分成多个 .putExtra 语句,并尝试了单个组件,但这些似乎都没有任何区别。

startActivity(Intent(Intent.ACTION_EDIT)
                    .setData(CalendarContract.Events.CONTENT_URI)
                    .setType("vnd.android.cursor.dir/event")
                    .putExtras(
                            bundleOf(
                                    CalendarContract.Events.TITLE to assignment.getItemText(),
                                    CalendarContract.Events.DESCRIPTION to "${assignment.beginDate.takeIf { it != null }?.let { 
                                        "${getString(R.string.start_date)}: ${it.format("MMM dd, h:mm a")}\n\n"} ?: ""}${getString(R.string.due_date)}: ${assignment.dueDate.format("MMM dd, h:mm a")}\n\n${assignment.getDetailText()}",
                                    // If there is just one time, put it in for both so that the generated event in the calendar app has logical and related start/end times
                                    CalendarContract.EXTRA_EVENT_BEGIN_TIME to (assignment.beginDate?.time ?: assignment.dueDate.time),
                                    CalendarContract.EXTRA_EVENT_END_TIME to assignment.dueDate.time,
                                    CalendarContract.EXTRA_EVENT_ALL_DAY to true
                            )
                    )
            )

我希望这将允许我添加一个全天选择的事件,但它不会默认为该事件。

我注意到这个问题只发生在谷歌日历上

任何帮助都会非常感谢。

标签: androidandroid-intentkotlincalendar

解决方案


这似乎是当前版本的 Android 与 Google 日历集成中的一个错误。希望他们很快会在补丁版本中修复错误。它肯定适用于我测试过的所有其他日历


推荐阅读