什么是Handler(二)
生活随笔
收集整理的這篇文章主要介紹了
什么是Handler(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 如何下載Android源代碼
2. ThreadLocal初步介紹
3. Handler相關代碼執行流程
?吧
1. 如何下載Android源代碼
SDK Manager下載, 下圖所示 ??
?????
下載完之后在文件夾
?
2. ThreadLocal初步介紹
線程本地變量
代碼回顧
1 <pre> 2 * class LooperThread extends Thread { 3 * public Handler mHandler; 4 * 5 * public void run() { 6 * Looper.prepare(); 7 * 8 * mHandler = new Handler() { 9 * public void handleMessage(Message msg) { 10 * // process incoming messages here 11 * } 12 * }; 13 * 14 * Looper.loop(); 15 * } 16 * }</pre>?
? Looper、Thread、Handler一一對應, 一個Looper存在于一個Thread中
?
3. Handler相關代碼執行流程
查看相關方法的具體代碼, 可知
1 class LooperThread extends Thread { 2 public Handler mHandler; 3 public void run() { 4 Looper.prepare(); //生成Looper對象, 并把Looper與當前線程對象形成鍵值對, 存放在ThreadLocald 當中 5 mHandler = new Handler() //生成Handler對象, 調用Loop的MyLooper方法,根據鍵得到與Handler對應的Looper對象取出來(從ThreadLocal中取出) 6 { 7 //實際上,Looper和Handler之間是通過ThreadLocal建立一一對應的連接 8 public void handleMessage(Message msg) { 9 // process incoming messages here 10 } 11 }; 12 Looper.loop(); //循環從消息隊列往外讀數據 13 } 14 }?
轉載于:https://www.cnblogs.com/iMirror/p/3962461.html
總結
以上是生活随笔為你收集整理的什么是Handler(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 经典游戏“大富翁4”存档文件修改器Ric
- 下一篇: html5 canvas实现图片玻璃碎片