日历提醒在时间到了时不发出通知
calendar reminder not gives notifiaction when it is time
我在我的应用程序中成功创建了带有提醒的日历事件,它在 AVD 中运行良好,但是当我自己安装 apk 时 phone 提醒不起作用。
我确定我已经授权日历在我直接通过系统日历创建事件时发出通知,提醒有效。
这是我创建日历事件的代码:
public static long addCalendarEvent(Context context, String title, String description, long startTime, int repeatTime) {
if (context == null) {
return 0;
}
int calId = checkAndAddCalendarAccount(context);
if (calId < 0) {
return 0;
}
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE, title);
event.put(CalendarContract.Events.DESCRIPTION, description);
event.put(CalendarContract.Events.CALENDAR_ID, calId);
event.put(CalendarContract.Events.DTSTART, startTime);
long endTime = getNowTime(new Date(startTime)) + 86400000L;
event.put(CalendarContract.Events.DTEND, endTime);
event.put(CalendarContract.Events.HAS_ALARM, 1);
event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
String daily = "FREQ=DAILY;COUNT=" + repeatTime;
event.put(CalendarContract.Events.RRULE, daily);
Uri newEvent = context.getContentResolver().insert(Uri.parse(CALENDER_EVENT_URL), event);
if (newEvent == null) {
return 0;
}
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = context.getContentResolver().insert(Uri.parse(CALENDER_REMINDER_URL), values);
if (uri == null) {
return 0;
}
return ContentUris.parseId(newEvent);
}
好的,现在我终于知道出了什么问题。字段 CalendarContract.Reminders.MINUTES 必须设置为 0,因为它的默认值为 -1
在代码中看起来像这样:
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
values.put(CalendarContract.Reminders.MINUTES,0);
我在我的应用程序中成功创建了带有提醒的日历事件,它在 AVD 中运行良好,但是当我自己安装 apk 时 phone 提醒不起作用。 我确定我已经授权日历在我直接通过系统日历创建事件时发出通知,提醒有效。 这是我创建日历事件的代码:
public static long addCalendarEvent(Context context, String title, String description, long startTime, int repeatTime) {
if (context == null) {
return 0;
}
int calId = checkAndAddCalendarAccount(context);
if (calId < 0) {
return 0;
}
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.TITLE, title);
event.put(CalendarContract.Events.DESCRIPTION, description);
event.put(CalendarContract.Events.CALENDAR_ID, calId);
event.put(CalendarContract.Events.DTSTART, startTime);
long endTime = getNowTime(new Date(startTime)) + 86400000L;
event.put(CalendarContract.Events.DTEND, endTime);
event.put(CalendarContract.Events.HAS_ALARM, 1);
event.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
String daily = "FREQ=DAILY;COUNT=" + repeatTime;
event.put(CalendarContract.Events.RRULE, daily);
Uri newEvent = context.getContentResolver().insert(Uri.parse(CALENDER_EVENT_URL), event);
if (newEvent == null) {
return 0;
}
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
Uri uri = context.getContentResolver().insert(Uri.parse(CALENDER_REMINDER_URL), values);
if (uri == null) {
return 0;
}
return ContentUris.parseId(newEvent);
}
好的,现在我终于知道出了什么问题。字段 CalendarContract.Reminders.MINUTES 必须设置为 0,因为它的默认值为 -1
在代码中看起来像这样:
ContentValues values = new ContentValues();
values.put(CalendarContract.Reminders.EVENT_ID, ContentUris.parseId(newEvent));
values.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);
values.put(CalendarContract.Reminders.MINUTES,0);