ANDROID: - 比较当前时间和用户输入时间时出错?

时间:2018-04-11 11:52:11

标签: android timepicker

我想将当前时间与时间戳中输入的时间进行比较,以使我的手机在时间戳中用户输入的时间振动,它仅适用于双位数,例如10:10,11: 12,12:14但它不适用于单个数字时间,如1:10,2:10,3:10等,同时将单个数字数字与当前时间进行比较,因为它进入了ELSE条件如果是单位数的条件,它应该转到else吗?

    @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;
        }


}

0 个答案:

没有答案