我有一个显示特殊日子的应用程序。我想将它们整合到日历中。
事件是静态的,它们不会改变,所以我不必经常更新日历。
我首先想到创建本地日历并添加事件,但新的 android 版本(从 2.3 开始?)似乎不支持;要实现,我必须创建一个日历提供程序。
我在github上看到这个项目:https://github.com/dschuermann/birthday-adapter .这很复杂;它的主要用途是将联系人的生日添加到新日历中。
有很多代码,其中很多我认为我不需要。我真的需要注册到 android 的客户经理才能集成 Calendar Provider 吗?我只需要一个新日历来记录我的 Activity ...
使用用户的默认日历并在其中添加所有事件是否更容易?我可以在描述中添加一些标识符,以便能够在用户不需要时删除事件。
感谢任何提示、教程或进一步阅读。
羽衣甘蓝
最佳答案
您可以通过 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;
}
欲了解更多信息,请访问 http://developer.android.com/reference/java/util/Calendar.html
关于java - Android - 使用事件创建自定义日历,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27449743/