16

I'm using postDelayed method of the Handler in order to perform an action after certain amount of time:

private static int time_to_wait = 2000;

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  public void run() {
    // Make Action
  }
}, time_to_wait);

now in the middle of the waiting time i want to change the value of the remaining milliseconds due to some processing results, let's say it now waited 1000 ms and i want to make it begins to count from 2000 again, So, i set the time_to_wait value to 2000 but it doesn't take that in count as it only takes the var value (2000) and just waits that time regardless changing the time_to_wait value to any other value.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • 1
    You need to cancel and repost the runnable I believe. [StackOverflow][1] [1]: http://stackoverflow.com/questions/7407242/how-to-cancel-handler-postdelayed – Kaediil Apr 03 '14 at 13:53
  • @Kaediil that won't be so efficient in my case – Muhammed Refaat Apr 03 '14 at 14:01
  • `int` is a primitive type and is passed by value - the value is copied, and is not connected with original primitive variable anyhow. If you pass `int` variable to some method, and then try to change that variable somewhere else (outside that method), the changes to the variable will not be reflected in this method, as it possesses it's own copy of variable – Drew Apr 03 '14 at 15:15
  • @Drew well, Great, now if I want to do what i explained, do you have any idea how ? – Muhammed Refaat Apr 06 '14 at 07:29
  • 1
    @MuhammedRefaat store a reference to your Runnable, declaring it as `Runnable r = new Runnable(...);`. You will post it like: `handler.postDelayed(r, time_to_wait);`. You could cancel it in the meantime by calling `handler.removeCallbacks(r);`. And then post it again, depending on your app's logic. Hope that helps. – Drew Apr 07 '14 at 06:42
  • [This answer](https://stackoverflow.com/questions/46403060/restart-a-handler-postdelayed-in-java/51984070#51984070) could be helpful for you. – Olga Konoreva Aug 27 '18 at 10:20
  • @OlgaKonoreva yeah it's the same technique I used in the answer here, something else, I believe the question you mentioned as mine is duplicate of should be the one that is duplicate for mine, as it has been asked after mine by more than three years :D – Muhammed Refaat Aug 27 '18 at 15:20
  • 1
    @MuhammedRefaat Yeap, you are right :) I'm removing my flag. – Olga Konoreva Aug 28 '18 at 12:04

3 Answers3

31

this can be achieved by easily create a runnable that will be displayed by the handler, then creating the handler as static member, finally when you want to stop it just remove the callback of your created runnable, and if you want to restart it you have to remove the callback and assign it again:

Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        // your code here
    }
};

public static Handler myHandler = new Handler();
private static final int TIME_TO_WAIT = 2000;

public void start() {
    myHandler.postDelayed(myRunnable, TIME_TO_WAIT);
}

public void stop() {
    myHandler.removeCallbacks(myRunnable);
}

public void restart() {
    myHandler.removeCallbacks(myRunnable);
    myHandler.postDelayed(myRunnable, TIME_TO_WAIT);
}
Graham
  • 7,431
  • 18
  • 59
  • 84
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • when using `removeCallbacks` what will happen if `myRunnable` wasn't running. Will the application crash? – Mohamad Mousheimish Apr 17 '20 at 10:57
  • @MohamadMousheimish I think it will not, as if the runnable is not running it will do nothing, but I'm just guessing and I'm not pretty sure about that, better to surround it with try-catch block or make a check before running it – Muhammed Refaat Apr 19 '20 at 06:21
0

you can just try

handler.removeMessage(what)
handler.sendMessageDelayed(time_to_wait + INTERVER);

every time you need first remove message and then send a new message.

whb_zju
  • 133
  • 1
  • 1
  • 10
0

KOTLIN VERSION: (#Muhammed Refaat's Answer):

var myHandler = Handler()
private val TIME_TO_WAIT: Long = 2000

var myRunnable = Runnable {
    // your code here
}

fun start() {
    myHandler.postDelayed(myRunnable, TIME_TO_WAIT)
}

fun stop() {
    myHandler.removeCallbacks(myRunnable)
}

fun restart() {
    stop()
    start()
}
Irfan Anwar
  • 1,878
  • 17
  • 30