Get remaining time when using handler.postDelayed

风格不统一 提交于 2019-12-24 01:48:18

问题


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:


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;


来源:https://stackoverflow.com/questions/25285326/get-remaining-time-when-using-handler-postdelayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!