Simple realisation of Stopwatch.
add jitpack.io repository to the top build.gradle or settings.gradle
repositories {
// ...
maven { url 'https://jitpack.io' }
}
add the dependency to your module build.gradle
dependencies {
//...
implementation('com.github.fmaxx:AndroidStopwatch:0.0.2')
}
// initialization
val stopwatch = Stopwatch.Builder(scope = YOUR_SCOPE).build()
// collect data with Flow
YOUR_SCOPE.launch {
stopwatch.collect { elapsedMilliseconds ->
println("elapsed: $elapsedMilliseconds")
}
}
stopwatch.start()
override fun onCreate(savedInstanceState: Bundle?) {
val stopwatch = Stopwatch.Builder(scope = lifecycleScope).build()
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
stopwatch.collect { elapsedMilliseconds ->
println("elapsed: $elapsedMilliseconds")
}
}
}
stopwatch.start()
}
Stopwatch.Builder has additional methods
setTickDelay(milliseconds: Long)
- milliseconds between each time tick (should be more than zero)
setStartMilliseconds(milliseconds: Long)
- milliseconds started at (should be more than zero)
val stopwatch = Stopwatch.Builder(scope = YOUR_SCOPE)
.setTickDelay(20)
.setStartMilliseconds(10_000)
.build()