SubwayTooter-Android-App/app/src/main/java/jp/juggler/util/LooperUtils.kt

24 lines
731 B
Kotlin
Raw Normal View History

2018-12-01 00:02:18 +01:00
package jp.juggler.util
import android.os.Handler
import android.os.Looper
////////////////////////////////////////////////////////////////////
// threading
val isMainThread : Boolean get() = Looper.getMainLooper().thread === Thread.currentThread()
// メインスレッドから呼び出された場合、それがsynchronizedの中だと GlobalScope.launch を使うのは良くない
2018-12-01 00:02:18 +01:00
fun runOnMainLooper(proc : () -> Unit) {
val looper = Looper.getMainLooper()
if(looper.thread === Thread.currentThread()) {
proc()
} else {
Handler(looper).post { proc() }
}
}
fun runOnMainLooperDelayed(delayMs : Long, proc : () -> Unit) {
Handler(Looper.getMainLooper()).postDelayed({ proc() }, delayMs)
2018-12-01 00:02:18 +01:00
}