Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:恢复中的恢复时间_Java_Android_Countdowntimer - Fatal编程技术网

Java Android:恢复中的恢复时间

Java Android:恢复中的恢复时间,java,android,countdowntimer,Java,Android,Countdowntimer,我有一个问题,我的应用程序中有一个计时器,当我回到我的主窗口,回到计时器活动时,我只想从倒计时恢复计时器上的时间,如何恢复?Bundle对我和类似的话题没有帮助:(我将非常感激任何帮助 public class Timer extends Activity { private boolean start = false; // Semafor to button private Chronometer timer; // Chronometr private CountD

我有一个问题,我的应用程序中有一个计时器,当我回到我的主窗口,回到计时器活动时,我只想从倒计时恢复计时器上的时间,如何恢复?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);
        }
    }

}

使用
onSaveInstanceState
。重写此方法,并将当前时间保存到捆绑包中。然后,在
onCreate
上,从捆绑包中提取值并将其应用到视图中。

使用
onSaveInstanceState
。重写此方法,并将当前时间保存到捆绑包中。然后,在
onCreate上
,从包中提取值并将其应用于您的视图。

嗯,Alex说的没错,但我有一个新想法。倒计时开始计数,我们单击backButton活动被终止,但倒计时仍在计数,但当我再次使用此活动时,我可以运行新计时器,这很疯狂,有任何方法可以像at:在create方法中,我们检查倒计时是否仍在运行,我们只是更新一个计时器,但如果不是,我们将从新创建所有内容?目前,我的解决方案很棘手:覆盖onBackPressed()并添加此moveTaskToBack(true);但这不是回到我的主要活动,而是回到电话屏幕。

嗯,Alex说的没错,但我有了新的想法。倒计时开始计数,我们单击backButton活动被终止,但倒计时仍在计数,但当我再次使用此活动时,我可以运行新计时器,这很疯狂,有任何方法可以这样做那就是:在create方法中,我们检查倒计时器是否仍在运行,我们只是更新一个计时器,但如果不是,我们将从new创建所有内容?目前,我的解决方案很棘手:覆盖onBackPressed()并添加这个moveTaskToBack(true);但这不会返回到我的主要活动,而是返回到电话屏幕。

一旦创建并在onResume中;您可以在onRestoreInstanceState中获取savedBundle,当用户返回appAs时,覆盖这些方法以获取捆绑包上的保存数据,只要活动没有关闭。谢谢您的回答,非常简单:)非常感谢,我将尝试:)onCreate和onResume;您可以在onRestoreInstanceState中获取savedBundle,在用户返回您的appAs时覆盖这些方法以获取捆绑包上的保存数据,只要活动未关闭。谢谢您的回答,非常简单:)谢谢,我会尝试很多:)