我在搜索引擎优化和各种论坛上进行了广泛搜索,但我无法解决这个问题。我在Android日历中生成了一个重新安装事件。有时候它完美无瑕地工作,有时它只为即将到来的4-5周设置它然后停止。我到底错在了什么?
这是我在日历中添加事件的功能
private void addToCalendar(int DAY_WEEK, String TITLE, String DESC, String LOC, int hour_s, int min_s, int hour_e, int min_e) {
Uri uri = Calendars.CONTENT_URI;
String[] projection = new String[]{
Calendars._ID,
Calendars.ACCOUNT_NAME,
Calendars.CALENDAR_DISPLAY_NAME,
Calendars.NAME,
Calendars.CALENDAR_COLOR
};
// Construct event details
long startMillis = 0;
long endMillis = 0;
int year = Calendar.getInstance().get(Calendar.YEAR);
int month = Calendar.getInstance().get(Calendar.MONTH);
Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DAY_OF_WEEK);
int daym = now.get(Calendar.DAY_OF_MONTH);
int start_day = 0;
if (day != DAY_WEEK) {
start_day = daym - (day - DAY_WEEK);
//Toast.makeText(this, Integer.toString(start_day), Toast.LENGTH_LONG).show();
} else {
start_day = daym;
}
Calendar beginTime = Calendar.getInstance();
beginTime.set(year, month, start_day, hour_s, min_s);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(year, month, start_day, hour_e, min_e);
endMillis = endTime.getTimeInMillis();
Calendar endRRule = Calendar.getInstance();
endRRule.add(Calendar.MONTH, 3);
long endRRuleMillis = endRRule.getTimeInMillis();
// Insert Event
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
TimeZone timeZone = TimeZone.getDefault();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.EVENT_TIMEZONE, timeZone.getID());
values.put(Events.RRULE, "FREQ=WEEKLY;");
values.put(Events.HAS_ALARM, 1);
values.put(Events.TITLE, TITLE);
values.put(Events.DESCRIPTION, DESC);
values.put(Events.EVENT_LOCATION, LOC);
values.put(Events.CALENDAR_ID, 1);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CALENDAR}, 101);
}
uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
// Retrieve ID for new event
String eventID = uri.getLastPathSegment();
ContentValues reminders = new ContentValues();
reminders.put(CalendarContract.Reminders.EVENT_ID, eventID);
reminders.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
reminders.put(CalendarContract.Reminders.MINUTES, 15);
Uri uri2 = cr.insert(CalendarContract.Reminders.CONTENT_URI, reminders);
}
我从另一个缩短的函数调用它以提高代码可读性
此循环遍历本地数据库的所有SQL结果
while (c.moveToNext()) {
Log.d("COURSE NAME", c.getString(c.getColumnIndex("course_name")));
final ListModelSchedules sched = new ListModelSchedules();
...
if (setDay != 0 && title != null && desc != null && room != null && setStartTime != 0 && setEndTime != 0)
{
addToCalendar(setDay, title, desc, room, setStartTime, 0, setEndTime, 0);
}
}
它正确地添加了事件,但它不是无限期地重复,它有时会在4-5周后停止。我在这做错了什么?如果我设置RRule的UNTIL =时间戳,它会中断并且不会向日历添加任何内容。任何帮助将不胜感激。
编辑:我刚刚注意到它实际上已经为所有日子添加了所有这些事件。但是,在您手动选择日历应用程序内的刷新选项后,它会显示。这是可以解决的问题还是日历应用程序的设计方式?