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

后端 未结 11 2309
青春惊慌失措
青春惊慌失措 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:23

    now in Kotlin you can run threads this way:

    class SimpleRunnable: Runnable {
        public override fun run() {
            println("${Thread.currentThread()} has run.")
        }
    }
    fun main(args: Array) {
        val thread = SimpleThread()
        thread.start() // Will output: Thread[Thread-0,5,main] has run.
        val runnable = SimpleRunnable()
        val thread1 = Thread(runnable)
        thread1.start() // Will output: Thread[Thread-1,5,main] has run
    }
    

提交回复
热议问题