Вопрос

I made a comparison between two calendars: Year, Month and day are the current year, month and day: but the result is not as expected if the time of the first calendar is after the current time the calendar waits till that time and starts ringing but if it's before the current time the calendar starts ringing directly so it's always accessing the second condition:

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));

                            }

I really can't figure out what the problem is!! thanks.

Edit: so the cause of this unexpected result was because of the 24hours format I am adding the time of the first calendar in 24hours format and the current time is getting its time placed as 12 hours format so need to get both of them in 24 hours format;

Edit2: So I changed the

int hour=calendar.get(Calendar.HOUR);//12 hours

to

int hour=calendar.get(Calendar.HOUROFDAY);//24 hours

and basically worked will add it as an answer if it really worked...

Это было полезно?

Решение

the cause of this unexpected result was because of the 24hours format I am adding the time of the first calendar in 24hours format and the current time is getting its time placed as 12 hours format so need to get both of them in 24 hours format; So I changed the:

int hour=calendar.get(Calendar.HOUR);//12 hours

to

int hour=calendar.get(Calendar.HOUROFDAY);//24 hours

and it worked!!

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top