Я знаю, что этот вопрос задавался несколько раз, но я действительно не понимаю, почему мой код не работает. Я основывался на множестве примеров (из ответов stackoverflow), чтобы написать свой код, и мне нужна помощь, чтобы понять, где проблема.Уведомление и AlarmManager
Контекст
Пользователь может, корыто предпочтения, выбрать день, чтобы быть извещать. Я получаю номер от 2 (понедельника) 3 ... 4 ... 5 ... до 1 (воскресенье).
public Calendar getCalendar(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK,getDayForNotification());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.AM);
return calendar;
}
Затем я хочу повторить будильник один раз в неделю в тот день, который выбрал пользователь.
public void setNotificationDate() {
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, myAlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
DateNotificationPreference dateNotificationPreference = new DateNotificationPreference(this);
alarmMgr.setRepeating(AlarmManager.RTC, dateNotificationPreference.getCalendar().getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, alarmIntent);
}
Мой радиовещательный приемник
public class myAlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent service1 = new Intent(context, AlarmService.class);
context.startService(service1);
}
}
В моей службе, у меня есть Juste один метод:
public void createNotification(){
final NotificationManager mNotification = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Intent launchNotifiactionIntent = new Intent(this, mySilhouette.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, launchNotifiactionIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.icon_notification)
.setContentTitle("my notification")
.setContentText("my first notification")
.setContentIntent(pendingIntent);
mNotification.notify(0, builder.build());
}
Когда я выбираю текущий день, уведомление появляется в хорошее время, но например, когда я перезапустил приложение в 8:05 утра, я снова получил уведомление. После этого, если я выберу еще один день и вернусь к мероприятию, я снова получаю уведомление ... Есть что-то странное, и я не знаю, в чем проблема. Мне нужна ваша помощь :)
Большое спасибо!
Kmel
Вы когда-нибудь выясняли, как проверить, имеете ли вы дело с прошлыми или будущими датами? – Eenvincible
Я попробовал несколько решений, но мне это не удалось. Даже если я делаю чек, уведомление отправляется несколько раз ... – Kmel