[英]Android : Restore time in resume
I have question i have a chronometer in my app, when i back to my main window, and get back to Timer activity, I just want to resume a time on chronometer from CountDownTimer, how to to this ? 我有一个问题,我的应用中有一个计时器,当我返回主窗口并返回到计时器活动时,我只想从CountDownTimer恢复计时器的时间,该如何进行? Bundle doesn't help me and similar topics :( I will be very gratefull for any help. 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);
}
}
}
Make use of onSaveInstanceState
. 利用onSaveInstanceState
。 Override this method, and save your current time into the Bundle. 重写此方法,并将您当前的时间保存到Bundle中。 Then, on onCreate
, extract the value from the Bundle and apply it to your View. 然后,在onCreate
,从Bundle中提取值并将其应用于视图。
Hmm yes that is correct what Alex says but I have new idea. 嗯,是的,Alex说的没错,但我有新主意。 CountDownTimer start count, we click backButton activity is killed, but CountDownTimer still counting, but when I use this activity again, I can run new timer and this is crazy, there is any way to do this like that : In create method we check that CountDownTimer is still running and we just update a chronometer, but if not we create everything from new? CountDownTimer开始计数,我们单击了backButton活动被杀死,但是CountDownTimer仍在计数,但是当我再次使用此活动时,我可以运行新计时器,这很疯狂,有任何办法可以做到这一点:在create方法中,我们检查CountDownTimer仍在运行,我们只是更新了一个计时器,但如果不是,我们是否会从new中创建所有内容? At now my solutions is tricky: override the onBackPressed() and add this moveTaskToBack(true); 现在,我的解决方案很棘手:重写onBackPressed()并添加此moveTaskToBack(true); but this not back to my mainActivity but return to telephone screen. 但这不返回我的mainActivity,而是返回电话屏幕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.