我有一個活動,我需要實現一個基本的mm :: ss計時器。以下是我爲它編寫的代碼。但問題是,當我在模擬器中按下後退按鈕並再次單擊該應用程序時,值變化更快。它看起來像onCreate再次被調用。我如何糾正這一點? 我已經嘗試創建一個布爾變量,並在第一次調用任務時將其設置爲true。我只在值爲false時才調用startPeriodidUpdates()。但是onCreate再次以false值創建變量。Android:onCreate被重複調用
public class GraphicsActivity extends Activity {
static int seconds = 0;
static int minutes = 0;
public static String time_elapsed;
public static boolean clearView = true;
Handler myhandler = new Handler();
public static String min,sec;
public static boolean running = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startPeriodicUpdates();
}
public void onResume(){
super.onResume();
}
public void onPause() {
super.onPause();
}
public void onStop(){
super.onStop();
}
public void startPeriodicUpdates()
{
periodicCall();
}
public void stopPeriodicUpdates(){
myhandler.removeCallbacksAndMessages(myhandler);
}
public void periodicCall()
{
seconds++;
if(seconds ==60)
{
seconds=0;
minutes++;
if(minutes==60)
{
seconds=0;
minutes=0;
}
}
// left-padding zeros to the minutes and seconds values
min = String.format("%02d",minutes);
sec = String.format("%02d",seconds);
time_elapsed = min + ":" + sec;
time_elapsed = min + ":" + sec + "";
myhandler.postDelayed(new Runnable(){
public void run(){
periodicCall();
}
},1000);
}
是那做正確的事情,但對我來說我希望計時器在運行即使我按下了模擬器中的後退按鈕,也會出現背景。 – hektor 2012-04-24 05:30:21