如何在Android中添加日历活动?

Pet*_*son 152 android calendar

我刚刚开始使用Android,今天在项目会议上有人说Android没有原生日历应用程序,所以用户只需使用他们喜欢的任何日历应用程序.

这是真的,如果是这样,我如何以编程方式将事件添加到用户的日历中?他们共有一个共同的API吗?

对于它的价值,我们可能会瞄准Android 2.x.

ori*_*rel 284

在您的代码中尝试此操作:

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);
Run Code Online (Sandbox Code Playgroud)

  • 您应该使用.setData(CalendarContract.Events.CONTENT_URI)而不是setType,因为使用setType(string)它会在某些设备上崩溃! (10认同)
  • 您的代码会告诉您的应用将数据发送到另一个(日历)应用,然后该应用会使用该数据添加新的日历事件.您的应用程序不会写入也不会读取日历数据,因此您不需要清单中的任何权限. (7认同)
  • 我非常喜欢这个解决方案.主要是因为它促使用户选择他/她自己的日历(如果他有多个). (6认同)
  • 这里有完整的教程:http://code.tutsplus.com/tutorials/android-essentials-adding-events-to-the-users-calendar--mobile-8363最好使用常量来避免拼写错误;) (4认同)
  • 如何使这兼容Android 2.2? (2认同)

Pra*_*eep 65

在您的代码中使用此API ..它将帮助您插入事件,带有提醒的事件和可以启用会议的事件...此api适用于平台2.1及更高版本使用少于2.1而不是content:// com的用户.android.calendar/events使用content:// calendar/events

 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;

}
Run Code Online (Sandbox Code Playgroud)

  • 我添加了`event.put("eventTimezone","UTC/GMT +2:00")`并删除了`event.put("visibility",3)`和`event.put("transparency",0)`和它运作良好 (4认同)
  • 我在`Uri reminderUri = curActivity.getApplicationContext().getContentResolver().insert(Uri.parse(reminderUriString),reminderValues);```android.database.sqlite.SQLiteException`. (2认同)

Mil*_*kla 55

我使用下面的代码,它解决了我的问题,在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);
        }
Run Code Online (Sandbox Code Playgroud)

希望它会有所帮助.....


tob*_*sen 39

从Android 4.0版开始,官方API和意图可用于与可用的日历提供程序进行交互.

  • 真有用的评论,不能强调这一个:) (4认同)

Com*_*are 18

如何以编程方式将事件添加到用户的日历?

哪个日历?

他们共有一个共同的API吗?

不,只不过是Windows日历应用程序存在"共享的API".存在一些常见的数据格式(例如,iCalendar)和因特网协议(例如,CalDAV),但是没有通用的API.某些日历应用甚至不提供API.

如果您希望集成特定日历应用程序,请与其开发人员联系并确定他们是否提供API.因此,例如,Mayra引用的Android开源项目中的日历应用程序不提供任何文档化和支持的API.谷歌甚至明确告诉开发人员不要使用 Mayra引用的教程中概述的技术.

另一个选项是您可以将事件添加到相关的Internet日历中.例如,事件添加到从Android开源项目的日历应用程序的最佳方式是通过适当的GData的API将事件添加到用户的谷歌日历.


UPDATE

Android 4.0(API Level 14)添加了一个CalendarContract ContentProvider.

  • 我不知道你为什么一直把它与Windows进行比较 - 我认识的每个人都用他们的手机来安排他们的日程安排和约会 - 我不知道有谁在他们的电脑上做这件事 - 也许这是一代人的事情.但我觉得Android无法做到这一点.谢谢. (22认同)
  • 我们不希望定位特定日历 - 我们只是希望能够将事件添加到用户正在使用的任何日历中,这只是为了方便用户.我们的应用程序为组织专业会议和会议的网站提供界面.因此,如果有人注册一个,那么将它放入他们的日历中会很不错. (5认同)
  • @Peter Nelson:我一直把它与Windows进行比较,因为人们做出同样的假设.许多开发人员认为他们可以"在Windows上向用户使用的任何日历添加事件",因为他们认为每个人都使用Outlook,因此只有一个"无论日历". (5认同)
  • @Peter Nelson:"我们不希望定位特定的日历 - 我们只希望能够将事件添加到用户正在使用的任何日历中,这对用户来说是方便的." - 你不能在Android上做到这一点,只能在Windows上做到这一点.在这两个平台上,您都不知道"用户正在使用的日历",并且在这两个平台上都没有用于处理此类日历应用程序的通用API. (3认同)
  • @Yar:“主要的 Android 手机日历,任何 Android 手机都有。” -- 除了所有没有该应用程序的 Android 设备。例如,许多 HTC 设备没有该应用程序,而是有自己的日历应用程序,例如我左边六英寸的 HTC DROID Incredible。欢迎设备制造商用自己的实现来替换日历应用程序或任何其他应用程序。 (2认同)

Che*_*mon 7

Google日历是"原生"日历应用.据我所知,所有手机都安装了一个版本,默认SDK提供了一个版本.

您可以查看本教程以了解它.


Aru*_*ney 7

试试这个 ,

   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);
Run Code Online (Sandbox Code Playgroud)


dot*_*tsa 7

万一有人在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);
Run Code Online (Sandbox Code Playgroud)

助手功能:

    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;
    }
Run Code Online (Sandbox Code Playgroud)