Get remaining time when using handler.postDelayed

2019-07-15 16:26发布

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?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-07-15 16:36

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;
查看更多
登录 后发表回答