Android - Create Custom Calendar with Event

我怕爱的太早我们不能终老 提交于 2019-12-06 03:18:02

问题


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.

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.

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...

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


回答1:


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

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




回答2:


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

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



来源:https://stackoverflow.com/questions/27449743/android-create-custom-calendar-with-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!