java Android - 使用事件创建自定义日历

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27449743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:49:24  来源:igfitidea点击:

Android - Create Custom Calendar with Event

javaandroideventscalendarprovider

提问by metinkale38

I have an App which shows special Days. I want to integrate them into the calendar.

我有一个显示特殊日子的应用程序。我想将它们整合到日历中。

The events are static, they don't change, so I don't have to update the calendar very often.

事件是静态的,不会改变,所以我不必经常更新日历。

I first thought of creating a local calendar and add the events, but new android versions (since 2.3?) seem not to support that; to implement I would have to create a Calendar Provider.

我首先想到创建一个本地日历并添加事件,但新的 android 版本(从 2.3 开始?)似乎不支持;要实现,我必须创建一个日历提供程序。

I saw this project on github: https://github.com/dschuermann/birthday-adapter. It is very complicated; its main use is adding the birthdays of the contacts to a new calendar.

我在 github 上看到了这个项目:https: //github.com/dschuermann/birthday-adapter。它非常复杂;它的主要用途是将联系人的生日添加到新日历中。

There is lots of code, much of which I don't think I need. Do I really need to register to android's account manager to integrate a Calendar Provider? I just need a new Calendar with my event...

有很多代码,其中很多我认为我不需要。我真的需要注册到 android 的客户经理才能集成日历提供程序吗?我只需要一个包含我的活动的新日历...

Would it be easier to take the user's default Calendar and add all the Events there? I could add some identifiers to the description, to be able to remove the events if the user doesn't want them.

使用用户的默认日历并将所有事件添加到那里会更容易吗?我可以在描述中添加一些标识符,以便在用户不想要它们时删除事件。

Any tips, tutorials, or further readings are appreciated.

感谢任何提示、教程或进一步阅读。

Metin Kale

羽衣甘蓝

采纳答案by Andriy Dychka

You can create events in your device calendar via Intent. I think it could be useful for you.

您可以通过 Intent 在您的设备日历中创建事件。我认为它可能对你有用。

public long addEventToCalender(ContentResolver cr, String title, String addInfo, String place, int status,
                                      long startDate, boolean isRemind,long endDate) {
    String eventUriStr = "content://com.android.calendar/events";
    ContentValues event = new ContentValues();
    // id, We need to choose from our mobile for primary its 1
    event.put("calendar_id", 1);
    event.put("title", title);
    event.put("description", addInfo);
    event.put("eventLocation", place);
    event.put("eventTimezone", "UTC/GMT +2:00");

    // For next 1hr
    event.put("dtstart", startDate);
    event.put("dtend", endDate);
    //If it is bithday alarm or such kind (which should remind me for whole day) 0 for false, 1 for true
    // values.put("allDay", 1);
    //  event.put("eventStatus", status);
    event.put("hasAlarm", 1);

    Uri eventUri = cr.insert(Uri.parse(eventUriStr), event);
    long eventID = Long.parseLong(eventUri.getLastPathSegment());

    if (isRemind) {
        String reminderUriString = "content://com.android.calendar/reminders";
        ContentValues reminderValues = new ContentValues();
        reminderValues.put("event_id", eventID);
        // Default value of the system. Minutes is a integer
        reminderValues.put("minutes", 5);
        // Alert Methods: Default(0), Alert(1), Email(2), SMS(3)
        reminderValues.put("method", 1);
        cr.insert(Uri.parse(reminderUriString), reminderValues); //Uri reminderUri =
    }
    return eventID;
}

For more information visit http://developer.android.com/reference/java/util/Calendar.html

有关更多信息,请访问http://developer.android.com/reference/java/util/Calendar.html

回答by Cristian M.G

In this reply you how to get contacts and birthdays. Android Applicaiton - How to get birthday of a contact

在此回复中,您将了解如何获取联系人和生日。 Android Applicaiton - 如何获取联系人的生日

and this library will offer you a powerful and flexible schedule so you can use Caldroid Library

这个库将为您提供强大而灵活的时间表,以便您可以使用 Caldroid 库