Android - 使用Event创建自定义日历

met*_*e38 5 java provider events android calendar

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

事件是静态的,它们不会改变,因此我不必经常更新日历.

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

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

有很多代码,其中很多我认为我不需要.我真的需要注册到Android的客户经理来集成日历提供商吗?我只需要一个新的日历与我的活动......

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

任何提示,教程或进一步阅读表示赞赏.

Metin Kale

小智 5

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

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