取消CountDownTimer Android Java的问题

当我通过按BACK按钮(调用onBackPressed())关闭应用程序时,CountDownTimer它不会停止,直到完成计数为止。我怎样才能把CountDownTimer

在我的onBackPressed()

因为,当我退出应用程序时(如下面的说明所示),我不再想要在屏幕上进行举杯了。

boolean network_connected = false;

if (check_network.isInternetAvailable(this)) {

network_connected = true;

new connect_task_main().execute("");

} else {

network_connected = false;

}

if (network_connected == false) {

new CountDownTimer(11000, 1000) {

public void onTick(long millisUntilFinished) {

global.toast.setText("No Internet Connection!" + "\n" + "Automatic Refresh In: " + millisUntilFinished / 1000); //set text for toast

global.toast.show(); //show toast

}

public void onFinish() {

if (network_connected == false) {

global.cancel_toast(0); //stop all toasts

finish(); //quit activity

startActivity(new Intent(main_activity.this, main_activity.class)); //start activity

} else {

}

}

}.start(); //start the countdowntimer

} else {

network_connected = true;

}

方法

@Override

public void onBackPressed() {

if (page_number > global.page_number_min) { //does not matter

page_number--; //does not matter

global.cancel_toast(0); //stop all toasts

network_connected = true;

finish();

} else {

global.cancel_toast(0);

network_connected = true;

finish(); //quit activity

super.onBackPressed(); //quit application

}

}

谢谢。

回答:

创建一个CountDownTimer例如的全局对象。

timer = new CountDownTimer(11000, 1000) 

{

public void onTick(long millisUntilFinished)

{

global.toast.setText("No Internet Connection!" + "\n" + "Automatic Refresh In: " + millisUntilFinished / 1000); //set text for toast

global.toast.show(); //show toast

}

public void onFinish()

{

if (network_connected == false)

{

global.cancel_toast(0); //stop all toasts

finish(); //quit activity

startActivity(new Intent(main_activity.this, main_activity.class)); //start activity

}

else

{

}

}

}.start(); //start the countdowntimer

}

onBackPressed呼吁timer.cancel();

@Override

public void onBackPressed()

{

if (page_number > global.page_number_min)

{ //does not matter

page_number--; //does not matter

global.cancel_toast(0); //stop all toasts

network_connected = true;

finish();

}

else

{

global.cancel_toast(0);

network_connected = true;

finish(); //quit activity

super.onBackPressed(); //quit application

}

timer.cancel();

}

以上是 取消CountDownTimer Android Java的问题 的全部内容, 来源链接: utcz.com/qa/406159.html

回到顶部