我是 android 新手。我有一个可用于考试管理的应用程序。用户可以插入每个科目的日期,该应用程序将为每个科目提供时间(以小时为单位)。倒计时计时器将从分配给每个主题的时间(例如 48 小时)并且应该归零。当它达到零时,应该播放警报,显示分配给该主题的时间已经结束。这是我的代码。
public class Alarmpage extends Activity{
TextView hourshow,minshow,secshow;
SQLiteDatabase database_read;
sampleDatabase samp;
Cursor cur;
Handler handler;
int initStart;
Runnable updater;
int t;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
hourshow=(TextView)findViewById(R.id.hr);
samp=new sampleDatabase(getApplicationContext());
database_read=samp.getReadableDatabase();
minshow=(TextView)findViewById(R.id.min);
secshow=(TextView)findViewById(R.id.sec);
cur=database_read.query(sampleDatabase.TABLE_SEC, null, null, null, null, null, null);
}
public void onResume() {
super.onResume();
if(cur.moveToFirst())
{
t=cur.getInt(3);
Log.e("time", String.valueOf(t));
}
handler=new Handler();
initStart = (int) SystemClock.elapsedRealtime();
Log.e("init", String.valueOf(initStart));
updater = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int sec,minute,hour;
int diff = t*60*60;
Log.e("time in sec", String.valueOf(diff));
System.out.println(diff);
/*if(diff>=1)
{
hour= diff/3600;
}
else {hour = 00; }
hourshow.setText(String.valueOf(hour));
Log.e("diff after hour", String.valueOf(diff));
Log.e("hour", String.valueOf(hour));
minute = (diff % 3600) / 60;
minshow.setText(":"+String.valueOf(minute));
sec= (diff % 60);
secshow.setText(":"+String.valueOf(sec));*/
if (diff >= 1) {
sec = (int) (diff%60);
} else {
sec=00;
}
secshow.setText(":"+String.valueOf(sec));
diff = diff/60;
if (diff >= 1) {
minute = (int) (diff%60);
} else {
minute=00;
}
minshow.setText(":"+String.valueOf(minute));
diff = diff/60;
if (diff >= 1) {
hour = (int) (diff%24);
} else {hour = 00; }
hourshow.setText(String.valueOf(hour));
t=(t*60*60)-1;
handler.postDelayed(this, 1000);
}
};
handler.post(updater);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
handler.removeCallbacks(updater);
}
}
但它显示了一些值。但它们不正确。请帮助我。我不理解此代码中的错误。提前致谢