Android:并非已保存所有重复发生的事件实例 [英] Android: Not All Instances of Recurring Event Saved
问题描述
我目前正在创建自定义日历应用程序,并且一直在努力保存重复事件.例如,如果我将事件设置为每天100天,那么我只会看到大约38个实例保存到实例数据库中.不知道为什么会这样.
I am currently working on creating a custom calendar application and have been struggling with saving a recurring event. If for example I set the event to occur daily for 100 days I am only seeing about 38 instances saved to the instances database. Not sure why this is happening.
这是添加/更新事件的代码
Here is the code that adds/updates an event
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方法之后,我立即调用另一个方法来列出实例
Directly after I call the addUpdate method I call another method to list the instances
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天发生的事件的属性示例:
Here is an example of the properties of an event I tried to save that occurred daily for 100 days:
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文件中的事件信息:
Here is the event information from the vcd file:
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
无法弄清楚为什么它只保存了一部分实例.请帮忙!!!如果您需要查看其他信息,请告诉我.
Can't figure out why it is only saving a fraction of the instances. Please Help!!! Please let me know if you need to see any other information.
谢谢,亚伦
推荐答案
我不久前就发现了...抱歉,我没有更早添加它.问题在于在搜索事件时传递Long.min和Long.max.我通过每月一次加载事件来解决了这些问题.
I figured it out a while ago...sorry I didn't add it earlier. The issue was with passing in Long.min and Long.max when searching through the events. I solved the issues by loading events one month at a time.
这篇关于Android:并非已保存所有重复发生的事件实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!