0

I'm trying to execute some function repeatedly for every 5 sec & it's working fine.

I wanted this timer to stop when the app is closed or back button is pressed.

  int delay = 0; 
        int period = 5000; // repeat every sec.
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.out.println("done");
            }
        }, delay, period);
    }
Uday
  • 1,619
  • 3
  • 23
  • 48
  • have you tried using a boolean and a while loop ? you can Override onBackPressed() and set the flag to true and also Override onFinish() to handle the app shutting down in a similar fashion. – kandroidj Mar 12 '14 at 13:31
  • Nope i have not tried that. – Uday Mar 12 '14 at 13:35

2 Answers2

3

Don't use a Timer just use a Handler

private boolean keepLooping = true;
private static final int DELAY = 1000 * 5;
final Handler printHandler = new Handler();
Runnable printStuff = new Runnable(){

     @Override
     public void run(){
         System.out.println("done");
         if(keepLooping)
             printHandler.postDelayed(this, DELAY);
     }

}

//wherever you want to start your printing
printHandler.postDelayed(printStuff, DELAY);
keepLooping = true;


//when back is pressed or app is stopped
keepLooping = false;
printHandler.removeCallbacks(printStuff);

postDelayed will run the runnable on the delay you want. If you want it to loop forever just start the handler again from within the runnable. When you want it to stop just removeCallbacks which will prevent the looping.

Rarw
  • 7,645
  • 3
  • 28
  • 46
  • I will try and let u know. – Uday Mar 12 '14 at 13:38
  • Yeah this also works fine for me,But i'm using @Akbun code to stop the timer on Backpress. – Uday Mar 14 '14 at 06:12
  • Timer is just not the recommended way to do this. There are a number of posts if you look around on SO - [like this one](http://stackoverflow.com/questions/20330355/timertask-or-handler) that explain the disadvantages of using a timer. That's why I provided the handler code. It's the correct way to solve this problem. – Rarw Mar 14 '14 at 13:19
  • So, if I have a function that does something, say "getTheMessages()", would I put it directly underneath `printHandler.postDelayed(this, DELAY);`? I also tried to Log.d(TAG, "postDelayed called");, but it logs nothing. I placed my method to get stuff inside that, and it works........but doesn't print my Log message? How come? I'm kind of lost. Lol. Never used Runnable before and I'm intrigued. – DevOpsSauce Dec 20 '20 at 01:37
1

In Android you can override below methods to identify the back press and activity close, then you can have the code to cancel the timer. 1. onDestroy() 2. onBackPressed()

In method implementation you can use below statements to stop. timer.cancel(); timer.purge();

ex:

  @Override
    public void onBackPressed() {
    timer.cancel();
    timer.purge();
    super.onBackPressed();
    }

I guess this would help..

Akbun
  • 13
  • 3