<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > Android之Handler消息機(jī)制

          Android之Handler消息機(jī)制

          作者: 時間:2016-10-10 來源:網(wǎng)絡(luò) 收藏

          Android中Handle類的主要作用:

          本文引用地址:http://www.ex-cimer.com/article/201610/306009.htm

          1.在新啟動的線程中發(fā)送給消息

          2.在主線程獲取、處理消息

          為什么要用Handle這樣的一個機(jī)制:

          因為在Android系統(tǒng)中UI操作并不是線程安全的,如果多個線程并發(fā)的去操作同一個組件,可能導(dǎo)致線程安全問題。為了解決這一個問題,制定了一條規(guī)則:只允許UI線程來修改UI組件的屬性等,也就是說必須單線程模型,這樣導(dǎo)致如果在UI界面進(jìn)行一個耗時叫長的數(shù)據(jù)更新等就會形成程序假死現(xiàn)象 也就是ANR異常,如果20秒中沒有完成程序就會強(qiáng)制關(guān)閉。所以比如另一個線程要修改UI組件的時候,就需要借助Handler消息機(jī)制了。

          Handle發(fā)送和處理消息的幾個方法:

          1. void handleMessage(Message msg):處理消息的方法,該方法通常被重寫。

          2.final boolean hasMessage(int what):檢查消息隊列中是否包含有what屬性為指定值的消息

          3.final boolean hasMessage(int what ,Object object) :檢查消息隊列中是否包含有what好object屬性指定值的消息

          4.sendEmptyMessage(int what):發(fā)送空消息

          5.final Boolean send EmptyMessageDelayed(int what ,long delayMillis):指定多少毫秒發(fā)送空消息

          6.final boolean sendMessage(Message msg):立即發(fā)送消息

          7.final boolean sendMessageDelayed(Message msg,long delayMillis):多少秒之后發(fā)送消息

          與Handle工作的幾個組件Looper、MessageQueue各自的作用:

          1.Handler:它把消息發(fā)送給Looper管理的MessageQueue,并負(fù)責(zé)處理Looper分給它的消息

          2.MessageQueue:采用先進(jìn)的方式來管理Message

          3.Looper:每個線程只有一個Looper,比如UI線程中,系統(tǒng)會默認(rèn)的初始化一個Looper對象,它負(fù)責(zé)管理MessageQueue,不斷的從MessageQueue中取消息,并將

          相對應(yīng)的消息分給Handler處理

          在線程中使用Handler的步驟:

          1.調(diào)用Looper的prepare()方法為當(dāng)前線程創(chuàng)建Looper對象,創(chuàng)建Looper對象時,它的構(gòu)造器會自動的創(chuàng)建相對應(yīng)的MessageQueue

          2.創(chuàng)建Handler子類的實(shí)例,重寫HandleMessage()方法,該方法處理除UI線程以外線程的消息

          3.調(diào)用Looper的loop()方法來啟動Looper

          實(shí)例

          xmlns:tools=http://schemas..com/tools

          :layout_width=match_parent

          android:layout_height=match_parent

          android:paddingBottom=@dimen/activity_vertical_margin

          android:paddingLeft=@dimen/activity_horizontal_margin

          android:paddingRight=@dimen/activity_horizontal_margin

          android:paddingTop=@dimen/activity_vertical_margin

          tools:context=.MainActivity >

          android:id=@+id/ed1

          android:layout_width=match_parent

          android:layout_height=wrap_content

          android:inputType=number />

          android:id=@+id/Ok

          android:layout_width=match_parent

          android:layout_height=wrap_content

          android:layout_below=@id/ed1

          android:text=@string/Ok />

          android:id=@+id/next

          android:layout_width=match_parent

          android:layout_height=wrap_content

          android:layout_below=@id/Ok

          android:text=下一張 />

          android:id=@+id/image1

          android:layout_width=match_parent

          android:layout_height=match_parent

          android:layout_below=@id/next

          android:src=@drawable/a3 />



          關(guān)鍵詞: android handler

          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();