Android中消息处理机制研究

消息机制概述

Android应用程序的每一个线程在启动时,都可以首先在内部创建一个消息队列,然后再进入一个无限循环中,不断检查它的消息队列是否有新的消息需要处理,如果有新的消息需要处理,那么线程就会将它从消息队列中取出来,并且对它进行处理;否则线程就会进入睡眠等待状态,直到有新的消息需要处理为止,这样就可以通过消息来驱动Android应用程序的执行。

消息机制组成部分

Android系统主要通过Messagequeue,Looper,Handler三个类来实现Android应用程序的消息处理机制:

1.Messagequeue:描述消息队列

2.Looper:创建消息队列,以及进入消息循环

3.Handler:用来发送消息和处理消息

消息处理流程

1、程序启动的时候,主线程会创建一个Looper对象。Looper对象初始化一个MessageQueue,然后调用loop()方法循环去读取消息。

2、初始化Handler的时候,在Handler的构造函数内部,会获取当前线程的Looper对象,进而获取MessageQueue对象。由此可见,想要操作UI的Handler必须在主线程中创建。否则会提示你:【”Can’tcreate handler inside thread that has not called Looper.prepare()”】

3、调用Handler的相关方法时,会获取Message对象,将消息对象的target指向当前handler对象,然后放到消息队列中。

4、loop()工作中,会从消息队列中获取一个个的消息,调用handle的dispatchMessage(msg)分发处理。

5、Message内部维护一个消息池,用来回收缓存message对象。

6、Looper相当于一个发动机,MessageQueue相当于流水线,Message相当于一个个的物品,而Handler就相当于工人。

消息循环机制机构图

消息循环机制java层与c++层关系图

创建线程消息队列

1.创建Java层Looper对象:

[java]view plaincopy

privatestaticvoidprepare(booleanquitAllowed) {

if(sThreadLocal.get() !=null) {

thrownewRuntimeException("Only one Looper may be created per thread");

}

sThreadLocal.set(newLooper(quitAllowed));//创建looper对象

}

2.Looper对象内部创建一个MessageQueue对象(mQueue):

[java]view plaincopy

privateLooper(booleanquitAllowed) {

//Looper在创建的时候会创建一个MessageQueue对象

mQueue =newMessageQueue(quitAllowed);

mThread = Thread.currentThread();

}

3.调用MessageQueue的nativeInit方法创建一个NativeMessageQueue对象:

[java]view plaincopy

MessageQueue(booleanquitAllowed) {

mQuitAllowed = quitAllowed;

mPtr = nativeInit();//..

}

4.nativeInit方法返回NativeMessageQueue地址给mPtr:

[cpp]view plaincopy

staticjlong android_os_MessageQueue_nativeInit(JNIEnv* env, jclass clazz) {

//在C++层通过此方法创建一个NativeMessageQueue对象

NativeMessageQueue* nativeMessageQueue =newNativeMessageQueue();

if(!nativeMessageQueue) {

jniThrowRuntimeException(env,"Unable to allocate native queue");

return0;

}

nativeMessageQueue->incStrong(env);

//返回nativeMessageQueue地址给Java层;

returnreinterpret_cast(nativeMessageQueue);

}

5.NativeMessageQueue创建时内部创建一个C++层Looper(Native)对象:

[cpp]view plaincopy

NativeMessageQueue::NativeMessageQueue() : mPollEnv(NULL), mPollObj(NULL), mExceptionObj(NULL) {

//NativeMessageQueue创建时会创建一个Looper(Native)对象

mLooper = Looper::getForThread();

if(mLooper == NULL) {

mLooper =newLooper(false);

Looper::setForThread(mLooper);

}

}

6.Looper(Native)创建时内部创建一个管道通过两个文件描述符管理它:

[cpp]view plaincopy

