我的应用设置中有CountDownTimer
,如下所示:
new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
clock.setText("Seconds Remaining: " + millisUntilFinished / 1000);
secsrem = (int) millisUntilFinished / 1000;
}
public void onFinish() {
//...}
}.start();
我需要在我的应用中使用其他方法访问onFinish
方法,这样当按下按钮时,它会调用定时器onFinish
方法。我该怎么做?
答案 0 :(得分:1)
只需将您的计时器存储在变量中:
public class YourClass {
public CountDownTimer timer = null;
...
timer = new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
clock.setText("Seconds Remaining: " + millisUntilFinished / 1000);
secsrem = (int) millisUntilFinished / 1000;
}
public void onFinish() {
//...}
}
timer.start();
...
然后你可以这样做:
timer.onFinish();
顺便说一句,我认为你不应该自己打电话给onFinish
,时间到了,系统会调用它。如果要取消定时器,请使用:
timer.cancel();