繼承順序:java.lang.Object-->android.os.Handler
直接子類:AsyncQueryHandler,HttpAuthHandler,SslErrorHandler
public class Handler extends Object
一個(gè)Handler允許你傳遞、處理Message和Runnable對象,通過相關(guān)聯(lián)的thread的消息隊(duì)列(MessageQueue).每個(gè)handler實(shí)例都與一個(gè)thread相關(guān)聯(lián)和特定的消息隊(duì)列。當(dāng)你創(chuàng)建了一個(gè)新的Handler,他就綁定到了這個(gè)thread的thread或者message隊(duì)列,而這個(gè)thread從創(chuàng)建的地點(diǎn)將傳遞消息和runnables到那個(gè)消息隊(duì)列,且處理那些從消息隊(duì)列中出來消息。
有兩種主要的使用方式,1、有計(jì)劃的處理message和runnable,在未來的某個(gè)時(shí)刻。2、在非你的另外的線程上執(zhí)行一個(gè)方法。
計(jì)劃消息使用下列方法處理:post(java.lang.Runnable),postAtTime(Runnable,long),postDelayer(java.lang.Runnable,long),sendEmptyMessage(int),sendMessage(android.os.Message),sendMessageAtTime(android.os.Message,long),和sendMessageDelayed(android.os.Message,long)。消息隊(duì)列調(diào)用post方法允許你講Runnable對象壓入隊(duì)列,當(dāng)被接收的時(shí)候。當(dāng)Handler的handleMessage方法處理一束Message包含的數(shù)據(jù)的時(shí)候,允許你壓入message進(jìn)入隊(duì)列(需要你實(shí)現(xiàn)一個(gè)Handler的子類)。
當(dāng)提交或者傳遞一個(gè)Handler的時(shí)候,你既可以讓item處理與消息隊(duì)列準(zhǔn)備好一樣快,或者指定一個(gè)延遲在他被處理或者被處理前的絕對時(shí)間間隔。后兩者需要你實(shí)現(xiàn)時(shí)間、軌跡和其他的時(shí)間基本行為。
當(dāng)應(yīng)用創(chuàng)建好一個(gè)process,他的主線程決定運(yùn)行一個(gè)消息隊(duì)列,這個(gè)隊(duì)列接管著應(yīng)用最高等級的對象(activities,broadcast,receivers,etc)和他們創(chuàng)建的任意的窗口。你可以創(chuàng)建你的自己的threads和通訊,通過一個(gè)Handler與主線程完成回調(diào)。在完成之前調(diào)用相同的post或者sendMessage方法,但是通過新的thread.給定的Runnable或者M(jìn)essage將被添加到Handler的消息隊(duì)列,且在適當(dāng)?shù)臅r(shí)候完成處理。
一、嵌套類摘要
?Handler.Callback
在你實(shí)例化一個(gè)Handler的時(shí)候你可以使用Callback接口用來避免必須實(shí)現(xiàn)你自己的Handler子類。、
二、構(gòu)造方法摘要
Handler()
默認(rèn)的構(gòu)造方法使得該handler與當(dāng)前的thread隊(duì)列相關(guān)聯(lián)。
Handler(Handler.Callback callback)
與當(dāng)前的thread隊(duì)列想關(guān)聯(lián)且在回調(diào)接口中可以處理message
Handler(Looper looper)
使用自定義的隊(duì)列代替默認(rèn)的隊(duì)列。
Handler(Looper looper,Handler.Callback callback)
使用自定義的隊(duì)列代替默認(rèn)的隊(duì)列且在回調(diào)接口中處理message
三、方法摘要
dispatchMessage(Message msg):處理系統(tǒng)的信息
dump(Printer pw, String prefix)
getLooper()
handleMessage(Message msg)子類必須實(shí)現(xiàn)這個(gè)來接收消息
hasMessage(int what):檢查消息中的what是否含有消息且obj在隊(duì)列中是否是對象。
obtainMessage():從全局message池返回一個(gè)新的Message
obtainMessage(int what):功能同上,除了能夠設(shè)置返回消息中的member屬性
obtainMessage(int what,int arg1,int arg2):功能同obtainMessage(),除了能設(shè)置what arg1, arg2這些屬性。
obtainMessage(int what,int arg1,int arg2,Object obj):功能同不帶參數(shù)的方法,除了能夠設(shè)置what ,obj,arg1,arg2值。
obtainMessage(int what,Object obj):功能同無參數(shù)方法一樣,除了能設(shè)置what和obj屬性值。
post(Runnable r):使得Runnable r添加到消息隊(duì)列。
postAtFrontOfQueue(Runnable r):提交一個(gè)消息到一個(gè)實(shí)現(xiàn)了Runnable的對象
postAtTime(Runnable r,long uptimeMillis):使得Runnable r被添加到消息隊(duì)列,在由uptimeMills給定的特定時(shí)間
postAtTime(Runnable r , Object token, long uptimeMills):使得Runnable r添加到消息隊(duì)列,在由uptimeMills給定的特定時(shí)間運(yùn)行。
postDelayer(Runnable r , long delayMillis):使得Runnable r被添加到隊(duì)列,在由指定的時(shí)間后運(yùn)行。
removeCallbacks(Runnable r):移除在消息隊(duì)列中的沒有提交的Runnable r
removeCallbacks(Runnable r,Object token):移除在消息隊(duì)列中使用Object 的token的沒有提交的Runnable r.
removeCallbacksAndMessage(Object token):移除在回調(diào)函數(shù)中沒有提交的消息且發(fā)送消息的obj是token
voidremoveMessages(int what)
Remove any pending posts of messages with code 'what' that are in the message queue.
voidremoveMessages(int what,Objectobject)
Remove any pending posts of messages with code 'what' and whose obj is 'object' that are in the message queue.
booleansendEmptyMessage(int what)
Sends a Message containing only the what value.
booleansendEmptyMessageAtTime(int what, long uptimeMillis)
Sends a Message containing only the what value, to be delivered at a specific time.
booleansendEmptyMessageDelayed(int what, long delayMillis)
Sends a Message containing only the what value, to be delivered after the specified amount of time elapses.
booleansendMessage(Messagemsg)
Pushes a message onto the end of the message queue after all pending messages before the current time.
booleansendMessageAtFrontOfQueue(Messagemsg)
Enqueue a message at the front of the message queue, to be processed on the next iteration of the message loop.
booleansendMessageAtTime(Messagemsg, long uptimeMillis)
Enqueue a message into the message queue after all pending messages before the absolute time (in milliseconds)uptimeMillis.
booleansendMessageDelayed(Messagemsg, long delayMillis)
Enqueue a message into the message queue after all pending messages before (current time + delayMillis).
Returns a string containing a concise, human-readable description of this object.