Я провел сравнение между двумя календарями: год, месяц и день - это текущий год, месяц и день: но результат не такой, как ожидалось, если время первого календаря после текущего времени, календарь ждет до этого времени и начинает звонить но если это до текущего времени, календарь начинает звонить напрямую, поэтому он всегда обращается ко второму условию:
public void scheduleAlarm()
{
//to initialize the time variable not a real value:
Long time=Long.parseLong("0");
int hour=calendar.get(Calendar.HOUR);
int minute=calendar.get(Calendar.MINUTE);
Calendar cal_now = new GregorianCalendar(Year,Month,Day, hour, minute,Calendar.SECOND);
Calendar cal_alarm_first=new GregorianCalendar(Year,Month,Day,21,14,0);
//if the first calendar is in the past increment its day by one:
if(cal_alarm_first.before(cal_now)){
Notif_Body=getResources().getString(R.string.remembrance_body_first);
//here if the first alarm is already gone I should add a day and it should ring after a day from now with the specified hour and minute...
cal_alarm_first.add(Calendar.DATE, 1);
time=cal_alarm_first.getTimeInMillis();
}
else if(cal_alarm_first.after(cal_now)){
//the problem is here it always access this condition even if the first calendar time is before the current time for example same date but the first calendar is 9:14 and current time is 9:17 it always access this condition and the alarm starts ringing...
Notif_Body=getResources().getString(R.string.remembrance_body_first);
time=cal_alarm_first.getTimeInMillis();
}
//to send this alarm to be retrieved by the broadcast receiver class:
Intent intentAlarm = new Intent(this, TimeAlarm.class);
//add the notification body to the intent:
intentAlarm.putExtra("Notif_body", Notif_Body);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}
Я действительно не могу понять, в чем проблема!! Спасибо.
Редактировать: так что причина этого неожиданного результата была из-за 24-часового формата. Я добавляю время первого календаря в 24-часовом формате, а текущее время помещается в 12-часовой формат, поэтому необходимо получить оба в 24-часовом формате;
Edit2: поэтому я изменил
int hour=calendar.get(Calendar.HOUR);//12 hours
to
int hour=calendar.get(Calendar.HOUROFDAY);//24 hours
и в основном сработало, добавит это как ответ, если это действительно сработало...