取消 CountDownTimer Android Java 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23630828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems To Cancel A CountDownTimer Android Java
提问by KD-21
When I close my app by pressing the BACK button (onBackPressed() called), the CountDownTimer
doesn't stop, until it is done with counting. How can I put the CountDownTimer
cancel();in my onBackPressed()
?
当我按 BACK 按钮(调用 onBackPressed())关闭我的应用程序时,CountDownTimer
它不会停止,直到完成计数。我怎么能把CountDownTimer
取消(); 在我的onBackPressed()
?
Because, when I quit my application (shown below with descriptions) I don't want counter toasts on my screen anymore.
因为,当我退出我的应用程序(如下所示)时,我不想再在我的屏幕上显示柜台敬酒了。
On top of my code:
在我的代码之上:
boolean network_connected = false;
What's in my onCreate()
:
我的onCreate()
:
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;
}
onBackPressed()
method
onBackPressed()
方法
@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
}
}
Thanks.
谢谢。
采纳答案by Apoorv
Create a global object of CountDownTimer
eg.
创建一个全局对象,CountDownTimer
例如。
On top of the main_activity class set: CountDownTimer timer;
after that do the things below.
在 main_activity 类集之上:CountDownTimer timer;
然后做下面的事情。
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
}
and on onBackPressed
call timer.cancel();
like
并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();
}