2013-07-15 157 views
0

我有問題我在我的應用程序中有一個計時器,當我回到我的主窗口,並返回到計時器活動時,我只想從CountDownTimer恢復計時器的時間,如何這個 ?包不幫我和類似主題:(我會很gratefull任何幫助。Android:恢復恢復時間

public class Timer extends Activity { 
    private boolean start = false; // Semafor to button 
    private Chronometer timer; // Chronometr 
    private CountDownTimer count; // CountDown class 
    private final long startTime = 30 * 1000; // High 
    private final long end = 1 * 1000; //LOw 
    private ImageButton startB; // Button 
    private int VIBRATION_TIME = 1000; //vibration time 
    private Bundle state = new Bundle(); //button state 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_timer); 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      // Show the Up button in the action bar. 
      getActionBar().setDisplayHomeAsUpEnabled(true); 
     } 
     this.timer = ((Chronometer) findViewById(R.id.chronometer1)); 
     count = new MyCountDownTimer(startTime, end); 
     timer.setText("00:" + String.valueOf(startTime/1000)); 
     startB = (ImageButton) this.findViewById(R.id.start_pause); 

     startB.setBackgroundResource(R.drawable.start_button); 
     startB.setOnClickListener(new View.OnClickListener() { 
      // click init 
      @Override 
      public void onClick(View v) { 
       start = !start; 
       if (start == true) { 
        count.start(); // start count 
        startB.setBackgroundResource(R.drawable.stop_button); 
       } else { 
        count.cancel(); // pause count 

        startB.setBackgroundResource(R.drawable.start_button); 
       } 
      } 
     }); 
    } 
    .... 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     start = state.getBoolean("Button"); 

    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     state.putBoolean("Button", start); 
     //count.cancel(); I don't wanna this! 
    } 


    class MyCountDownTimer extends CountDownTimer { 
     public MyCountDownTimer(long startTime, long interval) { 
      super(startTime, interval); 
     } 

     @Override 
     public void onFinish() { 
      timer.setText("00:00"); 
      // TO DO : 

      Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
      v.vibrate(VIBRATION_TIME); 
      startB.setBackgroundResource(R.drawable.start_button); 
     } 

     @Override 
     public void onTick(long millisUntilFinished) { 
      long tmp = millisUntilFinished/1000; 
       timer.setText("00:" + tmp); 
     } 
    } 

} 

回答

5

利用onSaveInstanceState。重寫此方法,並保存當前的時間到指定的捆綁。然後,在onCreate,從包中提取值並將其應用到你的視圖

+0

onCreate and inResume;並且您可以在onRestoreInstanceState中獲取savedBundle,當用戶返回到您的應用程序時,覆蓋這些方法以獲取已保存的數據捆綁 –

+0

只要活動未關閉 – kabuto178

+0

Oww感謝您的回答,這非常簡單:)非常感謝我將嘗試它:) – pkruk

0

嗯是的,這是正確的什麼亞歷克斯說,但我有新的想法CountDownTimer開始計數,我們點擊backButton活動死亡,但CountDownTimer仍然計數,但是當我再次使用這個活動,我可以運行新的計時器,這是瘋狂的,有任何方式可以這樣做:在create方法中,我們檢查CountDownTimer仍在運行,我們只是更新計時器,但如果n我們是否從新創造了一切?現在我的解決方案很棘手:重寫onBackPressed()並添加moveTaskToBack(true);但這並沒有回到我的主要活動,而是回到了電話屏幕。