How to pause / sleep thread or process in Android?

后端 未结 12 973
故里飘歌
故里飘歌 2020-11-22 05:49

I want to make a pause between two lines of code, Let me explain a bit:

-> the user clicks a button (a card in fact) and I show it by changing the background of thi

12条回答
  •  青春惊慌失措
    2020-11-22 06:50

    If you use Kotlin and coroutines, you can simply do

    GlobalScope.launch {
       delay(3000) // In ms
       //Code after sleep
    }
    

    And if you need to update UI

    GlobalScope.launch {
      delay(3000)
      GlobalScope.launch(Dispatchers.Main) {
        //Action on UI thread
      }
    }
    

提交回复
热议问题