Timer Utility for Android
We launched LenX last week and got a lot of feedback. One of them was to include a timer utility to set the exposure time. I looked up a bit and found out that the best way to do this was to use Handler
and create a separate thread.
Usage of the TimerUtility class:
- Call a fixed method of my activity after certain time interval
- Pause the timer if pause button is pressed
- Resumer the timer when resume button is pressed
The first part was pretty easy. Create a Runnable
which invokes the method. Use a Handler
to process this Runnable instance using postDelayed
method and provide the time interval in milliseconds.
For the second part, I looked around a bit and found out that I can remove the Runnable
using ‘removeCallbacks’ method. Now, I was wondering what to do for the third part. I could simply use postDelayed
again to process the Runnable, but I wanted the appropriate time interval. To overcome this, I started to save the system time when it started, paused, resumed etc and calculated elapsed time and used this elapsed time in postDelayed
and processed the Runnable.
TimerUtility Class
We also added some animation with the timer, but that content is for some other post. I have added a simple working demo showing the usage of TimerUtility
on GitHub. You can fork it here.
P.S. So much to do with LenX. I am overwhelmed, but it’s fun!
Playing around with Android UI
Articles focusing on Android UI - playing around with ViewPagers, CoordinatorLayout, meaningful motions and animations, implementing difficult customized views, etc.