Looper::Looper(boolallowNonCallbacks) : mAllowNonCallbacks(allowNonCallbacks), mResponseIndex(0) {

intwakeFds[2];

intresult = pipe(wakeFds);//创建一个管道

LOG_ALWAYS_FATAL_IF(result != 0,"Could not create wake pipe.  errno=%d", errno);

mWakeReadPipeFd = wakeFds[0];//读端文件描述符

mWakeWritePipeFd = wakeFds[1];//写端文件描述符

result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);

LOG_ALWAYS_FATAL_IF(result != 0,"Could not make wake read pipe non-blocking.  errno=%d",errno);

result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);

LOG_ALWAYS_FATAL_IF(result != 0,"Could not make wake write pipe non-blocking.  errno=%d",errno);

#ifdef LOOPER_USES_EPOLL

// Allocate the epoll instance and register the wake pipe.

mEpollFd = epoll_create(EPOLL_SIZE_HINT);//..

LOG_ALWAYS_FATAL_IF(mEpollFd < 0,"Could not create epoll instance.  errno=%d", errno);

structepoll_event eventItem;

memset(& eventItem, 0,sizeof(epoll_event));// zero out unused members of data field union

eventItem.events = EPOLLIN;

eventItem.data.fd = mWakeReadPipeFd;

//将文件描述符放在epoll中进行管理

result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, & eventItem);

LOG_ALWAYS_FATAL_IF(result != 0,"Could not add wake read pipe to epoll instance.  errno=%d",errno);

#else

// Add the wake pipe to the head of the request list with a null callback.

structpollfd requestedFd;

requestedFd.fd = mWakeReadPipeFd;

requestedFd.events = POLLIN;

mRequestedFds.push(requestedFd);

Request request;

request.fd = mWakeReadPipeFd;

request.callback = NULL;

request.ident = 0;

request.data = NULL;

mRequests.push(request);

mPolling =false;

mWaiters = 0;

#endif

#ifdef LOOPER_STATISTICS

mPendingWakeTime = -1;

mPendingWakeCount = 0;

mSampledWakeCycles = 0;

mSampledWakeCountSum = 0;

mSampledWakeLatencySum = 0;

mSampledPolls = 0;

mSampledZeroPollCount = 0;

mSampledZeroPollLatencySum = 0;

mSampledTimeoutPollCount = 0;

mSampledTimeoutPollLatencySum = 0;

#endif

}

消息循环过程

1.Looper获取当前线程MessageQueue并循环调用它的next方法检查是否有新消息需要处理:

[java]view plaincopy

publicstaticvoidloop() {

finalLooper me = myLooper();//获取当前线程looper

if(me ==null) {

thrownewRuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");

}

finalMessageQueue queue = me.mQueue;//获取当前线程MessageQueue

// Make sure the identity of this thread is that of the local process,

// and keep track of what that identity token actually is.

Binder.clearCallingIdentity();

finallongident = Binder.clearCallingIdentity();

for(;;) {//不断检查是否有新消息需要处理

Message msg = queue.next();// might block

if(msg ==null) {

// No message indicates that the message queue is quitting.

return;

}

// This must be in a local variable, in case a UI event sets the logger

Printer logging = me.mLogging;

if(logging !=null) {

logging.println(">>>>> Dispatching to "+ msg.target +" "+

msg.callback +": "+ msg.what);

}

//msg.target指向一个Handler对象,调用Handler的dispatchMessage方法分发消息

msg.target.dispatchMessage(msg);

if(logging !=null) {

logging.println("<<<<< Finished to "+ msg.target +" "+ msg.callback);

}

// Make sure that during the course of dispatching the

// identity of the thread wasn't corrupted.

finallongnewIdent = Binder.clearCallingIdentity();

if(ident != newIdent) {

Log.wtf(TAG,"Thread identity changed from 0x"

+ Long.toHexString(ident) +" to 0x"

+ Long.toHexString(newIdent) +" while dispatching to "

+ msg.target.getClass().getName() +" "

+ msg.callback +" what="+ msg.what);

}

msg.recycleUnchecked();

}

}

