最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2016年03月24日 14:25:17   作者:zztt  
這篇文章主要介紹了Android App在線程中創(chuàng)建handler的方法講解,文中同時講解了handler和線程的關(guān)系以及使用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綁定的(當然也只有主線程才能這么干,子線程是干不了的,除非自己創(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)。


線程中消息處理的流程圖

2016324142334101.png (558×479)

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

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

2016324142408465.png (556×342)

1.HandlerThread就是一個封裝了Looper的Thread.
2.Handler會與實例化時所在的線程綁定.

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

2016324142428965.png (556×325)

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)文章

最新評論

普安县| 安顺市| 汉沽区| 阿克苏市| 应城市| 奉新县| 布拖县| 龙胜| 句容市| 新河县| 潞城市| 富顺县| 巴青县| 赣榆县| 神木县| 将乐县| 鹰潭市| 时尚| 葵青区| 博客| 舟山市| 荔波县| 礼泉县| 扶风县| 家居| 石景山区| 崇信县| 神农架林区| 乌恰县| 绥江县| 澄迈县| 辽宁省| 同德县| 利津县| 岳池县| 东源县| 故城县| 吴桥县| 满洲里市| 兴安盟| 澜沧|