保存在 sharedpreference 中的倒数计时器显示错误的计时器
Countdowntimer saved in sharedpreference shows wrong timer
我有一个倒计时计时器,它需要一个 long 作为参数,然后它会显示那个时间的倒计时。当我单击后退按钮并退出应用程序时,我希望将倒数计时器的剩余时间保存在 onStop() 方法的共享首选项中。当我重新打开应用程序时,我想将剩余时间作为参数并再次启动计时器,直到它达到 0。
我遇到了麻烦,因为如果我的参数是 2,则倒数计时器会从 1 分 59 秒开始倒计时。当我退出应用程序并重新打开应用程序时,它仍然显示 1 分 59 秒。倒数计时器不会减去剩余时间。它只是显示与输入相同的时间。
private void startTimer() {
countDownTimer = new CountDownTimer(timee*60000 , 1000) {
@Override
public void onTick(long l) {
tv.setText(simpleDateFormat.format(l));
timesLeft = l;
}
@Override
public void onFinish() {
tv.setText("00:00:00");}
}.start();
这里是 onStop() 和 onStart();
protected void onStart() {
super.onStart();
SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
if(1>0){
long s = sPrefs.getLong("sysstoptime", 0);
long tt = sPrefs.getLong("timeleft", 0);
long currenttime = System.currentTimeMillis();
long k = sPrefs.getLong("timeset", 0);
s = s+tt;
timee = (s-currenttime)/60000+1;
startTimer();}
else {
Toast t5 = Toast.makeText(this, "less than or equal 0", Toast.LENGTH_LONG);
t5.show();
}
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sPrefs.edit();
editor.putLong("sysstoptime", System.currentTimeMillis());
editor.putLong("timeleft", timesLeft);
editor.putLong("timeset", timeset);
editor.apply();
}
我想做的是,当倒数计时器为 运行 并且我要离开应用程序时,我正在使用 System.currenttimeinmillis + 倒数计时器的剩余时间。即变量s+tt。
然后我把 timee 等于 s+tt 的剩余时间 - System.currenttimeinmillis,有倒数计时器,倒计时剩余时间包括 app 的时间 closed/stopped。
然后我调用方法 startTimer();使用我的新参数 timee。但是它不显示剩余时间,而是在我退出并进入应用程序时显示与我输入相同的时间。
timee是long变量,用户选择timee是什么,单位是分钟。因此,如果 timee = 10,则表示 10 分钟,倒数计时器将从 09:59
开始计时
首先,当您获得倒计时的价值时,在按下开始按钮 [或您用来调用 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();
}
更改 onStop()
@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();
}
}
我有一个倒计时计时器,它需要一个 long 作为参数,然后它会显示那个时间的倒计时。当我单击后退按钮并退出应用程序时,我希望将倒数计时器的剩余时间保存在 onStop() 方法的共享首选项中。当我重新打开应用程序时,我想将剩余时间作为参数并再次启动计时器,直到它达到 0。
我遇到了麻烦,因为如果我的参数是 2,则倒数计时器会从 1 分 59 秒开始倒计时。当我退出应用程序并重新打开应用程序时,它仍然显示 1 分 59 秒。倒数计时器不会减去剩余时间。它只是显示与输入相同的时间。
private void startTimer() {
countDownTimer = new CountDownTimer(timee*60000 , 1000) {
@Override
public void onTick(long l) {
tv.setText(simpleDateFormat.format(l));
timesLeft = l;
}
@Override
public void onFinish() {
tv.setText("00:00:00");}
}.start();
这里是 onStop() 和 onStart();
protected void onStart() {
super.onStart();
SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
if(1>0){
long s = sPrefs.getLong("sysstoptime", 0);
long tt = sPrefs.getLong("timeleft", 0);
long currenttime = System.currentTimeMillis();
long k = sPrefs.getLong("timeset", 0);
s = s+tt;
timee = (s-currenttime)/60000+1;
startTimer();}
else {
Toast t5 = Toast.makeText(this, "less than or equal 0", Toast.LENGTH_LONG);
t5.show();
}
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sPrefs.edit();
editor.putLong("sysstoptime", System.currentTimeMillis());
editor.putLong("timeleft", timesLeft);
editor.putLong("timeset", timeset);
editor.apply();
}
我想做的是,当倒数计时器为 运行 并且我要离开应用程序时,我正在使用 System.currenttimeinmillis + 倒数计时器的剩余时间。即变量s+tt。 然后我把 timee 等于 s+tt 的剩余时间 - System.currenttimeinmillis,有倒数计时器,倒计时剩余时间包括 app 的时间 closed/stopped。 然后我调用方法 startTimer();使用我的新参数 timee。但是它不显示剩余时间,而是在我退出并进入应用程序时显示与我输入相同的时间。
timee是long变量,用户选择timee是什么,单位是分钟。因此,如果 timee = 10,则表示 10 分钟,倒数计时器将从 09:59
开始计时首先,当您获得倒计时的价值时,在按下开始按钮 [或您用来调用 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();
}
更改 onStop()
@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();
}
}