The following code schedules a Runnable
for immediate execution if this Runnable
is already scheduled:
public void onSomeEvent() {
if (handler.hasCallbacks(runnable)) {
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 0); //should be executed immediately
}
}
private final Handler handler = new Handler();
private final Runnable runnable = this::doSomething;
public void onResume() {
handler.postDelayed(runnable, 5000);
}
Is there a method setDelayOfCallback
to change the delay directly, like this?
public void onSomeEvent() {
if (handler.hasCallbacks(runnable)) {
handler.setDelayOfCallback(runnable, 0); //should be executed immediately
}
}
The following code schedules a Runnable for immediate execution if this Runnable is already scheduled
I would describe it as scheduling the Runnable
to run with zero added delay. How soon the Runnable
runs will depend on what else you do in the main application thread before returning control to the framework, and what else is in the main application thread's work queue.
Note that you could use post()
instead of postDelayed()
with a 0
parameter.
This code would run the Runnable
immediately:
public void onSomeEvent() {
if (handler.hasCallbacks(runnable)) {
handler.removeCallbacks(runnable);
runnable.run();
}
}
Is there a method setDelayOfCallback to change the delay directly, like this?
No, sorry, there is no such method on Handler
.