Problems To Cancel A CountDownTimer Android Java

拜拜、爱过 提交于 2019-11-28 03:30:39

问题


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()?

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():

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

@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.


回答1:


Create a global object of CountDownTimer eg.

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
}

and on onBackPressed call timer.cancel(); like

@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();
}


来源:https://stackoverflow.com/questions/23630828/problems-to-cancel-a-countdowntimer-android-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!