How to run a Runnable thread in Android at defined intervals?

后端 未结 11 2184
青春惊慌失措
青春惊慌失措 2020-11-22 02:50

I developed an application to display some text at defined intervals in the Android emulator screen. I am using the Handler class. Here is a snippet from my cod

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 03:10

    Kotlin with Coroutines

    In Kotlin, using coroutines you can do the following:

    CoroutineScope(Dispatchers.Main).launch { // Main, because UI is changed
        ticker(delayMillis = 1000, initialDelayMillis = 1000).consumeEach {
            tv.append("Hello World")
        }
    }
    

    Try it out here!

提交回复
热议问题