文章出處
文章列表
Handler的實例化
Handler handler = new Handler();
一般是默認使用當前線程的looper
- 要刷新UI,handler要用主線程的looper,如果當前是主線程
Handler handler = new handler();
如果是在其他線程的話Handler handler = new Handler(Looper.getMainLooper());
- 不刷新UI,只是做消息處理。當前線程如果是主線程,
Handler handler = new Handler()
,不是主線程的話,Looper.prepare(),Handler handler = new Hnadler(); Looper.loop()
。或者使用Handler handler = new Handler(Looper.getMainLooper())
是放到主線程處理。
- 如果實例化的時候用的是
Looper.getMainLooper();
;則是放到UI線程中去處理。 - 由于只有主線程才默認
Looper.perpare(); Looper.loop();
,所以在其他線程中使用的時候要手動寫上這兩行代碼;
文章列表
全站熱搜