Android App在線程中創(chuàng)建handler的方法講解
相關(guān)概念
1.Handler:可以看做是一個工具類,用來向消息隊列中插入消息的;
2.Thread:所有與Handler相關(guān)的功能都是與Thread密不可分的,Handler會與創(chuàng)建時所在的線程綁定;
3.Message:消息;
4.MessageQueue:消息隊列,對消息進行管理,實現(xiàn)了一個Message鏈表;
5.Looper:消息循環(huán),從MessageQueue中取出Message進行處理;
6.HandlerThread:繼承Thread,實例化時自動創(chuàng)建Looper對象,實現(xiàn)一個消息循環(huán)線程.
在Android開發(fā)中經(jīng)常會使用到線程,一想到線程,一般都會想到:
new Thread(){...}.start();
這樣的方式。這樣如果在一個Activity中多次調(diào)用上面的代碼,那么將創(chuàng)建多個匿名線程,如果這些線程的沒有被銷毀,那肯定會影響性能呢。這個時候我么就想到了android提供的一個異步處理線程的類HandlerThread。
一般Handler的用法
Handler handler = new Handler(){...};
這樣創(chuàng)建的handler是在主線程即UI線程下的Handler,即這個Handler是與UI線程下的默認Looper綁定的(當(dāng)然也只有主線程才能這么干,子線程是干不了的,除非自己創(chuàng)建個looper)。因此,有些時候會占用ui主線程,引起一些問題,所以我們就想到了重新創(chuàng)建個子線程,來處理handler。。。。
使用HandlerThread解決問題
HandlerThread實際上繼承于Thread,只不過它比普通的Thread多了一個Looper。我們可以使用下面的例子創(chuàng)建Handler
HandlerThread thread = new HandlerThread("MyHandlerThread");
thread.start();
創(chuàng)建HandlerThread時要把它啟動了,即調(diào)用start()方法。
接著就是handler的使用,如下:
mHandler = new Handler(thread.getLooper()); //TODO:you can post or send something....
創(chuàng)建Handler時將HandlerThread中的looper對象傳入。那么這個mHandler對象就是與HandlerThread這個線程綁定了(這時就不再是與UI線程綁定了,這樣它處理耗時操作將不會阻塞UI)。
線程中消息處理的流程圖

消息插入隊列的位置由參數(shù)uptimeMillis來確定。
Handler與線程的關(guān)系

1.HandlerThread就是一個封裝了Looper的Thread.
2.Handler會與實例化時所在的線程綁定.
UI線程與子線程通信相關(guān)

1.需要更新UI,則需要使用與主線程綁定的Handler發(fā)送消息,若使用在子線程中創(chuàng)建的Handler則會拋出異常;
2.子線程中實例化Handler對象首先需要調(diào)用Looper.prepare(),否則會拋出異常;
3.調(diào)用Looper.loop()方法消息循環(huán)才會啟動;
使用Handler時一些需要注意的地方
Looper.prepare(),主線程使用handler,系統(tǒng)默認prepare了,子線程中創(chuàng)建handler必須在前面Looper.prepare(),后面加上Looper.loop();
源碼中:
主線程:
在程序啟動的時候,系統(tǒng)已經(jīng)幫我們自動調(diào)用了Looper.prepare()方法。查看ActivityThread中的main()
public static void main(String[] args) {
SamplingProfilerIntegration.start();
CloseGuard.setEnabled(false);
Environment.initForCurrentUser();
EventLogger.setReporter(new EventLoggingReporter());
Process.setArgV0("<pre-initialized>");
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false);
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
AsyncTask.init();
if (false) {
Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG, "ActivityThread"));
}
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
請注意Looper.prepareMainLooper():
public static final void prepareMainLooper() {
prepare();
setMainLooper(myLooper());
if (Process.supportsProcesses()) {
myLooper().mQueue.mQuitAllowed = false;
}
}
子線程:
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare()
handler2 = new Handler();
Looper.loop()
}
}).start();
如果沒有Looper.prepare().會報錯:
Can't create handler inside thread that has not called Looper.prepare()
因為沒looper對象創(chuàng)建
looper.prepare()源碼:
public static final void prepare() {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper());
}
相關(guān)文章
Android使用SharedPreferences存儲XML文件的實現(xiàn)方法
這篇文章主要介紹了Android使用SharedPreferences存儲XML文件的實現(xiàn)方法,實例分析了SharedPreferences類的基本初始化與文件存儲相關(guān)技巧,需要的朋友可以參考下2016-07-07
Android ApplicationInfo 應(yīng)用程序信息的詳解
這篇文章主要介紹了Android ApplicationInfo 應(yīng)用程序信息的詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10
Android自定義View實現(xiàn)QQ運動積分轉(zhuǎn)盤抽獎功能
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)QQ運動積分轉(zhuǎn)盤抽獎功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
android Setting中隱藏項實現(xiàn)原理與代碼
我們都知道做程序員有時會惡搞,就像android中,程序員在setting中就隱藏這樣一項,接下來將詳細介紹,感興趣的朋友可以了解下哦2013-01-01
Android UI 之實現(xiàn)多級樹形列表TreeView示例
這篇文章主要介紹了Android UI 之實現(xiàn)多級列表TreeView示例,TreeView就是在Windows中常見的多級列表樹,有興趣的可以了解一下。2017-03-03