2.MessageQueue的next方法中调用nativePollOnce函数检查当前线程的消息队列中是否有新消息要处理,如果有消息会存在mMessage中并进行处理:

[java]view plaincopy

Message next() {

// Return here if the message loop has already quit and been disposed.

// This can happen if the application tries to restart a looper after quit

// which is not supported.

finallongptr = mPtr;

if(ptr ==0) {

returnnull;

}

intpendingIdleHandlerCount = -1;// -1 only during first iteration

intnextPollTimeoutMillis =0;//当前线程需要进入睡眠等待状态的时间

for(;;) {//不断调用成员函数nativePollOnce来检查当前线程的消息队列是否有新消息要处理

if(nextPollTimeoutMillis !=0) {

Binder.flushPendingCommands();

}

nativePollOnce(ptr, nextPollTimeoutMillis);//..

synchronized(this) {

// Try to retrieve the next message.  Return if found.

finallongnow = SystemClock.uptimeMillis();

Message prevMsg =null;

Message msg = mMessages;//当前线程需要处理的消息

if(msg !=null&& msg.target ==null) {

// Stalled by a barrier.  Find the next asynchronous message in the queue.

do{

prevMsg = msg;

msg = msg.next;

}while(msg !=null&& !msg.isAsynchronous());

}

if(msg !=null) {

if(now < msg.when) {

// Next message is not ready.  Set a timeout to wake up when it is ready.

nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);

}else{

// Got a message.

mBlocked =false;

if(prevMsg !=null) {

prevMsg.next = msg.next;

}else{

mMessages = msg.next;

}

msg.next =null;

if(DEBUG) Log.v(TAG,"Returning message: "+ msg);

msg.markInUse();

returnmsg;

}

}else{

// No more messages.

nextPollTimeoutMillis = -1;//没有消息就睡觉

}

// Process the quit message now that all pending messages have been handled.

if(mQuitting) {

dispose();

returnnull;

}

// If first time idle, then get the number of idlers to run.

// Idle handles only run if the queue is empty or if the first message

// in the queue (possibly a barrier) is due to be handled in the future.

if(pendingIdleHandlerCount <0

&& (mMessages ==null|| now < mMessages.when)) {

pendingIdleHandlerCount = mIdleHandlers.size();

}

if(pendingIdleHandlerCount <=0) {

// No idle handlers to run.  Loop and wait some more.

mBlocked =true;

continue;

}

if(mPendingIdleHandlers ==null) {

mPendingIdleHandlers =newIdleHandler[Math.max(pendingIdleHandlerCount,4)];

}

mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);

}

// Run the idle handlers.

// We only ever reach this code block during the first iteration.

for(inti =0; i < pendingIdleHandlerCount; i++) {

finalIdleHandler idler = mPendingIdleHandlers[i];

mPendingIdleHandlers[i] =null;//release the reference to the handler

booleankeep =false;

try{

keep = idler.queueIdle();

}catch(Throwable t) {

Log.wtf(TAG,"IdleHandler threw exception", t);

}

if(!keep) {

synchronized(this) {

mIdleHandlers.remove(idler);

}

}

}

// Reset the idle handler count to 0 so we do not run them again.

pendingIdleHandlerCount =0;

// While calling an idle handler, a new message could have been delivered

// so go back and look again for a pending message without waiting.

nextPollTimeoutMillis =0;

}

}

3.在nativePollOnce函数中调用Looper(Native)的pollOnce函数不断检查是否有新消息要处理:

[cpp]view plaincopy

staticvoidandroid_os_MessageQueue_nativePollOnce(JNIEnv* env, jobject obj,jlong ptr, jint timeoutMillis) {

//通过ptr找到NativeMessageQueue

NativeMessageQueue* nativeMessageQueue =reinterpret_cast(ptr);

//调用nativeMessageQueue对象的pollOnce函数检查当前线程是否有新消息

nativeMessageQueue->pollOnce(env, obj, timeoutMillis);

}

4.在pollOnce函数中调用polllnner函数(返回值不等于0即有新消息):

