I am going to set daily alarm in my app.
All works fine in it. But now I want, for specific time duration, my alarm should remain silent.
e.g: if I have set the alarm should be play at every 30 minutes and if I have specified it should remain silent from 1:00 AM to 2:00 AM then it should not be play during that period.
Every 30 minutes, my service will call and if the time is remain silent between 1:00 AM to 2:00 AM.
See below code:
Calendar currentTime = new GregorianCalendar();
currentTime.set(Calendar.HOUR, Calendar.HOUR);
currentTime.set(Calendar.MINUTE, Calendar.MINUTE);
currentTime.set(Calendar.SECOND, Calendar.SECOND);
currentTime.set(Calendar.DATE, Calendar.DATE);
currentTime.set(Calendar.MONTH, Calendar.MONTH);
Calendar start_time = new GregorianCalendar();
start_time.set(Calendar.HOUR, sHour);
start_time.set(Calendar.MINUTE, sMinute);
start_time.set(Calendar.SECOND, Calendar.SECOND);
start_time.set(Calendar.DATE, Calendar.DATE);
start_time.set(Calendar.MONTH, Calendar.MONTH);
Calendar end_time = new GregorianCalendar();
end_time.set(Calendar.HOUR, eHour);
end_time.set(Calendar.MINUTE, eMinute);
end_time.set(Calendar.SECOND, Calendar.SECOND);
end_time.set(Calendar.DATE, Calendar.DATE);
end_time.set(Calendar.MONTH, Calendar.MONTH);
if((!SILENCE) && (((!(start_time.getTimeInMillis() < currentTime.getTimeInMillis())) && (!(currentTime.getTimeInMillis() < end_time.getTimeInMillis()) )))){
// Alarm will play
}
else{
// you are in Silent mode
}
This code does not seem to work. I always get "you are in Silent mode"
You are doing wrong
currentTime.set(Calendar.HOUR, Calendar.HOUR);
Using this sentence you are setting the hours and minutes to the integer values. for example Calendar.HOUR has constant value 10 like wise all the constant have different fixed values and you are assigning this values to the calendar and probably it causes an issue to you.
Solution
If you want to set the only hours and minutes then leave the other fields so that it will automatically consider your current datatime.
Calendar currentTime = Calendar.getInstant();
currentTime.set(Calendar.HOUR, hours);
currentTime.set(Calendar.MINUTE, minuts);
so this will set hours and minutes in the calendar to the values which you provided and it will set other information like day, month, years and others to the current.
Edit
If you are facing the issue to get the current time in milli seconds then use System.currentTimeMillis();.
And for get future time in milli seconds you have to set appropriate time in the calendar and after that you have to get the time from the calendar in the milli seconds like calendar.getTimeInMillis().