如何在Android中添加日历事件?
我刚刚开始了解 Android,今天在一个项目会议上有人说 Android 没有本机日历应用程序,因此用户只需使用他们喜欢的任何日历应用程序即可。
这是真的吗?如果是,我如何以编程方式将事件添加到用户的日历中?它们有共同的 API 吗?
就其价值而言,我们的目标可能是 Android 2.x。
I'm just getting up to speed on Android, and today in a project meeting someone said that Android has no native calendar app so users just use whatever calendar app they like.
Is this true, and if so how do I programmatically add an event to the user's calendar? Is there a common API they all share?
For what it's worth, we're probably targeting Android 2.x.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
在您的代码中尝试一下:
Try this in your code:
在您的代码中使用此 API.. 它将帮助您插入事件,可以启用带提醒的事件和带会议的事件... 该 api 适用于平台 2.1 及更高版本
那些使用小于2.1而不是content://com.android.calendar/events的人使用content://calendar/events
Use this API in your code.. It will help u to insert event, event with reminder and event with meeting can be enabled... This api works for platform 2.1 and above
Those who uses less then 2.1 instead of content://com.android.calendar/events use content://calendar/events
我使用下面的代码,它解决了我在 ICS 中的默认设备日历中添加事件的问题,也解决了 ICS 版本较小的问题,
希望它会有所帮助......
i used the code below, it solves my problem to add event in default device calendar in ICS and also on version less that ICS
Hope it would help.....
从 Android 版本 4.0 开始官方 API 和意图可用 与可用的日历提供程序进行交互。
As of Android version 4.0 official APIs and intents are available to interact with the available calendar providers.
哪个日历?
不,就像 Windows 日历应用程序有一个“他们都共享的通用 API”一样。有一些通用的数据格式(例如,iCalendar)和互联网协议(例如,CalDAV),但没有通用的API。有些日历应用程序甚至不提供 API。
如果您希望集成特定的日历应用程序,请联系其开发人员并确定他们是否提供 API。例如,Mayra 引用的 Android 开源项目中的日历应用程序不提供任何记录和支持的 API。 Google 甚至明确告诉开发人员不要使用这些技术 在 Mayra 引用的教程中概述。
另一种选择是您将事件添加到相关的 Internet 日历中。例如,从 Android 开源项目向日历应用程序添加事件的最佳方法是通过适当的 GData API 将事件添加到用户的 Google 日历。
更新
Android 4.0(API 级别 14)添加了
CalendarContract
ContentProvider
。Which calendar?
No, no more than there is a "common API they all share" for Windows calendar apps. There are some common data formats (e.g., iCalendar) and Internet protocols (e.g., CalDAV), but no common API. Some calendar apps don't even offer an API.
If there are specific calendar applications you wish to integrate with, contact their developers and determine if they offer an API. So, for example, the Calendar application from the Android open source project, that Mayra cites, offers no documented and supported APIs. Google has even explicitly told developers to not use the techniques outlined in the tutorial Mayra cites.
Another option is for you to add events to the Internet calendar in question. For example, the best way to add events to the Calendar application from the Android open source project is to add the event to the user's Google Calendar via the appropriate GData APIs.
UPDATE
Android 4.0 (API Level 14) added a
CalendarContract
ContentProvider
.试试这个,
Try this ,
以防万一有人需要 C# 中的 Xamarin:
辅助函数:
Just in case if someone needs this for Xamarin in c#:
Helper Functions:
Google 日历是“原生”日历应用程序。据我所知,所有手机都安装了它的一个版本,默认的SDK也提供了一个版本。
您可以查看此教程 用于使用它。
Google calendar is the "native" calendar app. As far as I know, all phones come with a version of it installed, and the default SDK provides a version.
You might check out this tutorial for working with it.
如果您有给定的带有 date 和 time 的日期字符串。
例如
String GiveDateString = pojoModel.getDate()/* Format dd-MMM-yyyy hh:mm:ss */
使用以下代码将带有日期和时间的事件添加到日历中
if you have a given Date string with date and time .
for e.g
String givenDateString = pojoModel.getDate()/* Format dd-MMM-yyyy hh:mm:ss */
use the following code to add an event with date and time to the calendar
您必须添加标志:
否则您将导致错误:
startActivity()
从 Activity 上下文外部需要FLAG_ACTIVITY_NEW_TASK
you have to add flag:
or you will cause error with:
startActivity()
from outside of an Activity context requires theFLAG_ACTIVITY_NEW_TASK
以下是将事件添加到日历的 Kotlin 版本:
Here is the Kotlin version for adding event to calendar :
Android 主要提供两种处理日历事件的方法。一种是使用
Calendar Provider
,另一种是将其交给系统日历应用
。CalendarProvider为我们提供了所有功能,包括插入、查询、更新和删除现有的日历事件。但是,这些步骤非常繁琐,并且必须需要用户的运行时权限(
android.permission.READ_CALENDA
R 和android.permission.WRITE_CALENDAR
)才能读取和写入敏感日历信息。这种方法很容易出错。Google官方建议开发者使用第二种方式,即通过Intent将所有日历操作交给系统日历应用。日历应用程序在我们的应用程序请求后立即打开
甚至插入新日历
以下活动打开
Android provides mainly two approaches to work on calendar events. One is using
Calendar Provider
and the other one is handing off it to thesystem Calendar app.
Calendar Provider provides us all the functionalities, including inserting, querying, updating and deleting existing calendar events. However, the steps are tedious and must require user’s runtime permissions (
android.permission.READ_CALENDA
R andandroid.permission.WRITE_CALENDAR
) to read and write the sensitive calendar information. It is easy to make a mistake with this approach.Google officially recommends developers use the second approach, i.e. handing off all the calendar operations to the system Calendar app by using Intent. The calendar app is opened right after requested by our app
Inserting new calendar even
Following activity is opend
这是官方教程:
https:// developer.android.com/guide/topics/providers/calendar-provider?hl=id#intent-insert
它有效!
this is official tutorial from :
https://developer.android.com/guide/topics/providers/calendar-provider?hl=id#intent-insert
it work!