[cpp]view plaincopy

voidNativeMessageQueue::pollOnce(JNIEnv* env, jobject pollObj,inttimeoutMillis) {

mPollEnv = env;

mPollObj = pollObj;

//调用Looper(Native)的pollOnce函数检查当前线程是否有新消息要处理

mLooper->pollOnce(timeoutMillis);

mPollObj = NULL;

mPollEnv = NULL;

if(mExceptionObj) {

env->Throw(mExceptionObj);

env->DeleteLocalRef(mExceptionObj);

mExceptionObj = NULL;

}

}

----------------------------------------------------------分割线------------------------------------------------------------------

[java]view plaincopy

intLooper::pollOnce(inttimeoutMillis,int* outFd,int* outEvents,void** outData) {

intresult =0;

for(;;) {//不断调用pollInner方法检查是否有新消息

while(mResponseIndex < mResponses.size()) {

constResponse& response = mResponses.itemAt(mResponseIndex++);

if(! response.request.callback) {

#ifDEBUG_POLL_AND_WAKE

LOGD("%p ~ pollOnce - returning signalled identifier %d: "

"fd=%d, events=0x%x, data=%p",this,

response.request.ident, response.request.fd,

response.events, response.request.data);

#endif

if(outFd != NULL) *outFd = response.request.fd;

if(outEvents != NULL) *outEvents = response.events;

if(outData != NULL) *outData = response.request.data;

returnresponse.request.ident;

}

}

if(result !=0) {

#ifDEBUG_POLL_AND_WAKE

LOGD("%p ~ pollOnce - returning result %d",this, result);

#endif

if(outFd != NULL) *outFd =0;

if(outEvents != NULL) *outEvents = NULL;

if(outData != NULL) *outData = NULL;

returnresult;

}

result = pollInner(timeoutMillis);//如果有新消息返回值不等于0

}

}

5.polllnner函数中调用awoken方法把管道中的旧数据清理掉:

[cpp]view plaincopy

