Hemanth
Hemanth

Reputation: 2737

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?

Upvotes: 3

Views: 3817

Answers (1)

axl coder
axl coder

Reputation: 749

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;

Upvotes: 4

Related Questions