【发布时间】:2018-03-29 15:51:03
【问题描述】:
我正在尝试在某些时间段每天发送通知。它在我创建警报的那一天工作。但是接下来的几天我没有收到任何通知,我的 ReminderClass 也没有触发。我到处搜索,但无法解决问题。
我尝试使用setInexactRepeating 而不是setRepeating,但我仍然面临这个问题。
这是我的代码:
public void setRecurringAlarmsForPeriod(Integer sh, Integer sm, Integer eh, Integer em, Integer interval)
{
this.interval = interval;
startHour = sh;
startMinute = sm;
endHour = eh;
endMinute = em;
reminderCount = 0;
Integer currentStartHour = startHour;
Integer currentStartMinute = startMinute;
while (currentStartHour < endHour || (currentStartHour.equals(endHour) && currentStartMinute <= endMinute))
{
setRecurringAlarm(currentStartHour, currentStartMinute, reminderCount);
reminderCount++;
currentStartMinute += interval;
if (currentStartMinute >= 60)
{
currentStartMinute -= 60;
currentStartHour++;
}
}
savePersistent();
}
在下面的代码中,我通过alarmmanager 在一天中的某些时间段创建警报
private void setRecurringAlarm(Integer hour, Integer minute, Integer index)
{
Calendar notificationTime = Calendar.getInstance();
notificationTime.set(Calendar.HOUR_OF_DAY, hour);
notificationTime.set(Calendar.MINUTE, minute);
notificationTime.set(Calendar.SECOND, 0);
notificationTime.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(MainActivity.activity.getApplicationContext(), WaterReminderReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.activity.getApplicationContext(), index, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) MainActivity.activity.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null)
{
try {
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, notificationTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
catch (Exception e)
{
Crashlytics.log(Log.ASSERT, "exceptipon :" , e.toString());
}
Crashlytics.log(Log.ASSERT, "Alarm Set For", hour + " " + minute);
Crashlytics.log(Log.ASSERT, "Alarm Date", notificationTime.getTime().toString());
}
else
{
Crashlytics.log(Log.ASSERT, "ALARMS", "WAS NULL");
}
}
【问题讨论】:
-
我认为它与“PendingIntent.FLAG_ONE_SHOT”有关。我将其更改为“0”,然后再试一次。
标签: android notifications alarmmanager