intLooper::pollInner(inttimeoutMillis) {

#if DEBUG_POLL_AND_WAKE

LOGD("%p ~ pollOnce - waiting: timeoutMillis=%d",this, timeoutMillis);

#endif

intresult = ALOOPER_POLL_WAKE;

mResponses.clear();

mResponseIndex = 0;

#ifdef LOOPER_STATISTICS

nsecs_t pollStartTime = systemTime(SYSTEM_TIME_MONOTONIC);

#endif

#ifdef LOOPER_USES_EPOLL

structepoll_event eventItems[EPOLL_MAX_EVENTS];

inteventCount = epoll_wait(mEpollFd, eventItems, EPOLL_MAX_EVENTS, timeoutMillis);

boolacquiredLock =false;

#else

// Wait for wakeAndLock() waiters to run then set mPolling to true.

mLock.lock();

while(mWaiters != 0) {

mResume.wait(mLock);

}

mPolling =true;

mLock.unlock();

size_trequestedCount = mRequestedFds.size();

inteventCount = poll(mRequestedFds.editArray(), requestedCount, timeoutMillis);

#endif

if(eventCount < 0) {

if(errno == EINTR) {

gotoDone;

}

LOGW("Poll failed with an unexpected error, errno=%d", errno);

result = ALOOPER_POLL_ERROR;

gotoDone;

}

if(eventCount == 0) {

#if DEBUG_POLL_AND_WAKE

LOGD("%p ~ pollOnce - timeout",this);

#endif

result = ALOOPER_POLL_TIMEOUT;

gotoDone;

}

#if DEBUG_POLL_AND_WAKE

LOGD("%p ~ pollOnce - handling events from %d fds",this, eventCount);

#endif

#ifdef LOOPER_USES_EPOLL

for(inti = 0; i < eventCount; i++) {

intfd = eventItems[i].data.fd;

uint32_t epollEvents = eventItems[i].events;

//判断发生IO事件的文件描述符是否与当前线程所关联的管道的mWakeReadPipeFd一致

if(fd == mWakeReadPipeFd) {

if(epollEvents & EPOLLIN) {

awoken();

}else{

LOGW("Ignoring unexpected epoll events 0x%x on wake read pipe.", epollEvents);

}

}else{

if(! acquiredLock) {

mLock.lock();

acquiredLock =true;

}

ssize_t requestIndex = mRequests.indexOfKey(fd);

if(requestIndex >= 0) {

intevents = 0;

if(epollEvents & EPOLLIN) events |= ALOOPER_EVENT_INPUT;

if(epollEvents & EPOLLOUT) events |= ALOOPER_EVENT_OUTPUT;

if(epollEvents & EPOLLERR) events |= ALOOPER_EVENT_ERROR;

if(epollEvents & EPOLLHUP) events |= ALOOPER_EVENT_HANGUP;

pushResponse(events, mRequests.valueAt(requestIndex));

}else{

LOGW("Ignoring unexpected epoll events 0x%x on fd %d that is "

"no longer registered.", epollEvents, fd);

}

}

}

if(acquiredLock) {

mLock.unlock();

}

Done: ;

#else

for(size_ti = 0; i < requestedCount; i++) {

conststructpollfd& requestedFd = mRequestedFds.itemAt(i);

shortpollEvents = requestedFd.revents;

if(pollEvents) {

if(requestedFd.fd == mWakeReadPipeFd) {

if(pollEvents & POLLIN) {

awoken();

}else{

LOGW("Ignoring unexpected poll events 0x%x on wake read pipe.", pollEvents);

}

}else{

intevents = 0;

if(pollEvents & POLLIN) events |= ALOOPER_EVENT_INPUT;

if(pollEvents & POLLOUT) events |= ALOOPER_EVENT_OUTPUT;

if(pollEvents & POLLERR) events |= ALOOPER_EVENT_ERROR;

if(pollEvents & POLLHUP) events |= ALOOPER_EVENT_HANGUP;

if(pollEvents & POLLNVAL) events |= ALOOPER_EVENT_INVALID;

pushResponse(events, mRequests.itemAt(i));

}

if(--eventCount == 0) {

break;

}

}

}

Done:

// Set mPolling to false and wake up the wakeAndLock() waiters.

mLock.lock();

mPolling =false;

if(mWaiters != 0) {

mAwake.broadcast();

}

mLock.unlock();

#endif

#ifdef LOOPER_STATISTICS

nsecs_t pollEndTime = systemTime(SYSTEM_TIME_MONOTONIC);

mSampledPolls += 1;

if(timeoutMillis == 0) {

mSampledZeroPollCount += 1;

mSampledZeroPollLatencySum += pollEndTime - pollStartTime;

}elseif(timeoutMillis > 0 && result == ALOOPER_POLL_TIMEOUT) {

mSampledTimeoutPollCount += 1;

mSampledTimeoutPollLatencySum += pollEndTime - pollStartTime

- milliseconds_to_nanoseconds(timeoutMillis);

}

if(mSampledPolls == SAMPLED_POLLS_TO_AGGREGATE) {

LOGD("%p ~ poll latency statistics: %0.3fms zero timeout, %0.3fms non-zero timeout",this,

0.000001f *float(mSampledZeroPollLatencySum) / mSampledZeroPollCount,

0.000001f *float(mSampledTimeoutPollLatencySum) / mSampledTimeoutPollCount);

mSampledPolls = 0;

mSampledZeroPollCount = 0;

mSampledZeroPollLatencySum = 0;

mSampledTimeoutPollCount = 0;

mSampledTimeoutPollLatencySum = 0;

}

#endif

for(size_ti = 0; i < mResponses.size(); i++) {

constResponse& response = mResponses.itemAt(i);

if(response.request.callback) {

#if DEBUG_POLL_AND_WAKE || DEBUG_CALLBACKS

LOGD("%p ~ pollOnce - invoking callback: fd=%d, events=0x%x, data=%p",this,

response.request.fd, response.events, response.request.data);

#endif

intcallbackResult = response.request.callback(

response.request.fd, response.events, response.request.data);

if(callbackResult == 0) {

removeFd(response.request.fd);

}

result = ALOOPER_POLL_CALLBACK;

}

}

returnresult;

}

