I'm new at using an alarm service for Android. I want to create a medication reminder which is the medication information and number of repeated notification per day as the input data. However, I only want to enable the notification between 7 AM and 7 PM. So, the apps will notify within that time only and also the apps will start to notify at 7 AM.
Example: If the user wants to repeat the notification from the apps 3 times per day, then every 4 hours the apps will notify them to take the medicine (12 hours / 3 times = 4 hours).
I have tried using the while loop, however it somehow didn't worked as I expected where if the condition is true, then it will keep on notify even though it's not the time yet (this happened to my code).
So, here what I tried to do:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 7);
while(calendar.get(Calendar.HOUR_OF_DAY) >= 0) {
while (calendar.get(Calendar.HOUR_OF_DAY) >= 7 && calendar.get(Calendar.HOUR_OF_DAY) <= 19) {
// the process here
}
calendar.add(Calendar.HOUR_OF_DAY, 1);
}
I'm sorry if my code is lacking in many ways. But, I really hope that you guys can help me or share with me any other possible solution. Thank you.
You are setting the calendar hour incorrectly.
Replace
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 7);
while(calendar.get(Calendar.HOUR_OF_DAY) >= 0) {
while (calendar.get(Calendar.HOUR_OF_DAY) >= 7 && calendar.get(Calendar.HOUR_OF_DAY) <= 19) {
// the process here
}
calendar.add(Calendar.HOUR_OF_DAY, 1);
}
With
Calendar cc = Calendar.getInstance();
int mHour = cc.get(Calendar.HOUR_OF_DAY);
if (mHour >= 7 && mHour <= 19) {
// the process here
}