Dan Tech Academy

Android Thread: Practicing Code with a Clock Application

Android Thread: Practicing Code with a Clock Application

Dan TechDan TechSoftware Engineer, Weight Lifter1 min readUpdated 08/28/2025
Heads upThis post was last updated 1 year ago.Some details may be out of date. Double-check against current practices.

After the article about Thread in Android, it's time for you to write an Android program using Thread!

The focus of this application will guide you to write the simplest program to display the current time of the device. The tools used are Thread and Activity Lifecycle.

private val clockHandlerThread: HandlerThread = HandlerThread("Clock-Handler-Thread").apply {
    start()
}
private val clockHandler: Handler by lazy {
    Handler(clockHandlerThread.looper)
}
private val mainHandler: Handler = Handler(Looper.getMainLooper())
private val clockRunnable: Runnable = object : Runnable {
    override fun run() {
        mainHandler.post {
            updateTime()
        }
        clockHandler.postDelayed(this, 1000)
    }
}
private fun startClock() {
    updateTime()
    clockHandler.postDelayed(clockRunnable, 1000)
}

You can find the complete source code here.

Reading is fun. A roadmap gets you there.

The Kotlin Android Roadmap sorts the Android Mastery, OOP and Design Patterns posts into 5 levels - from your first line of Kotlin to an app on the store.

Plus a new post every week. Unsubscribe anytime.