Problems To Cancel A CountDownTimer Android Java

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 10:18:24
Apoorv

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