I prefer to use ScheduledExecutorService with ScheduledFuture , these APIs are more efficient and effective than Handler and Timer IMO:
ScheduledExecutorService scheduledTaskExecutor = Executors.newScheduledThreadPool(1); Runnable quitTask = new Runnable(); // schedule quit task in 2 minutes: ScheduledFuture scheduleFuture = scheduledTaskExecutor.schedule(quitTask, 2, TimeUnit.MINUTES); ... ... // At some point in the future, if you want to get how much time left: long timeLeft = scheduleFuture.getDelay(TimeUnit.MINUTES); ... ...
Hope this helps.
yorkw source share