问题要取消CountDownTimer的Andr​​oid的Java问题、CountDownTimer、Andr、oid

2023-09-05 03:56:16 作者:杀生予夺

在我附近pressing的onBack pressed按钮我的应用程序,该CountDownTimer不会停止,直到他与计数完成。我怎么可以把Cou​​ntDownTimer 取消(); 在我onBack pressed无效?因为,当我离开我的应用程序(用//描述如下图所示),我不希望我的屏幕上柜台祝酒词了。

When I close my app by pressing the onBackPressed button, the CountDownTimer doesn't stop, untill he is done with counting. How can I put the CountDownTimer cancel(); in my onBackPressed void? Because, when I quit my application (shown below with //descriptions) I don't want counter toasts on my screen anymore.

在我的code顶部:

boolean network_connected = false;

什么是在我的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;
    }

}   //Close my onCreate over here.


@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 如:

在main_activity类集的顶部: CountDownTimer定时器; 之后做的事情,下面的

On top of the main_activity class set: CountDownTimer timer; after that do the things below.

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
}

onBack pressed 呼叫 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();
}