Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get remaining time when using handler.postDelayed

I am using handler.postDelayed method to create some delay for some animation stuff. Like this:

Handler h = new Handler();
h.postDelayed(new Runnable() {
  @Override
  public void run() {
    // Start Animation.
  }
}, 6000);

Later, How can I get the remaining time until the animation starts?

like image 270
Hemanth Avatar asked Feb 05 '26 11:02

Hemanth


1 Answers

You can simply save the time in a var when you call post delayed

 long startTime = System.nanoTime();
 h.postDelayed(...

and then when you need to check the remaining time you can calculate the elapsed time like

 long elapsedTime = System.nanoTime()-startTime;

So in your case

 long remainingTime = 6000 - elapsedTime;
like image 65
axl coder Avatar answered Feb 06 '26 23:02

axl coder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!