Reputation: 15734
I have an alarm that will not go off until specific hour and minute. However, once that time comes, it will go off everytime I start the app again. (When the app starts it resets and check for a new alarm).
For example, it will go off at 9:48 AM. Before 9:48 nothing happens as expected. But AFTER 9:48, it will keep going off everytime the app starts (The alarm is a simple status bar notification).
Here is code -- where did I go wrong?
// Alarm set here - this code is called each time app starts up
public void setAlarm() {
for (int i : AlarmDays) {
Calendar cal = Calendar.getInstance();
if (cal.get(Calendar.DAY_OF_MONTH) > i)
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, i);
cal.set(Calendar.HOUR, 9);
cal.set(Calendar.MINUTE, 53);
cal.set(Calendar.SECOND, 1);
String Name = AlarmNames.get(count);
count = 0 + 1;
Intent intent = new Intent(ManageDebts.this, TimeAlarm.class);
Bundle b = new Bundle();
b.putString("keyvalue", Name);
intent.putExtras(b);
pendingIntent = PendingIntent.getBroadcast(this, i,
intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent);
}
}
TimeAlarm.class
public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
String DebtName = intent.getStringExtra("keyvalue");
nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Payment Due: " + DebtName;
CharSequence message = "Update your Balance Now";
Intent notificationIntent = new Intent(context, ManageDebts.class);
notificationIntent.getExtras();
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
Notification notif = new Notification(R.drawable.icon, "Pay "
+ DebtName + " today!", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.defaults = Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100;
notif.ledOffMS = 100;
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
}
}
Upvotes: 0
Views: 424
Reputation: 1006554
Where do I set it to ignore any Alarms that are NOT in that minute, hour and second? before OR after?
In your setAlarm()
method. Either filter things out from AlarmDays
before you go in the loop, or compare cal.getTimeInMillis()
to System.currentTimeMillis()
to see if it is in the past, or something like that.
Upvotes: 1