首先,当您得到倒计时的值时,在按下开始按钮[或您用来调用startTimer()的任何东西]后,将其乘以60000。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timee = YOUR_VALUE_TO_COUNTDOWN * 60000;
startTimer();
}
});
更改startTimer(),如下所示:
private void startTimer() {
countDownTimer = new CountDownTimer(timee, 1000) {
@Override
public void onTick(long l) {
tv.setText(simpleDateFormat.format(l));
timesLeft = l;
}
@Override
public void onFinish() {
tv.setText("00:00:00");
}
}.start();
}
更改桌面()
@Override
protected void onStop() {
super.onStop();
SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sPrefs.edit();
editor.putLong("timeleft", timesLeft);
editor.putLong("sysstoptime", System.currentTimeMillis());
editor.apply();
}
最后是onStart()
@Override
protected void onStart() {
super.onStart();
SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
if(1>0){
timesLeft = sPrefs.getLong("timeleft", 0);
long stopTime = sPrefs.getLong("sysstoptime", 0);
long currentTime = System.currentTimeMillis();
timee = timesLeft - (currentTime - stopTime);
startTimer();
}
else {
Toast t5 = Toast.makeText(this, "less than or equal 0", Toast.LENGTH_LONG);
t5.show();
}
}