如果你不帶參數的實例化:Handler handler=new Handler();那么這個會默認用當前線程的Looper對象。
一般而言,如果你的Handler是要用來刷新UI的,那么就需要在主線程下運行。
情況:
1 要 刷新UI,handler要用到主線程的Looper對象。那么在主線程Handler handler=new Handler() 如果在其他非主線程也要滿足這個功能的話,要Handler handler=new Handler(Looper.getMainLooper());
2 不用刷新UI ,只是處理消息。當前消息如果是主線程的話,Handler handler=new Handler ;不知主線程的話,Looper.prepare() Handler handler=new Handler();Looper.loop() 或者Handler handler=new Handle(Looper.getMainLooper());
若是實例化的時候調用Looper.getMainLooper()就表示放到主線程中去處理。
若有不是的話,因為只有UI 線程默認Loop.prepare() Loop.loop()過,其他線程需要手動調用這兩個。否則會報錯。