the purpose I'm writing this code is to make sure that my apps will notify the user on a certain time and the apps will be repeating the same notify every day starting at 7 AM.
However, I tried to use this function (as shown below). But it seems like it won't work.
calender.add(DATE, 1)
Actually, I want to generate an automatic notification. The medication information and the number of daily repeated is user input.
So the things that I want to do is for the if statement, it would compare whether the current hour is between 7 AM - 8 PM.
For the else if, the statement is to compare either the current hour is after 8 PM or not, if yes then the calendar will add another 1 day. So that the apps will pop up the same notification the next day at 7 AM.
And for else statement is to check if the current hour is before 7 AM or not. If yes, apps will pop up the notification right after the current hour equals to 7 AM
But the else if statement didn't work as I expected. I mean if I set the notification right now, at 10 PM, the apps won't notify me on the next day (at 7 AM).
So, I would like to ask if any of you could help me to fix my code as shown below. The if and else segment just works fine. But the else if statement seems like won't work.
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
Calendar now = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//repeat on 7am
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 0);
//check current hour
int timeOfDay = now.get(Calendar.HOUR_OF_DAY);
//to check if current hour is after 7pm
Calendar later = Calendar.getInstance();
later.set(Calendar.HOUR_OF_DAY, 20);
later.set(Calendar.MINUTE, 00);
later.set(Calendar.SECOND, 0);
if (timeOfDay >= 7 && timeOfDay <= 20) {
//notification's process
}
else if (now.after(later)) {
Log.d("Hey", "Added a day");
calendar.add(Calendar.DATE, 1);
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 0);
this.context = context;
prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new NotificationCompat.Builder(context);
name = prefs.getString("name" + count, "");
hours = prefs.getInt("hora" + count, 8);
minutes = prefs.getInt("minuto" + count, 0);
Intent newIntentKill = new Intent(context, Broadcast.class);
Bundle saco = new Bundle();
saco.putInt("count", count);
saco.putBoolean("shownBefore", true);
newIntentKill.putExtras(saco);
PendingIntent pendingIntentKill = PendingIntent.getBroadcast(context, count, newIntentKill, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntentKill);
} else {
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntentKill);
}
Log.d("Alarm", "Alarms set for everyday 7 am.");
}
else {
this.context = context;
prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE);
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new NotificationCompat.Builder(context);
name = prefs.getString("name" + count, "");
hours = prefs.getInt("hora" + count, 8);
minutes = prefs.getInt("minuto" + count, 0);
Intent newIntentKill = new Intent(context, Broadcast.class);
Bundle saco = new Bundle();
saco.putInt("count", count);
saco.putBoolean("shownBefore", true);
newIntentKill.putExtras(saco);
PendingIntent pendingIntentKill = PendingIntent.getBroadcast(context, count, newIntentKill, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntentKill);
}
else {
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntentKill);
}
Log.d("Alarm", "Alarms set for everyday 7 am.");
}
So, thank you in advance for your help. I would really appreciate it.