androidkotlinchronometer

Kotlin pause chronometer at different times


Im trying to develop an app with a chronometer in kotlin and I need to do several things when te chrono achives some conditions, but I can't do it and I don't know why

This is the setOnChronometerTickListener of my app

chrono?.setOnChronometerTickListener {
        if (chrono?.base == 180000L && !inPriority) {
            startStopChrono()
            showWinner()
        } else if (chrono?.base? == 30000L && inPriority) {
            println("EEEEEEEEE")
            startStopChrono()
            showWinner()
        }
    }

The problem is when the chrono is in 30 seconds and the condition is true, don't run the methods, and also with the 3 minutes condition

Thanks everyone


Solution

  • I think that you shouldn't use base, here is the solution:

    chrono?.setOnChronometerTickListener {
        val elapsed: Long = SystemClock.elapsedRealtime() - it.base
        if (elapsed >= 180000L) {
            println("Elapsed 3 minutes")
        } else if (elapsed >= 30000L) {
            println("Elapsed 30 seconds")
        }
    }
    

    According to Documentation getBase() return time from setBase(), if not set it will return SystemClock.elapsedRealtime as time base.