Android run Kotlin Coroutine every x min

Viewed 3068

I am using code below to execute certain task every 5 minutes.

Do coroutines have anything like this built into them?

Is there any better way of doing this?

private val interval = 1000*60*5L
private val handler:Handler = Handler()

val mHandlerTask = object : Runnable {
    override fun run() {
        val id: String = getId(this@HomeActivity)
        settingsViewModel.Settings(id)
        handler.postDelayed(this, interval)
    }
}

Thanks

1 Answers

Yes, you can handle it with a simple delay and a loop.

while(true) {
    task()
    delay(5000)
}

suspend fun task() {...}
Related