英文:
counter.Cancel(); isn't executed in OnStop(); method
问题
我有一个 `CounterTimer`,当 API 调用成功时会启动。如果用户按下 `KeyEvent.KEYCODE_HOME`,`CounterTimer` 必须取消,但实际上它并没有取消,而是继续运行。
**这是我启动计时器的方式**
```java
private void countDownStart(String event_date_time) {
String[] tokens = event_date_time.split(":");
int secondsToMs = Integer.parseInt(tokens[2]) * 1000;
int minutesToMs = Integer.parseInt(tokens[1]) * 60000;
int hoursToMs = Integer.parseInt(tokens[0]) * 3600000;
long total = secondsToMs + minutesToMs + hoursToMs;
timer = new CountDownTimer(total, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
int hours = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
CountDown.setVisibility(View.VISIBLE);
CountDown.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
public void onFinish() {
CountDown.setVisibility(View.VISIBLE);
CountDown.setText("00:00:00");
loader.showProgressBar();
startActivity(new Intent(mContext, Questions.class));
}
};
timer.start();
}
这是我用来取消计时器的方法。我尝试过在 onStop
和 KeyEvent.KEYCODE_HOME
中使用。
@Override
protected void onStop() {
System.out.println("count: onStop Called");
loader.hideProgressBar();
if (timer != null) {
timer.cancel();
}
super.onStop();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
loader.hideProgressBar();
System.out.println("KEYCODE_HOME");
if (timer != null) {
timer.cancel();
}
return true;
}
return false;
}
可能有什么问题?
<details>
<summary>英文:</summary>
I have a `CounterTimer` which starts when an Api is successful. if a user presses the `KeyEvent.KEYCODE_HOME` CounterTimer must cancel but instead of cancelling it keeps running
**This is how i start the counter**
private void countDownStart(String event_date_time) {
String[] tokens = event_date_time.split(":");
int secondsToMs = Integer.parseInt(tokens[2]) * 1000;
int minutesToMs = Integer.parseInt(tokens[1]) * 60000;
int hoursToMs = Integer.parseInt(tokens[0]) * 3600000;
long total = secondsToMs + minutesToMs + hoursToMs;
timer = new CountDownTimer(total, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60;
int minutes = (int) ((millisUntilFinished / (1000 * 60)) % 60);
int hours = (int) ((millisUntilFinished / (1000 * 60 * 60)) % 24);
CountDown.setVisibility(View.VISIBLE);
CountDown.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
}
public void onFinish() {
CountDown.setVisibility(View.VISIBLE);
CountDown.setText("00:00:00");
loader.showProgressBar();
startActivity(new Intent(mContext, Questions.class));
}
};
timer.start();
}
**and this is what I used to cancel. I've tried with both `onStop` and `KeyEvent.KEYCODE_HOME`**
@Override
protected void onStop() {
System.out.println("count: onStop Called");
loader.hideProgressBar();
if (timer != null) {
timer.cancel();
}
super.onStop();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
loader.hideProgressBar();
System.out.println("KEYCODE_HOME");
if (timer != null) {
timer.cancel();
}
return true;
}
return false;
}
What could be possibly wrong here?
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论