什么是Handler(二)
生活随笔
收集整理的這篇文章主要介紹了
什么是Handler(二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1. 如何下載Android源代碼
2. ThreadLocal初步介紹
3. Handler相關(guān)代碼執(zhí)行流程
?吧
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一一對應(yīng), 一個(gè)Looper存在于一個(gè)Thread中
?
3. Handler相關(guān)代碼執(zhí)行流程
查看相關(guān)方法的具體代碼, 可知
1 class LooperThread extends Thread { 2 public Handler mHandler; 3 public void run() { 4 Looper.prepare(); //生成Looper對象, 并把Looper與當(dāng)前線程對象形成鍵值對, 存放在ThreadLocald 當(dāng)中 5 mHandler = new Handler() //生成Handler對象, 調(diào)用Loop的MyLooper方法,根據(jù)鍵得到與Handler對應(yīng)的Looper對象取出來(從ThreadLocal中取出) 6 { 7 //實(shí)際上,Looper和Handler之間是通過ThreadLocal建立一一對應(yīng)的連接 8 public void handleMessage(Message msg) { 9 // process incoming messages here 10 } 11 }; 12 Looper.loop(); //循環(huán)從消息隊(duì)列往外讀數(shù)據(jù) 13 } 14 }?
轉(zhuǎn)載于:https://www.cnblogs.com/iMirror/p/3962461.html
總結(jié)
以上是生活随笔為你收集整理的什么是Handler(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 经典游戏“大富翁4”存档文件修改器Ric
- 下一篇: 维信小贷发布“蜂鸟”大数据风控云平台