據我所知,UI線程是主線程。 getMainLooper
根據文檔是UI線程。Android上下文線程檢查
應用程序上下文線程不應該是UI線程嗎?
另外,Fragment的Looper線程也不應該是UI線程?
爲什麼以下失敗(都)?
if (getActivity().getApplicationContext().getMainLooper().getThread() == Looper.getMainLooper().getThread()) {
throw new Exception("Context is not the application's thread");
}
//Inside a Fragment
if (new Handler().getLooper().getThread() == Looper.getMainLooper().getThread()) {
throw new Exception("Handler is not for the main thread");
}
上下文與Thread無關,默認情況下,任何Android應用程序都有一個主UI線程,並且這是唯一的線程應用程序正在運行 – pskink 2014-09-01 19:53:38
@pskink我同意,但從上下文有可能獲得MainLooper,然後它屬於的線程。此線程不應該與UI線程匹配嗎? – igorjrr 2014-09-01 20:16:59
你從Looper.myLooper()得到Looper,而不是從Context中得到 – pskink 2014-09-01 20:23:55