最近在使用系统日历提醒功能,也是github上找了个demo跑了下,地址:https://github.com/kylechandev/CalendarProviderManager。
开始设置一些简单提醒,日、周、月 都没什么问题,跑的挺溜,但是在设置一个长周期(比如结束时间设置到2年后)的时候,出现了匪夷所思的问题:日历只展示了最近三个月的,但是断点看日历设置 insert是成功的呀。
设置提醒的代码,也是上述demo的部分代码,拷贝使用的,只有rrule的规则我自己改了下
public static int addCalendarEvent(Context context, CalendarEvent calendarEvent) {
checkContextNull(context);
long calID = obtainCalendarAccountID(context);
Uri uri1 = CalendarContract.Events.CONTENT_URI;
Uri eventUri;
Uri uri2 = CalendarContract.Reminders.CONTENT_URI;
Uri reminderUri;
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.CALENDAR_ID, calID);
setupEvent(calendarEvent, event);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (PackageManager.PERMISSION_GRANTED == context.checkSelfPermission(
"android.permission.WRITE_CALENDAR")) {
eventUri = context.getContentResolver().insert(uri1, event);
} else {
return -2;
}
} else {
eventUri = context.getContentResolver().insert(uri1, event);
}
if (null == eventUri) {
return -1;
}
long eventID = ContentUris.parseId(eventUri);
ContentValues reminders = new ContentValues();
reminders.put(CalendarContract.Reminders.EVENT_ID, eventID);
reminders.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
reminderUri = context.getContentResolver().insert(uri2, reminders);
if (null == reminderUri) {
return -1;
}
return 0;
}
private static void setupEvent(CalendarEvent calendarEvent, ContentValues event) {
event.put(CalendarContract.Events.DTSTART, calendarEvent.getStart());
event.put(CalendarContract.Events.DTEND, calendarEvent.getEnd());
event.put(CalendarContract.Events.TITLE, calendarEvent.getTitle());
event.put(CalendarContract.Events.DESCRIPTION, calendarEvent.getDescription());
event.put(CalendarContract.Events.EVENT_LOCATION, calendarEvent.getEventLocation());
event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
event.put(CalendarContract.Events.ACCESS_LEVEL, CalendarContract.Events.ACCESS_DEFAULT);
event.put(CalendarContract.Events.STATUS, 0);
event.put(CalendarContract.Events.HAS_ALARM, 1);
event.put(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY);
final String repeatRRule = getFullRRuleForRRule(calendarEvent);
if (!TextUtils.isEmpty(repeatRRule)) {
event.put(CalendarContract.Events.RRULE, repeatRRule);
}
}
处理完的rrule规则是(每个月20号提醒,周期重复,一直到2023年4月20日),我的Dtstart时间给的就是当前时间 取time zone时间:
FREQ=MONTHLY;INTERVAL=1;UNTIL=20240420T000000Z;BYMONTHDAY=20
运行后,设置的结果只有近三个月有效,后续提醒全无
尝试找些出路:
首先,翻源码翻文档,找些日历属性看能不能起作用,举例 比如 在设置日历账户的时候有个属性:
/**
* The maximum number of reminders allowed for an event. Column name.
* <P>Type: INTEGER</P>
*/
public static final String MAX_REMINDERS = "maxReminders";
以为这个值限制了日历提醒数,因为最开始我理解这个是当日提醒数最多可以设置多少个(如,8:00,10:00,12:00这种),以为这个导致的,尝试后发现不是,当然这中间以为日历用户已经创建了,设置更改不生效等等,去删除日历用户,重头来,也是无效的@_@
其次,想着rrule本来除了使用UNTIL,还可以 设置COUNT,那我就用COUNT来,总归能兜底的喽:
FREQ=MONTHLY;INTERVAL=1;COUNT=100;BYMONTHDAY=20
结果也是完全没按照我的思维走,也是只展示3个月的提醒,后续的全没了,很难受啊!
期间也尝试了零零散散的方式方法,但都没有效果,也出现过能设置到2024年的数据,以为破了,结果手动删除,再尝试下,又被打回原形,实在摸不着头脑了,已经没什么解题思路了,不知道有没有兄弟姐妹遇到过类似问题,给提点提点 万分感谢