6.awoken方法的实现:

[cpp]view plaincopy

voidLooper::awoken() {

#if DEBUG_POLL_AND_WAKE

LOGD("%p ~ awoken",this);

#endif

#ifdef LOOPER_STATISTICS

if(mPendingWakeCount == 0) {

LOGD("%p ~ awoken: spurious!",this);

}else{

mSampledWakeCycles += 1;

mSampledWakeCountSum += mPendingWakeCount;

mSampledWakeLatencySum += systemTime(SYSTEM_TIME_MONOTONIC) - mPendingWakeTime;

mPendingWakeCount = 0;

mPendingWakeTime = -1;

if(mSampledWakeCycles == SAMPLED_WAKE_CYCLES_TO_AGGREGATE) {

LOGD("%p ~ wake statistics: %0.3fms wake latency, %0.3f wakes per cycle",this,

0.000001f *float(mSampledWakeLatencySum) / mSampledWakeCycles,

float(mSampledWakeCountSum) / mSampledWakeCycles);

mSampledWakeCycles = 0;

mSampledWakeCountSum = 0;

mSampledWakeLatencySum = 0;

}

}

#endif

charbuffer[16];

ssize_t nRead;

do{

nRead = read(mWakeReadPipeFd, buffer,sizeof(buffer));//将管道中数据读出来

}while((nRead == -1 && errno == EINTR) || nRead ==sizeof(buffer));

}

线程消息发送过程

1.通过调用sendMessageXXX方法将消息发送到一个消息队列中:

[java]view plaincopy

publicbooleansendMessageAtTime(Message msg,longuptimeMillis)

{

booleansent =false;

MessageQueue queue = mQueue;

if(queue !=null) {

msg.target =this;

sent = queue.enqueueMessage(msg, uptimeMillis);//将消息发送到消息队列中

}

else{

RuntimeException e =newRuntimeException(

this+" sendMessageAtTime() called with no mQueue");

Log.w("Looper", e.getMessage(), e);

}

returnsent;

}

2.调用enqueueMessage方法将消息插入到消息队列(队头或队中):

[java]view plaincopy

