Android Alarmmanager Send Notification Every Time App Starts Instead Of Only Once
My app use an AlarmManager that start a BroadcastReceiver. The BroadcastReceiver generate a reminder notification to the user. This is the MainActivity that start the Alarm public
Solution 1:
The issue is you are setting the time to current day. So if you are opening your app after 14:00, your alarm manager will immediately fire.
You need to check whether the time is over 14:00 for the current day, if yes you need to change the date to the next day:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
int curHr = calendar.get(Calendar.HOUR_OF_DAY);
// Checking whether current hour is over 14if (curHr >= 14)
{
// Since current hour is over 14, setting the date to the next day
calendar.add(Calendar.DATE, 1);
}
calendar.set(Calendar.HOUR_OF_DAY, 14);
// Schedule alarm manager
Post a Comment for "Android Alarmmanager Send Notification Every Time App Starts Instead Of Only Once"