in Android, Android API, CountDownTImer, Timer

Using a CountDownTimer for monitoring timed tasks in Android

Intro

Android developers have been faced with the requirement of performing a task within a specified time. Tasks such as file upload or download and a host of other tasks has to be pushed to the background at a specified time. The Android API has provided a lot of options to help out in scenarios like these. Examples of such APIs are Handler and TimerTask. TimerTask allows scheduling of tasks for “one-time or repeated execution by a Timer”. You can read more about them here and here.

For the purpose of this article, we will be reviewing the Android CountDownTimer.

CountDownTimer

As the name suggests, this Android class is used for tasks that needs to be done or completed at a specified time in future. It provides a beautiful way of notifying the “caller” at regular intervals as opposed to a TimerTask that runs its task without notifications. A short demo on how to use this class efficiently is provided below. In this demo, we’ll be creating a sample Android app that updates the screen with the progress in percentage. After the timer elapses, a screen is updated with the appropriate text.

  • Create a new Android project. Use the specifications as seen in the diagram below. Wait till the gradle sync is completed.
Create New Project
  • Create a class that extends the CountDownTimer class. Your class should look like this:
package com.example.countdowntimer

import android.os.CountDownTimer

class NotificationCountDownTimer(millisInFuture: Long, countDownInterval: Long) :
CountDownTimer(millisInFuture, countDownInterval) {
override fun onFinish() {

}

override fun onTick(millisUntilFinished: Long) {

}

}

Variable millisInFuture passed into the class’ constructor is the time, in milliseconds, that this task will be executed, from the call start until the countdown is done . countDownInterval provides the CountDownTimer the specific interval you want to be notified.

  • Create an interface class that will notify our Activity of the progress of the CountDownTimer. The interface class should look like this:
interface TimerUpdateListener {
fun onTimerFinished()
fun onTimerTick(progress: Int)
}

NOTE: We have created separate classes for the CountDownTimer and the interface for simplicity and modularity following the principle of Separation of Concerns and SOLID.

  • Send the interface to the NotificationCountDownTimer class
class NotificationCountDownTimer(millisInFuture: Long, countDownInterval: Long, timerUpdateListener: TimerUpdateListener){
...
}
  • Implement the interface callback in the count down timer class
  • Implement the count down timer class in the MainActivity class.

An util function has been created to convert the time interval to percentage.

When done, the NotificationCountDownTimer class should look like this:

class NotificationCountDownTimer(private val millisInFuture: Long, private val countDownInterval: Long, private val timerUpdateListener: TimerUpdateListener) :
CountDownTimer(millisInFuture, countDownInterval) {
override fun onFinish() {
timerUpdateListener.onTimerFinished()
}

override fun onTick(millisUntilFinished: Long) {
timerUpdateListener.onTimerTick(calculateProgress(millisUntilFinished))
}

private fun calculateProgress(millisUntilFinished: Long): Int {
val subtraction = millisInFuture - millisUntilFinished
val percentage = subtraction.toDouble() / millisInFuture
return (percentage * 100).toInt()
}

}
Screen before timer starts
The screen upon app start.
App screen while timer is running
App screen when timer is completed.

Finally,

Implementing a CountDownTimer is very simple and straightforward. This post has shown how it can be done in the smallest possible way for modularity and re-usability. With this example, a single CountDownTimer can be used in a full app, while the interface callbacks will be implemented by the calling app units.

Still confused? Ask your questions in the comment section below.

Link to full code on Github can be found here

Reference: CountDownTimer|Android Developers https://developer.android.com/reference/android/os/CountDownTimer

Write a Comment

Comment