How to run a code block, at current thread, after some delay time???
==========
Maybe simply do like below
runOnNewThread {
Thread.sleep(1000)
// do something after delay 1000 millseconds
}
==========
But if I do like below, whether it has some problems???
[ >>> click to see Source Code >>>]
/***
* if no [Looper] has associated with [currentThread][Thread.currentThread], just to [prepare][Looper.prepare] and call [Looper.loop],
* in this case, it will call [HandlerThread.quitSafely] or [HandlerThread.quit] after [block] invoke;
* and then send message to the [Handler] associated with [currentThread][Thread.currentThread].[looper][Looper.myLooper] to run the [block] after [delay] milliseconds
*/
postOnCurrentThread(1000) {
// do something in current thread after 1000 milliseconds
}
// same with below
if (isMainThread()) {
postOnUiThread(1000){
// do something in current thread after 1000 milliseconds
}
} else {
var needQuitLooper = false
val looper: Looper? = Looper.myLooper().ifNull {
needQuitLooper = true
Looper.prepare()
Looper.myLooper()
}
postOnUiThread {
Handler(looper).postDelayed(1000) {
try {
// do something in current thread after 1000 milliseconds
} finally {
if (needQuitLooper) {
fromSdk(18) {
looper?.quitSafely()
} other {
looper?.quit()
}
}
}
}
}
Looper.loop()
}