Android: get time to run Runnable through Handler

I have a timer in my application that closes the application implemented using the handler, to which I send the delayed runnable "quit". When the user clicks the timer icon, he should also show how much time is left. How can I get this data? Should I implement an object that will take seconds and use this data?

+4
source share
1 answer

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.

+4
source

Source: https://habr.com/ru/post/1398125/


All Articles