当按下设备后退按钮时,如何停止倒数计时器(不是我制作的按钮,而是设备上的后退按钮)。这就是我试过的......
@Override
public void onBackPressed() {
flight_engineer.this.finish();
win.cancel();
win = null;
Log.i("backPressed","hello");
}
此日志会打印问候语,但是当我是新活动时,计时器(称为win)仍会计数。
编辑:这就是我使用Timer的方式
win = new CountDownTimer(120000,1000) {
@Override
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
if(alti >= 28000 && alti <= 33000 && speed >= 280 && speed <= 320 && pressure >= 8 && pressure <= 12
&& over_heat >= 700 && over_heat <= 750 && fuel > 50){
Intent start = new Intent(flight_engineer.this, win.class);
finish();
flight_engineer.this.startActivity(start);
}
else
{
Intent start = new Intent(flight_engineer.this, losing.class);
finish();
flight_engineer.this.startActivity(start);
}
}
};
win.start();
这是我宣布代码顶部的地方:
public class flight_engineer extends AppCompatActivity {
int fuel = 100;
int alti = 10000;
int speed = 100;
int over_heat = 600;
int pressure = 10;
boolean started = true;
CountDownTimer fuelCount;
CountDownTimer PSICount;
CountDownTimer speedTimer;
CountDownTimer spike;
CountDownTimer win;