booleanenqueueMessage(Message msg,longwhen) {

if(msg.target ==null) {

thrownewIllegalArgumentException("Message must have a target.");

}

if(msg.isInUse()) {

thrownewIllegalStateException(msg +" This message is already in use.");

}

synchronized(this) {

if(mQuitting) {

IllegalStateException e =newIllegalStateException(

msg.target +" sending message to a Handler on a dead thread");

Log.w(TAG, e.getMessage(), e);

msg.recycle();

returnfalse;

}

msg.markInUse();

msg.when = when;

Message p = mMessages;

booleanneedWake;

if(p ==null|| when ==0|| when < p.when) {

// New head, wake up the event queue if blocked.

msg.next = p;

mMessages = msg;

needWake = mBlocked;//记录了当前线程是否处于睡眠等待状态

}else{

// Inserted within the middle of the queue.  Usually we don't have to wake

// up the event queue unless there is a barrier at the head of the queue

// and the message is the earliest asynchronous message in the queue.

needWake = mBlocked && p.target ==null&& msg.isAsynchronous();

Message prev;

for(;;) {

prev = p;

p = p.next;

if(p ==null|| when < p.when) {

break;

}

if(needWake && p.isAsynchronous()) {

needWake =false;

}

}

msg.next = p;// invariant: p == prev.next,将消息插入到消息队列中

prev.next = msg;

}

// We can assume mPtr != 0 because mQuitting is false.

if(needWake) {

nativeWake(mPtr);//如果线程正在睡眠,那么将其唤醒

}

}

returntrue;

3.如果线程处于睡眠状态调用NativeMessageQueue的nativeWake方法,唤醒他:

[cpp]view plaincopy

staticvoidandroid_os_MessageQueue_nativeWake(JNIEnv* env, jclass clazz, jlong ptr) {

NativeMessageQueue *nativeMessageQueue =reinterpret_cast(ptr);

//通过ptr找到NativeMessageQueue,并调用它的wake方法唤醒目标线程,让它处理消息

nativeMessageQueue->wake();

}

4.nativeWake方法中调用NativeMessageQueue的wake方法:

[cpp]view plaincopy

voidNativeMessageQueue::wake() {

mLooper->wake();//调用Looper(Native)的wake方法

}

5.wake方法中调用Looper(Native)的wake方法,向管道写入字符:

[cpp]view plaincopy

voidLooper::wake() {

#if DEBUG_POLL_AND_WAKE

LOGD("%p ~ wake",this);

#endif

#ifdef LOOPER_STATISTICS

// FIXME: Possible race with awoken() but this code is for testing only and is rarely enabled.

if(mPendingWakeCount++ == 0) {

mPendingWakeTime = systemTime(SYSTEM_TIME_MONOTONIC);

}

#endif

ssize_t nWrite;

do{

nWrite = write(mWakeWritePipeFd,"W", 1);//向管道中写入字符,唤醒线程。

}while(nWrite == -1 && errno == EINTR);

if(nWrite != 1) {

if(errno != EAGAIN) {

LOGW("Could not write wake signal, errno=%d", errno);

}

}

}

线程消息处理过程

1.通过Looper类的loop方法获取消息:

[java]view plaincopy

publicstaticvoidloop() {

finalLooper me = myLooper();//获取当前线程looper

if(me ==null) {

thrownewRuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");

}

finalMessageQueue queue = me.mQueue;//获取当前线程MessageQueue

// Make sure the identity of this thread is that of the local process,

// and keep track of what that identity token actually is.

Binder.clearCallingIdentity();

finallongident = Binder.clearCallingIdentity();

for(;;) {//不断检查是否有新消息需要处理

Message msg = queue.next();// might block

if(msg ==null) {

// No message indicates that the message queue is quitting.

return;

}

// This must be in a local variable, in case a UI event sets the logger

Printer logging = me.mLogging;

if(logging !=null) {

logging.println(">>>>> Dispatching to "+ msg.target +" "+

msg.callback +": "+ msg.what);

}

//msg.target指向一个Handler对象,调用Handler的dispatchMessage方法分发消息

msg.target.dispatchMessage(msg);

if(logging !=null) {

logging.println("<<<<< Finished to "+ msg.target +" "+ msg.callback);

}

// Make sure that during the course of dispatching the

// identity of the thread wasn't corrupted.

finallongnewIdent = Binder.clearCallingIdentity();

if(ident != newIdent) {

Log.wtf(TAG,"Thread identity changed from 0x"

+ Long.toHexString(ident) +" to 0x"

+ Long.toHexString(newIdent)

+" while dispatching to "

+ msg.target.getClass().getName() +" "

+ msg.callback +" what="+ msg.what);

}

msg.recycleUnchecked();

}

}

2.通过Handler类的dispatchMessage分发消息:

[java]view plaincopy

//分发Looper传来的消息

publicvoiddispatchMessage(Message msg) {

if(msg.callback !=null) {

handleCallback(msg);

}else{

if(mCallback !=null) {

if(mCallback.handleMessage(msg)) {

return;

}

}

handleMessage(msg);

}

}

3.通过Handler的成员函数handleMessage处理消息:

[java]view plaincopy

//通过重写handleMessage方法处理消息

publicvoidhandleMessage(Message msg) {    }


===================================================

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,612评论 5 471
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,345评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,625评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,022评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,974评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,227评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,688评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,358评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,490评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,402评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,446评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,126评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,721评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,802评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,013评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,504评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,080评论 2 341

推荐阅读更多精彩内容