文章出處

Handler的實例化

Handler handler = new Handler();一般是默認使用當前線程的looper

  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 Hnadler(); Looper.loop()。或者使用Handler handler = new Handler(Looper.getMainLooper())是放到主線程處理。
  • 如果實例化的時候用的是Looper.getMainLooper();;則是放到UI線程中去處理。
  • 由于只有主線程才默認Looper.perpare(); Looper.loop();,所以在其他線程中使用的時候要手動寫上這兩行代碼;

文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()