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.
我刚开始在Android上加速,今天在一个项目会议上有人说Android没有本地日历应用程序,所以用户只能使用他们喜欢的日历应用程序。
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?
这是真的吗?如果是的话,我如何以编程方式向用户的日历添加事件?他们有共同的API吗?
For what it's worth, we're probably targeting Android 2.x.
不管怎么说,我们的目标很可能是Android 2.x。
10 个解决方案
#1
16
how do I programmatically add an event to the user's calendar?
如何以编程方式向用户的日历添加事件?
Which calendar?
日历?
Is there a common API they all share?
他们有共同的API吗?
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.
不,只不过是Windows日历应用程序的“通用API”。有一些常见的数据格式(如iCalendar)和Internet协议(如CalDAV),但是没有通用的API。有些日历应用程序甚至不提供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.
如果您希望集成特定的日历应用程序,请联系他们的开发人员,确定他们是否提供API。例如,Mayra引用的Android开源项目中的Calendar应用程序没有提供文档化的和受支持的api。谷歌甚至明确地告诉开发人员不要使用Mayra引用教程中列出的技术。
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.
另一个选择是将事件添加到Internet日历中。例如,从Android开源项目向日历应用程序添加事件的最佳方式是通过适当的GData api将事件添加到用户的谷歌日历中。
UPDATE
更新
Android 4.0 (API Level 14) added a CalendarContract
ContentProvider
.
Android 4.0 (API级别14)增加了一个calendar arcontract内容提供程序。
#2
263
Try this in your code:
在你的代码中试试这个:
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);
Add permission..
添加权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"...>
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
...
</manifest>
#3
62
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
在代码中使用这个API。它将帮助你插入事件,事件与提醒,事件与会议可以被启用…这个api适用于2.1平台,并且适用于那些使用少于2.1而不是内容://com.android的用户。日历/事件使用内容:/ /日历事件
public static long pushAppointmentsToCalender(Activity curActivity, String title, String addInfo, String place, int status, long startDate, boolean needReminder, boolean needMailService) {
/***************** Event: note(without alert) *******************/
String eventUriString = "content://com.android.calendar/events";
ContentValues eventValues = new ContentValues();
eventValues.put("calendar_id", 1); // id, We need to choose from
// our mobile for primary
// its 1
eventValues.put("title", title);
eventValues.put("description", addInfo);
eventValues.put("eventLocation", place);
long endDate = startDate + 1000 * 60 * 60; // For next 1hr
eventValues.put("dtstart", startDate);
eventValues.put("dtend", endDate);
// values.put("allDay", 1); //If it is bithday alarm or such
// kind (which should remind me for whole day) 0 for false, 1
// for true
eventValues.put("eventStatus", status); // This information is
// sufficient for most
// entries tentative (0),
// confirmed (1) or canceled
// (2):
eventValues.put("eventTimezone", "UTC/GMT +2:00");
/*Comment below visibility and transparency column to avoid java.lang.IllegalArgumentException column visibility is invalid error */
/*eventValues.put("visibility", 3); // visibility to default (0),
// confidential (1), private
// (2), or public (3):
eventValues.put("transparency", 0); // You can control whether
// an event consumes time
// opaque (0) or transparent
// (1).
*/
eventValues.put("hasAlarm", 1); // 0 for false, 1 for true
Uri eventUri = curActivity.getApplicationContext().getContentResolver().insert(Uri.parse(eventUriString), eventValues);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
if (needReminder) {
/***************** Event: Reminder(with alert) Adding reminder to event *******************/
String reminderUriString = "content://com.android.calendar/reminders";
ContentValues reminderValues = new ContentValues();
reminderValues.put("event_id", eventID);
reminderValues.put("minutes", 5); // Default value of the
// system. Minutes is a
// integer
reminderValues.put("method", 1); // Alert Methods: Default(0),
// Alert(1), Email(2),
// SMS(3)
Uri reminderUri = curActivity.getApplicationContext().getContentResolver().insert(Uri.parse(reminderUriString), reminderValues);
}
/***************** Event: Meeting(without alert) Adding Attendies to the meeting *******************/
if (needMailService) {
String attendeuesesUriString = "content://com.android.calendar/attendees";
/********
* To add multiple attendees need to insert ContentValues multiple
* times
***********/
ContentValues attendeesValues = new ContentValues();
attendeesValues.put("event_id", eventID);
attendeesValues.put("attendeeName", "xxxxx"); // Attendees name
attendeesValues.put("attendeeEmail", "yyyy@gmail.com");// Attendee
// E
// mail
// id
attendeesValues.put("attendeeRelationship", 0); // Relationship_Attendee(1),
// Relationship_None(0),
// Organizer(2),
// Performer(3),
// Speaker(4)
attendeesValues.put("attendeeType", 0); // None(0), Optional(1),
// Required(2), Resource(3)
attendeesValues.put("attendeeStatus", 0); // NOne(0), Accepted(1),
// Decline(2),
// Invited(3),
// Tentative(4)
Uri attendeuesesUri = curActivity.getApplicationContext().getContentResolver().insert(Uri.parse(attendeuesesUriString), attendeesValues);
}
return eventID;
}
#4
48
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
我使用下面的代码,它解决了我的问题,在默认设备日历中添加事件在ICS中,在版本更少的ICS。
if (Build.VERSION.SDK_INT >= 14) {
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
.putExtra(Events.TITLE, "Yoga")
.putExtra(Events.DESCRIPTION, "Group class")
.putExtra(Events.EVENT_LOCATION, "The gym")
.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
.putExtra(Intent.EXTRA_EMAIL, "rowan@example.com,trevor@example.com");
startActivity(intent);
}
else {
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);
}
Hope it would help.....
希望这将有助于.....
#5
37
As of Android version 4.0 official APIs and intents are available to interact with the available calendar providers.
在Android版本4.0中,官方api和意图可以与可用的日历提供者交互。
#6
7
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.
谷歌日历是“本地”日历应用程序,据我所知,所有的手机都安装了它的版本,默认的SDK提供了一个版本。
You might check out this tutorial for working with it.
您可以查看本教程以了解如何使用它。
#7
6
Try this ,
试试这个,
Calendar beginTime = Calendar.getInstance();
beginTime.set(yearInt, monthInt - 1, dayInt, 7, 30);
ContentValues l_event = new ContentValues();
l_event.put("calendar_id", CalIds[0]);
l_event.put("title", "event");
l_event.put("description", "This is test event");
l_event.put("eventLocation", "School");
l_event.put("dtstart", beginTime.getTimeInMillis());
l_event.put("dtend", beginTime.getTimeInMillis());
l_event.put("allDay", 0);
l_event.put("rrule", "FREQ=YEARLY");
// status: 0~ tentative; 1~ confirmed; 2~ canceled
// l_event.put("eventStatus", 1);
l_event.put("eventTimezone", "India");
Uri l_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
l_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
l_eventUri = Uri.parse("content://calendar/events");
}
Uri l_uri = MainActivity.this.getContentResolver()
.insert(l_eventUri, l_event);
#8
6
Just in case if someone needs this for Xamarin in c#:
以防万一有人在c#中需要Xamarin:
Intent intent = new Intent(Intent.ActionInsert);
intent.SetData(Android.Provider.CalendarContract.Events.ContentUri);
intent.PutExtra(Android.Provider.CalendarContract.ExtraEventBeginTime, Utils.Tools.CurrentTimeMillis(game.Date));
intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.AllDay, false);
intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.EventLocation, "Location");
intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.Description, "Description");
intent.PutExtra(Android.Provider.CalendarContract.ExtraEventEndTime, Utils.Tools.CurrentTimeMillis(game.Date.AddHours(2)));
intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.Title, "Title");
StartActivity(intent);
Helper Functions:
辅助功能:
private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long CurrentTimeMillis(DateTime date)
{
return (long)(date.ToUniversalTime() - Jan1st1970).TotalMilliseconds;
}
#9
1
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 */
为e。gstring givenDateString = pojoModel.getDate()/* Format dd- mm - yyyyyyyyyyy hh:mm:ss */
use the following code to add an event with date and time to the calendar
使用以下代码将带有日期和时间的事件添加到日历中
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss").parse(givenDateString));
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", false);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime",cal.getTimeInMillis() + 60 * 60 * 1000);
intent.putExtra("title", " Test Title");
startActivity(intent);
#10
1
you have to add flag:
你必须添加标志:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
or you will cause error with:
否则,您将导致以下方面的错误:
startActivity()
from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
startActivity()从活动上下文外部要求FLAG_ACTIVITY_NEW_TASK
#1
16
how do I programmatically add an event to the user's calendar?
如何以编程方式向用户的日历添加事件?
Which calendar?
日历?
Is there a common API they all share?
他们有共同的API吗?
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.
不,只不过是Windows日历应用程序的“通用API”。有一些常见的数据格式(如iCalendar)和Internet协议(如CalDAV),但是没有通用的API。有些日历应用程序甚至不提供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.
如果您希望集成特定的日历应用程序,请联系他们的开发人员,确定他们是否提供API。例如,Mayra引用的Android开源项目中的Calendar应用程序没有提供文档化的和受支持的api。谷歌甚至明确地告诉开发人员不要使用Mayra引用教程中列出的技术。
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.
另一个选择是将事件添加到Internet日历中。例如,从Android开源项目向日历应用程序添加事件的最佳方式是通过适当的GData api将事件添加到用户的谷歌日历中。
UPDATE
更新
Android 4.0 (API Level 14) added a CalendarContract
ContentProvider
.
Android 4.0 (API级别14)增加了一个calendar arcontract内容提供程序。
#2
263
Try this in your code:
在你的代码中试试这个:
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);
Add permission..
添加权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"...>
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
...
</manifest>
#3
62
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
在代码中使用这个API。它将帮助你插入事件,事件与提醒,事件与会议可以被启用…这个api适用于2.1平台,并且适用于那些使用少于2.1而不是内容://com.android的用户。日历/事件使用内容:/ /日历事件
public static long pushAppointmentsToCalender(Activity curActivity, String title, String addInfo, String place, int status, long startDate, boolean needReminder, boolean needMailService) {
/***************** Event: note(without alert) *******************/
String eventUriString = "content://com.android.calendar/events";
ContentValues eventValues = new ContentValues();
eventValues.put("calendar_id", 1); // id, We need to choose from
// our mobile for primary
// its 1
eventValues.put("title", title);
eventValues.put("description", addInfo);
eventValues.put("eventLocation", place);
long endDate = startDate + 1000 * 60 * 60; // For next 1hr
eventValues.put("dtstart", startDate);
eventValues.put("dtend", endDate);
// values.put("allDay", 1); //If it is bithday alarm or such
// kind (which should remind me for whole day) 0 for false, 1
// for true
eventValues.put("eventStatus", status); // This information is
// sufficient for most
// entries tentative (0),
// confirmed (1) or canceled
// (2):
eventValues.put("eventTimezone", "UTC/GMT +2:00");
/*Comment below visibility and transparency column to avoid java.lang.IllegalArgumentException column visibility is invalid error */
/*eventValues.put("visibility", 3); // visibility to default (0),
// confidential (1), private
// (2), or public (3):
eventValues.put("transparency", 0); // You can control whether
// an event consumes time
// opaque (0) or transparent
// (1).
*/
eventValues.put("hasAlarm", 1); // 0 for false, 1 for true
Uri eventUri = curActivity.getApplicationContext().getContentResolver().insert(Uri.parse(eventUriString), eventValues);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
if (needReminder) {
/***************** Event: Reminder(with alert) Adding reminder to event *******************/
String reminderUriString = "content://com.android.calendar/reminders";
ContentValues reminderValues = new ContentValues();
reminderValues.put("event_id", eventID);
reminderValues.put("minutes", 5); // Default value of the
// system. Minutes is a
// integer
reminderValues.put("method", 1); // Alert Methods: Default(0),
// Alert(1), Email(2),
// SMS(3)
Uri reminderUri = curActivity.getApplicationContext().getContentResolver().insert(Uri.parse(reminderUriString), reminderValues);
}
/***************** Event: Meeting(without alert) Adding Attendies to the meeting *******************/
if (needMailService) {
String attendeuesesUriString = "content://com.android.calendar/attendees";
/********
* To add multiple attendees need to insert ContentValues multiple
* times
***********/
ContentValues attendeesValues = new ContentValues();
attendeesValues.put("event_id", eventID);
attendeesValues.put("attendeeName", "xxxxx"); // Attendees name
attendeesValues.put("attendeeEmail", "yyyy@gmail.com");// Attendee
// E
// mail
// id
attendeesValues.put("attendeeRelationship", 0); // Relationship_Attendee(1),
// Relationship_None(0),
// Organizer(2),
// Performer(3),
// Speaker(4)
attendeesValues.put("attendeeType", 0); // None(0), Optional(1),
// Required(2), Resource(3)
attendeesValues.put("attendeeStatus", 0); // NOne(0), Accepted(1),
// Decline(2),
// Invited(3),
// Tentative(4)
Uri attendeuesesUri = curActivity.getApplicationContext().getContentResolver().insert(Uri.parse(attendeuesesUriString), attendeesValues);
}
return eventID;
}
#4
48
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
我使用下面的代码,它解决了我的问题,在默认设备日历中添加事件在ICS中,在版本更少的ICS。
if (Build.VERSION.SDK_INT >= 14) {
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI)
.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
.putExtra(Events.TITLE, "Yoga")
.putExtra(Events.DESCRIPTION, "Group class")
.putExtra(Events.EVENT_LOCATION, "The gym")
.putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
.putExtra(Intent.EXTRA_EMAIL, "rowan@example.com,trevor@example.com");
startActivity(intent);
}
else {
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);
}
Hope it would help.....
希望这将有助于.....
#5
37
As of Android version 4.0 official APIs and intents are available to interact with the available calendar providers.
在Android版本4.0中,官方api和意图可以与可用的日历提供者交互。
#6
7
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.
谷歌日历是“本地”日历应用程序,据我所知,所有的手机都安装了它的版本,默认的SDK提供了一个版本。
You might check out this tutorial for working with it.
您可以查看本教程以了解如何使用它。
#7
6
Try this ,
试试这个,
Calendar beginTime = Calendar.getInstance();
beginTime.set(yearInt, monthInt - 1, dayInt, 7, 30);
ContentValues l_event = new ContentValues();
l_event.put("calendar_id", CalIds[0]);
l_event.put("title", "event");
l_event.put("description", "This is test event");
l_event.put("eventLocation", "School");
l_event.put("dtstart", beginTime.getTimeInMillis());
l_event.put("dtend", beginTime.getTimeInMillis());
l_event.put("allDay", 0);
l_event.put("rrule", "FREQ=YEARLY");
// status: 0~ tentative; 1~ confirmed; 2~ canceled
// l_event.put("eventStatus", 1);
l_event.put("eventTimezone", "India");
Uri l_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
l_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
l_eventUri = Uri.parse("content://calendar/events");
}
Uri l_uri = MainActivity.this.getContentResolver()
.insert(l_eventUri, l_event);
#8
6
Just in case if someone needs this for Xamarin in c#:
以防万一有人在c#中需要Xamarin:
Intent intent = new Intent(Intent.ActionInsert);
intent.SetData(Android.Provider.CalendarContract.Events.ContentUri);
intent.PutExtra(Android.Provider.CalendarContract.ExtraEventBeginTime, Utils.Tools.CurrentTimeMillis(game.Date));
intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.AllDay, false);
intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.EventLocation, "Location");
intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.Description, "Description");
intent.PutExtra(Android.Provider.CalendarContract.ExtraEventEndTime, Utils.Tools.CurrentTimeMillis(game.Date.AddHours(2)));
intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.Title, "Title");
StartActivity(intent);
Helper Functions:
辅助功能:
private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long CurrentTimeMillis(DateTime date)
{
return (long)(date.ToUniversalTime() - Jan1st1970).TotalMilliseconds;
}
#9
1
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 */
为e。gstring givenDateString = pojoModel.getDate()/* Format dd- mm - yyyyyyyyyyy hh:mm:ss */
use the following code to add an event with date and time to the calendar
使用以下代码将带有日期和时间的事件添加到日历中
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss").parse(givenDateString));
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", false);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime",cal.getTimeInMillis() + 60 * 60 * 1000);
intent.putExtra("title", " Test Title");
startActivity(intent);
#10
1
you have to add flag:
你必须添加标志:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
or you will cause error with:
否则,您将导致以下方面的错误:
startActivity()
from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
startActivity()从活动上下文外部要求FLAG_ACTIVITY_NEW_TASK