我已經爲倒數計時器做了單獨的項目,它工作正常。現在我必須在我的比賽中運用它。所以我需要一個建議,應該在哪裏調用計時器?在線程中,我在給遊戲視圖引用還是在遊戲視圖類中的主類中?倒計時計時器爲android
1
A
回答
1
由於定時器本身是一個線程,每個定時器都有一個線程,在這個線程上順序執行任務。當此線程忙於運行任務時,可運行的任務可能會受到延遲。所以,你應該打電話給你的線程從主類
0
CountDownTimer將顯示格式化你的天,小時,分鐘和秒的時間都在一個TextView:
public class DemotimerActivity extends Activity {
/** Called when the activity is first created. */
TextView tv;
long diff;
long milliseconds;
long endTime;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
SimpleDateFormat formatter = new SimpleDateFormat("dd.MM.yyyy, HH:mm");
formatter.setLenient(false);
String oldTime = "21.10.2013, 12:00";
Date oldDate;
try {
oldDate = formatter.parse(oldTime);
milliseconds = oldDate.getTime();
//long startTime = System.currentTimeMillis();
// do your work...
long endTime=System.currentTimeMillis();
diff = endTime-milliseconds;
Log.e("day", "miliday"+diff);
long seconds = (long) (diff/1000) % 60 ;
Log.e("secnd", "miliday"+seconds);
long minutes = (long) ((diff/(1000*60)) % 60);
Log.e("minute", "miliday"+minutes);
long hours = (long) ((diff/(1000*60*60)) % 24);
Log.e("hour", "miliday"+hours);
long days = (int)((diff/(1000*60*60*24)) % 365);
Log.e("days", "miliday"+days);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Long serverUptimeSeconds = (System.currentTimeMillis() - milliseconds)/1000;
String serverUptimeText = String.format("%d days %d hours %d minutes %d seconds",
serverUptimeSeconds/86400,
(serverUptimeSeconds % 86400)/3600 ,
((serverUptimeSeconds % 86400) % 3600)/60,
((serverUptimeSeconds % 86400) % 3600) % 60
);
Log.v("jjj", "miliday"+serverUptimeText);
MyCount counter = new MyCount(milliseconds,1000);
counter.start();
}
// countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
//tv.setText("Left: " + millisUntilFinished/1000);
long diff = endTime - millisUntilFinished;
Log.e("left", "miliday"+diff);
long seconds = (long) (diff/1000) % 60 ;
//Log.e("secnd", "miliday"+seconds);
long minutes = (long) ((diff/(1000*60)) % 60);
//Log.e("minute", "miliday"+minutes);
long hours = (long) ((diff/(1000*60*60)) % 24);
//Log.e("hour", "miliday"+hours);
int days = (int)((diff/(1000*60*60*24)) % 365);
Log.v("days", "miliday"+days);
Long serverUptimeSeconds =
(System.currentTimeMillis() - millisUntilFinished)/1000;
String serverUptimeText =
String.format("%d days %d hours %d minutes %d seconds",
serverUptimeSeconds/86400,
(serverUptimeSeconds % 86400)/3600 ,
((serverUptimeSeconds % 86400) % 3600)/60,
((serverUptimeSeconds % 86400) % 3600) % 60
);
Log.v("new its", "miliday"+serverUptimeText);
// tv.setText(days +":"+hours+":"+minutes + ":" + seconds);
tv.setText(serverUptimeText);
}
}
}
相關問題
- 1. 倒計時計時器android
- 2. 倒計時器爲Android
- 3. 倒計時計時器問題android
- 4. Android ProgressBar計時器倒計時
- 5. 在Android的倒計時計時器
- 6. 倒計時計時器問題Android
- 7. android倒計時
- 8. 倒計時計時器
- 9. 自動倒計時器和動態倒計時器android
- 10. JQuery倒計時器不倒計時
- 11. 倒計時 - iPhone倒數計時器
- 12. 修復倒計時倒數計時器
- 13. Android Studio倒數計時器
- 14. Android倒數計時器
- 15. 倒計時器
- 16. Android ProgressBar倒計時
- 17. 安卓倒計時計時器時間
- 18. 倒計時時間的Android
- 19. 頂部小時倒數計時器Android
- 20. Android中的定時器倒計時
- 21. 倒數計時器
- 22. 倒數計時器?
- 23. 倒數計時器
- 24. 倒數計時器
- 25. jQuery倒計時 - 每日倒計時,還有次要倒計時?
- 26. 定時器倒計時
- 27. WPF定時器倒計時
- 28. 倒計時計時器的故障
- 29. JavaScript的jQuery倒計時計時器
- 30. jQuery每週倒計時計時器