我在ServiceActivity的onStart部分使用以下代码,该部分每30分钟运行一次:
@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点作为startHour而上午8点作为EndHour时,它会在那段时间内保持沉默。
那么逻辑或代码有什么问题?
请帮助我。
感谢。