我創建了一個可以正常工作的倒計時器,但每次活動都會重新開始。我正在嘗試保存倒計時值並將其用於第二個活動。以下是我的主要活動頁面上的倒計時代碼。Android倒數計時器,繼續定時器上的新活動
//Countdown Start
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
final int j = (int) millisUntilFinished/1000;
long timed;
timed = millisUntilFinished;
TextView textic = (TextView) findViewById(R.id.textView2);
textic.setText("" + (millisUntilFinished));
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView textic = (TextView) findViewById(R.id.textView2);
textic.setText(Integer.toString((j)));;
}
});
}
public void onFinish() {
}
}.start();
//Countdown Finish
我試過存儲倒計時值,然後使用passextra在我的意圖傳遞「定時」,但沒有奏效。我搜索了四周,找不到任何可行的解決方案。任何幫助將不勝感激。
感謝
================
編輯
================
公共類MainActivity擴展啓動畫面{
private CountDownTimer timer3;
public void onCreate(){
timer3 = new CountDownTimer(10000,100){
public void onTick(long millisUntilFinished) {
TextView displaytime = (TextView) findViewById(R.id.textView3);
displaytime.setText("" + (millisUntilFinished));}
public void onFinish() {
}
}.start();
};;
public void startTimer(){
}
public void cancelTimer(){
}
==============
EDIT 2
//Counter 1
Counter1 = new CountDownTimer(10000 , 1000) {
public void onTick(long millisUntilFinished) {
mCounter1TextField.setText("Seconds left: " + formatTime(millisUntilFinished));
}
public void onFinish() {
mCounter1TextField.setText("Finished!");
// Counter1.start();
}
};
Counter1.start();
public void starttimer() {
Counter1.start();
}
然後在我的第二個活動我已經有了:
MainActivity Counter1 = (MainActivity) getApplicationContext();
Counter1.starttimer();
TextView mCounter1TextField=(TextView)findViewById(R.id.textView4);
mCounter1TextField.setText("Seconds left: " + (Counter1));
我試圖在它自己的類中創建它,但倒計時不起作用,它只顯示了一個固定值10.我仍在研究如何創建一個單例。我對android非常陌生。感謝您的建議。 – user3041828