我有这样的代码,我在Thread上运行它。所以,它会每秒检查一次:
SQLiteDatabase db = helper.getReadableDatabase();
cursor = db.rawQuery("select * from list where day = '"+day+"'", null);
cursor.moveToFirst();
for (int i=0; i<cursor.getCount(); i++){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, cursor.getString[2]);//13
cal.set(Calendar.MINUTE, cursor.getString[3]);//10
cal.set(Calendar.SECOND, 0);
long noon_start = cal.getTimeInMillis();
cal.set(Calendar.HOUR_OF_DAY, cursor.getString[4]);//13
cal.set(Calendar.MINUTE, cursor.getString[5]);//15
cal.set(Calendar.SECOND, 0);
long noon_end = cal.getTimeInMillis();
long now = System.currentTimeMillis();//current time
if(now > noon_start && now<noon_end){
if (jl.active==false){//ignore it
Toast.makeText(theService.this, "If "+(noon_end-now), Toast.LENGTH_LONG).show();
}
}else{
if (jl.active==true){ //ignore it
Toast.makeText(theService.this, "Else "+(noon_end-now), Toast.LENGTH_LONG).show();
}
}
cursor.moveToNext();
}
我想现在时间13:10到13:15,Toast会设置文本If,否则还会设置setText。但它不起作用。如果时间&lt; 13:10或&gt; 13.15,则可行。但是当13:10 - 13:15时,它会遇到if,之后再跑到别的地方,如果再次。请帮忙 。 。 我真的很困惑。有什么建议??
我认为cursor.moveToNext
答案 0 :(得分:0)
你走了:
Calendar cal = Calendar.getInstance(); // this will get a calendar set to the current time
Date now = new Date(cal.getTimeInMillis()); // we make a date object with the current time
cal.set(Calendar.HOUR_OF_DAY, 13); // now we set the calendar to a specific time
cal.set(Calendar.MINUTE, 10);
cal.set(Calendar.SECOND, 0);
Date start = new Date(cal.getTimeInMillis()); // create a new date
cal.set(Calendar.MINUTE, 15); // set calendar minutes. hour and seconds stay the same anyway
Date end = new Date(cal.getTimeInMillis());
if (now.after(start) && now.before(end)) {
Toast.makeText(theService.this, "If "+(noon_end-now), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(theService.this, "Else "+(noon_end-now), Toast.LENGTH_SHORT).show();
}