0

I want to compare the current time with the time entered in the timepicker to make my phone vibrate at the time entered by the user in the timepicker, it is working only for the double digit number for example 10:10,11:12,12:14 but it is not working for the single digit time like 1:10, 2:10, 3:10 etc while comparing the single digit number with the current time due to the substring because it goes into the ELSE condition but it should go to the else if condition of single digit number?

    @Override

    public void onReceive(Context context, Intent intent) {
        myAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        Date currenttime = Calendar.getInstance().getTime();
        settime = intent.getStringExtra("time");
        Log.e("=========","In Receiver");

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    chk=checkSlaMissedOrNot(settime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        },0,1000);



                if (chk == true) {
                    Log.e("=========", "Phone vibrates");
                    myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                } else /*if (chk == false)*/{

                    Log.e("=========", "Phone not vibrating");
                   // checkSlaMissedOrNot(settime);
                    myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);


                }


    }

    public boolean checkSlaMissedOrNot(String sla) throws ParseException {
        boolean slaMissedOrnot = false;
        Calendar cal = Calendar.getInstance();

        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int min = cal.get(Calendar.MINUTE);

        if ((hour>0 && hour<10 && min>=0 && min <10)) {
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sla.substring(0, 1)));
            cal.set(Calendar.MINUTE, Integer.parseInt(sla.substring(3,5)));
        }
         else if( (hour>0 && hour<10) && (min>9 && min <59) )
        {
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sla.substring(0,1)));
            cal.set(Calendar.MINUTE, Integer.parseInt(sla.substring(4,6)));
        }
        else if( (hour>9 && hour<=12) && (min>9 && min<=59 )) //done
        {
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sla.substring(0,2)));
           cal.set(Calendar.MINUTE, Integer.parseInt(sla.substring(5,7)));
        }

        else if( (hour>9 && hour<=12) && (min >0 && min<10)) //done
        {
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sla.substring(0, 2)));
            cal.set(Calendar.MINUTE, Integer.parseInt(sla.substring(3,4)));
        }
        else
        {
            Log.e("=============","ELSE");
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sla.substring(0,1)));
            cal.set(Calendar.MINUTE, Integer.parseInt(sla.substring(4,6)));

        }
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Log.e("=========", "In Method");


            if (Calendar.getInstance().after(cal) ) { //if current time is after the entered time
                Log.e("==============", "in the if condition");
                myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                Log.e("=========", "Phone vibrates");
                slaMissedOrnot = true;

            } else {
                myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

                Log.e("==============", "in the else condition");
                slaMissedOrnot = false;
                Log.e("=========", "Phone not vibrates");

            }

            return slaMissedOrnot;
        }


}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Rajat
  • 37
  • 5
  • better way to do the time parsing: https://stackoverflow.com/questions/19873864/android-parsing-string-to-date-time-with-simpledateformat + `format=new SimpleDateFormat("HH:mm");` – leonardkraemer Apr 11 '18 at 12:00
  • You're currently using a very unsafe way of checking your times. Try parsing your String to a Date object using SimpleDateFormat instead comparing it like you do now. – Sander van't Veer Apr 11 '18 at 12:03

0 Answers0