so I'm having this problem that whenever I set an Alarm it just sends the notification even though the time doesn't match. I'm using this class to set the alarm and cancel the alarm.
package com.example.home.quoteapp.notification;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import com.example.home.quoteapp.readers.NotificationsReader;
import java.util.Calendar;
public class AlarmActivater {
private final Context context;
public AlarmActivater(Context context) {
this.context = context;
}
public void setAlarm() {
cancelAlarm();
if (!NotificationsReader.getNotifications().isEmpty()) {
SharedPreferences prefs = context.getSharedPreferences("time", Context.MODE_PRIVATE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, prefs.getInt("hour", 1));
calendar.set(Calendar.MINUTE, prefs.getInt("minute", 01));
calendar.set(Calendar.SECOND, 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, new Intent(context, AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
private boolean isActive() {
return (PendingIntent.getBroadcast(context, 0,
new Intent(context, AlarmReceiver.class),
PendingIntent.FLAG_NO_CREATE) != null);
}
private void cancelAlarm() {
if (!NotificationsReader.getNotifications().isEmpty() && isActive()) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, 0, myIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);
}
}
}
I call the setAlarm method on startup of the app and when changing the time in a time picker. The prefs.getInt... stuff is correct I have tested that.