There is a timer repeating the same action at a certain interval vmeni.Timer repeats indefinitely. But I need to add the cancel button. Let us run randomly but I want to stop. Here is the timer code

final TimerTask timerTask = new TimerTask() { @Override public void run() { int i=0; i++; System.out.println("Request N +" +i); Sender s=new Sender(MainActivity.this,urlAddress,lat,lot); s.execute(); } }; final Timer timer =new Timer(); 

and here is the start of the timer

  saveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //timer.scheduleAtFixedRate(timerTask,0,3000); timer.scheduleAtFixedRate(timerTask,0,8000); saveBtn.setVisibility(View.INVISIBLE); } }); } 

And how to cancel this timer? Thanks to everyone who responds

    1 answer 1

     timer.cancel(); timer.purge(); 
    • but the other day I cried that you don’t know anything :) - elik
    • Just googled :)))) - Flippy