我需要計算兩次之間的時間。當它在Thread
中調用時,System.currentTimeMillis()
每次返回相同的值。 我的代碼是:System.currentTimeMillis()返回相同的時間戳
@Override
protected void onCreate(Bundle savedInstanceState) {
// Other codes..
start_sec = Math.round(System.currentTimeMillis()/1000);
fin = false;
runThread();
// Other codes..
}
private void runThread() {
new Thread() {
public void run() {
while (i++ < 61) {
if (!running) return;
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!fin){
int len = Math.round(System.currentTimeMillis()/1000) - start_sec;
Log.d("current time: ",String.valueOf( Math.round(System.currentTimeMillis()/1000)));
Log.d("difference is: ", String.valueOf(len));
if(len < 0 && len > 58){
fin=true;
}
timerec.getLayoutParams().width = metrics.widthPixels *(60- len)/60;
timerec.requestLayout();
}
else{
end_game();
running= true;
}
}
});
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
下面是日誌:
...
D/current time:: 1492337024
D/difference is:: 0
D/current time:: 1492337024
D/difference is:: 0
....
它returs相同的 「時間」。解決辦法是什麼?