使用 Looper.getMainLooper().thread === Thread.currentThread() 进行检测是否为主线程
if (Looper.getMainLooper().thread === Thread.currentThread()){
println("主线程")
// 更新UI
// ...
}else{
println("子线程")
Handler(Looper.getMainLooper()).post {
// 更新UI
// ...
}
}
该篇博客探讨了如何使用Java进行主线程检测,通过`Looper.getMainLooper().thread===Thread.currentThread()`来确认当前是否为主线程。当处于主线程时,直接更新UI;否则,通过Handler在主线程中执行UI更新任务,确保UI操作的正确性。
1424

被折叠的 条评论
为什么被折叠?



