0

我有一个问题,我的应用程序中有一个计时器,当我返回主窗口并返回计时器活动时,我只想从 CountDownTimer 恢复计时器上的时间,如何做到这一点?Bundle对我和类似的主题没有帮助:(我将非常感谢任何帮助。

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);
        }
    }

}
4

2 回答 2

5

利用onSaveInstanceState. 覆盖此方法,并将当前时间保存到 Bundle 中。然后,在 上onCreate,从 Bundle 中提取值并将其应用于您的视图。

于 2013-07-15T15:00:21.460 回答
0

嗯,是的,亚历克斯说的是正确的,但我有新的想法。CountDownTimer 开始计数,我们单击 backButton 活动被杀死,但 CountDownTimer 仍在计数,但是当我再次使用此活动时,我可以运行新计时器,这很疯狂,有任何方法可以做到这一点:在创建方法中,我们检查CountDownTimer 仍在运行,我们只是更新了一个计时器,但如果没有,我们会从新创建一切吗?现在我的解决方案很棘手:覆盖 onBackPressed() 并添加这个 moveTaskToBack(true); 但这不是回到我的 mainActivity 而是返回到电话屏幕。

于 2013-07-18T19:42:52.077 回答