1

我在每 30 分钟运行一次的 ServiceActivity 的 onStart 部分中使用以下代码:

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        Calendar ct = Calendar.getInstance();
        ct.setTimeInMillis(System.currentTimeMillis());
        int Hour = ct.get(Calendar.HOUR_OF_DAY);
        int Minute = ct.get(Calendar.MINUTE);
        System.out.println("Hour and Minute in Calender is: "+Hour+":"+Minute);
        
        myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        SILENCE = myPrefs.getBoolean("silence", false);
        
        int sHour = myPrefs.getInt("start_hour", 01);
        int sMinute = myPrefs.getInt("start_minute", 00);
        int eHour = myPrefs.getInt("end_hour", 02);
        int eMinute = myPrefs.getInt("end_minute", 00);
        
        System.out.println("======== SILENCE PERIOD ==========");
        System.out.println("The Start Time is: "+sHour+":"+sMinute);
        System.out.println("The END Time is: "+eHour+":"+eMinute);
        System.out.println("==================================");

        
        Calendar currentTime = new GregorianCalendar();
        currentTime.set(Calendar.HOUR_OF_DAY, Calendar.HOUR_OF_DAY);
        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_OF_DAY, 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_OF_DAY, 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);
        
        System.out.println(start_time.getTimeInMillis()+" < "+System.currentTimeMillis()+" && "+System.currentTimeMillis()+ " > "+end_time.getTimeInMillis());
        
        if(!SILENCE){
            System.out.println("SILENCE in if is :"+SILENCE);
            fireAlarm(Hour, Minute);
        }else{
            
            System.out.println("SILENCE in else is: "+SILENCE);
            
            if(((((start_time.getTimeInMillis() <= System.currentTimeMillis())) && ((System.currentTimeMillis() <= end_time.getTimeInMillis()) )))){
                System.out.println("You are in Silent mode");
                Toast.makeText(getApplicationContext(), "Ship's Clock is Silent", Toast.LENGTH_SHORT).show();
            }
            else{
                fireAlarm(Hour, Minute);
            }
        }
    }

现在,问题是,当 startHour 和 EndHour 是同一天的时间时,它工作得很好。但是,当我将 10:00 PM 作为 startHour 并将 8:00 AM 作为 EndHour 时,它不会在那段时间保持沉默。

那么逻辑或代码有什么问题?

4

0 回答 0