欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android App在線程中創(chuàng)建handler的方法講解

 更新時(shí)間:2016年03月24日 14:25:17   作者:zztt  
這篇文章主要介紹了Android App在線程中創(chuàng)建handler的方法講解,文中同時(shí)講解了handler和線程的關(guān)系以及使用Handler時(shí)一些需要注意的地方,需要的朋友可以參考下

相關(guān)概念
1.Handler:可以看做是一個(gè)工具類(lèi),用來(lái)向消息隊(duì)列中插入消息的;
2.Thread:所有與Handler相關(guān)的功能都是與Thread密不可分的,Handler會(huì)與創(chuàng)建時(shí)所在的線程綁定;
3.Message:消息;
4.MessageQueue:消息隊(duì)列,對(duì)消息進(jìn)行管理,實(shí)現(xiàn)了一個(gè)Message鏈表;
5.Looper:消息循環(huán),從MessageQueue中取出Message進(jìn)行處理;
6.HandlerThread:繼承Thread,實(shí)例化時(shí)自動(dòng)創(chuàng)建Looper對(duì)象,實(shí)現(xiàn)一個(gè)消息循環(huán)線程.

在Android開(kāi)發(fā)中經(jīng)常會(huì)使用到線程,一想到線程,一般都會(huì)想到:

new Thread(){...}.start();

這樣的方式。這樣如果在一個(gè)Activity中多次調(diào)用上面的代碼,那么將創(chuàng)建多個(gè)匿名線程,如果這些線程的沒(méi)有被銷(xiāo)毀,那肯定會(huì)影響性能呢。這個(gè)時(shí)候我么就想到了android提供的一個(gè)異步處理線程的類(lèi)HandlerThread。

一般Handler的用法

Handler handler = new Handler(){...};

這樣創(chuàng)建的handler是在主線程即UI線程下的Handler,即這個(gè)Handler是與UI線程下的默認(rèn)Looper綁定的(當(dāng)然也只有主線程才能這么干,子線程是干不了的,除非自己創(chuàng)建個(gè)looper)。因此,有些時(shí)候會(huì)占用ui主線程,引起一些問(wèn)題,所以我們就想到了重新創(chuàng)建個(gè)子線程,來(lái)處理handler。。。。
使用HandlerThread解決問(wèn)題

HandlerThread實(shí)際上繼承于Thread,只不過(guò)它比普通的Thread多了一個(gè)Looper。我們可以使用下面的例子創(chuàng)建Handler

HandlerThread thread = new HandlerThread("MyHandlerThread");
thread.start();

創(chuàng)建HandlerThread時(shí)要把它啟動(dòng)了,即調(diào)用start()方法。

接著就是handler的使用,如下:

mHandler = new Handler(thread.getLooper());
//TODO:you can post or send something....

創(chuàng)建Handler時(shí)將HandlerThread中的looper對(duì)象傳入。那么這個(gè)mHandler對(duì)象就是與HandlerThread這個(gè)線程綁定了(這時(shí)就不再是與UI線程綁定了,這樣它處理耗時(shí)操作將不會(huì)阻塞UI)。


線程中消息處理的流程圖

2016324142334101.png (558×479)

消息插入隊(duì)列的位置由參數(shù)uptimeMillis來(lái)確定。

Handler與線程的關(guān)系

2016324142408465.png (556×342)

1.HandlerThread就是一個(gè)封裝了Looper的Thread.
2.Handler會(huì)與實(shí)例化時(shí)所在的線程綁定.

UI線程與子線程通信相關(guān)

2016324142428965.png (556×325)

1.需要更新UI,則需要使用與主線程綁定的Handler發(fā)送消息,若使用在子線程中創(chuàng)建的Handler則會(huì)拋出異常;
2.子線程中實(shí)例化Handler對(duì)象首先需要調(diào)用Looper.prepare(),否則會(huì)拋出異常;
3.調(diào)用Looper.loop()方法消息循環(huán)才會(huì)啟動(dòng);
使用Handler時(shí)一些需要注意的地方
Looper.prepare(),主線程使用handler,系統(tǒng)默認(rèn)prepare了,子線程中創(chuàng)建handler必須在前面Looper.prepare(),后面加上Looper.loop();

源碼中:
主線程:
在程序啟動(dòng)的時(shí)候,系統(tǒng)已經(jīng)幫我們自動(dò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"); 
} 

請(qǐng)注意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();

如果沒(méi)有Looper.prepare().會(huì)報(bào)錯(cuò):

Can't create handler inside thread that has not called Looper.prepare()
因?yàn)闆](méi)looper對(duì)象創(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)文章

最新評(píng)論