sandeep kolhal
sandeep kolhal

Reputation: 423

How to use Timer.scheduleAtFixedRate in Kotlin

I am new in kotlin android development. So I want to know how to use

inline fun Timer.scheduleAtFixedRate(
    delay: Long,
    period: Long,
    crossinline action: TimerTask.() -> Unit
): TimerTask

Means any example....

Thanks in advance.

Upvotes: 8

Views: 16008

Answers (2)

K1521
K1521

Reputation: 31

you can also do this:

val fixedRateTimer = fixedRateTimer(name = "hello-timer",
        initialDelay = 1000, period = 1000,daemon = true) {
        println("Hello")
    }

Upvotes: 3

AskNilesh
AskNilesh

Reputation: 69689

Try this

    Timer().schedule(object : TimerTask() {
        override fun run() {
            Log.e("NIlu_TAG","Hello World")
        }
    }, 3000)

Or this one

Timer().schedule(timerTask {
            Log.e("NIlu_TAG","Hello World")
        }, 3000)

Or this one

    Timer().scheduleAtFixedRate(object : TimerTask() {
        override fun run() {
            Log.e("NIlu_TAG","Hello World")
        }
    },2000,2)

Short answer

Timer().scheduleAtFixedRate(timerTask {
        Log.e("NIlu_TAG","Hello World")
    },2000,2)

Upvotes: 17

Related Questions