-1

I need a delay for around 5 seconds. I have tried using Timer using below code :

            Timer myTimer = new Timer();
                myTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {

                      Log.d(TAG,"Timer");
                    }

                }, 4000, 5000);

When i check logs, the Timer is getting printed thrice. If I change time, sometimes it gets printed in log 4 times as well.

I have tried using Handler as well like below :

   final Handler mHandler = new Handler();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        while (true) {
                            try {
                                Thread.sleep(10000);
                                mHandler.post(new Runnable() {

                                    @Override
                                    public void run() {
                                        // TODO Auto-generated method stub

                                            Log.d(Utility.TAG,"Sleep::");

                                    }
                                });
                            } catch (Exception e) {
                                // TODO: handle exception
                            }
                        }
                    }
                }).start();

But again the log is printing multiple times. I just want to call my method once not multiple times. How can I achieve it ?

EDIT

used handler without thread as well like below :

 final Handler h = new Handler();
               final int delay = 3000; //milliseconds

                h.postDelayed(new Runnable(){
                    public void run(){
                        //do something
                        h.postDelayed(this, delay);
                        Log.d(Utility.TAG,"Sleep ::");
                    }
                }, delay);

But again, Log is getting printed thrice

BLEdevices
  • 11
  • 3
  • just use a `Handler` without any thread – pskink Oct 18 '15 at 07:54
  • where is `final Handler h = new Handler();` in your code? add `Log.d(Utility.TAG,"Start ::");` to see when it is called – pskink Oct 18 '15 at 08:00
  • @pskink i have edited the question. You can see, i have defined the final Handler h above. It is getting called in click listener of an image. – BLEdevices Oct 18 '15 at 08:12
  • 1
    if you are calling `h.postDelayed(new Runnable(){` three times you will get the results like yours, just delete any pending Runnables `removeCallbacks(Runnable r)` before posting new ones – pskink Oct 18 '15 at 08:15
  • @pskink thanks a lot, Your last comment helped me in fixing the issue :) – BLEdevices Oct 18 '15 at 08:19

1 Answers1

0

Your third approach (no Timer, no Thread) is the closest to being correct. It's printing multiple times because the Runnable is re-posting itself every time it runs. If you only want it to run once, remove this line from the run() method:

h.postDelayed(this, delay);
Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82