我目前正致力于创建自定义日历应用程序,并且一直在努力保存定期活动。例如,如果我将事件设置为每天发生100天,我只看到大约38个实例保存到实例数据库。不知道为什么会这样。
以下是添加/更新活动的代码
public Event addUpdateCalendarEvent(Context ctx, Event event) throws InterruptedException {
ContentResolver contentResolver = ctx.getContentResolver();
ContentValues calEvent = new ContentValues();
calEvent.put(Events.CALENDAR_ID, event.getCalendarId());
calEvent.put(Events.TITLE, event.getEventName());
calEvent.put(Events.DTSTART, event.getDateFrom().getTimeInMillis());
calEvent.put(Events.EVENT_TIMEZONE,event.getDateFrom().getTimeZone().getID());
if(event.isAllDay()) {
calEvent.put(Events.ALL_DAY, 1);
}
if(null != event.getRrule()) {
String rrule = RecurrenceRuleConverter.toString(event.getRrule());
Log.d("rrule: ", rrule);
calEvent.put(Events.RRULE, rrule);
}
if(null != event.getDuration()) {
calEvent.put(Events.DURATION, event.getDuration());
}
if(null != event.getDateTo()) {
calEvent.put(Events.DTEND, event.getDateTo().getTimeInMillis());
}
Uri uri = null;
try {
if(null != event.getEventId()) {
uri = ContentUris.withAppendedId(Events.CONTENT_URI, event.getEventId());
contentResolver.update(uri, calEvent, null, null);
}
else {
uri = contentResolver.insert(Events.CONTENT_URI, calEvent);
}
}
finally {
event.setEventId(Integer.parseInt(uri.getLastPathSegment()));
}
return event;
}
在我调用addUpdate方法之后,我直接调用另一个方法来列出实例
public List<Event> getEventInstances(Context context, Integer calendarId, Integer eventId) {
// Projection array. Creating indices for this array instead of doing
// dynamic lookups improves performance.
final String[] EVENT_PROJECTION = new String[] {
CalendarContract.Instances.CALENDAR_ID,
CalendarContract.Instances._ID,
CalendarContract.Instances.BEGIN,
CalendarContract.Instances.END,
CalendarContract.Instances.TITLE,
CalendarContract.Instances.RRULE,
CalendarContract.Instances.EVENT_ID,
CalendarContract.Instances.EVENT_TIMEZONE,
CalendarContract.Instances.ALL_DAY
};
// Run query
Cursor cur = null;
ContentResolver cr = context.getContentResolver();
Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, Long.MIN_VALUE);
ContentUris.appendId(builder, Long.MAX_VALUE);
Uri uri = builder.build();
List<Event> events = new ArrayList<Event>();
String selection = "((" + Instances.CALENDAR_ID + " = ?) AND (" + Instances.EVENT_ID + " = ?))";
String[] selectionArgs = new String[] {String.valueOf(calendarId),String.valueOf(eventId)};
try {
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
while (cur.moveToNext()) {
Event event = new Event();
event.setCalendarId(cur.getInt(0));
java.util.Calendar calFrom = java.util.Calendar.getInstance();
calFrom.setTimeInMillis(Long.valueOf(cur.getLong(2)));
java.util.Calendar calTo = java.util.Calendar.getInstance();
calTo.setTimeInMillis(Long.valueOf(Long.valueOf(cur.getLong(3))));
event.setDateFrom(calFrom);
event.setDateTo(calTo);
event.setTitle(cur.getString(4));
event.setEventId(cur.getInt(6));
RecurrenceRule rule = new RecurrenceRule();
rule.setRuleAsText(cur.getString(5));
event.setRrule(rule);
event.setAllDay(cur.getInt(8) == 1);
Log.d("Event:", event.getTitle() + " CalendarID: " + event.getCalendarId() + " EventId: " + event.getEventId() + " " + DateUtil.dateTimeFormat.format(calFrom.getTime()) + " to " + DateUtil.dateTimeFormat.format(calTo.getTime()) + " TimeZone: " + cur.getString(7));
events.add(event);
}
}
finally {
cur.close();
sortListEventsByDate(events);
Log.d("GetEventInstances Size", String.valueOf(events.size()));
}
return events;
}
以下是我尝试保存的事件属性的示例,该事件每天发生100天:
event Event (id=830026469328)
calendarId Integer (id=830015046320)
dateFrom GregorianCalendar (id=830027217712)
dateTo null
duration "P1H" (id=830029516016)
eventId Integer (id=830029520232)
eventName "ginos pizza" (id=830044532096)
eventTimezone "Central Standard Time" (id=830024050504)
isAllDay false
rdate null
rrule ruleAsText "FREQ=DAILY;COUNT=100" (id=830029519256)
以下是vcd文件中的事件信息:
BEGIN:VCALENDAR
VERSION:1.0
PRODID:vCal ID default
TZ:-05:00
BEGIN:VEVENT
UID:content://com.android.calendar/events/1187
DTEND:20130810T040000Z
DTSTART:20130810T030000Z
DUE:P3600S
COMPLETED:20131117T050000Z
RRULE:FREQ=DAILY;COUNT=100
SUMMARY;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:=67=69=6E=6F=73=20=70=69=7A=7A=61
STATUS:TENTATIVE
END:VEVENT
END:VCALENDAR
无法弄清楚为什么它只保存了一小部分实例。请帮忙!!!如果您需要查看任何其他信息,请与我们联系。
谢谢你, 亚伦
答案 0 :(得分:0)
我刚才想出来...抱歉我之前没有添加它。问题在于在搜索事件时传递Long.min和Long.max。我通过一次加载一个月来解决问题。