消息机制概述

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) {    }


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

更多相关文章

  1. android开发中实现个性化ListView的一些概念和思路
  2. Android(安卓)自定义对话框 控件
  3. Android(安卓)Butterknife 框架源码解析(2)——谈谈Java的注解
  4. android 消息机制浅析
  5. Android进程通信(IPC)之AIDL对象传递
  6. Android(安卓)Framework中的Application Framework层介绍
  7. 解析Android中的线程
  8. android 浏览器 打开本地html文件的方法
  9. Android(安卓)UI之ImageView

随机推荐

  1. Android手机应用开发(十) | HttpURLConnect
  2. Android横竖屏切换时Activity的生命周期
  3. 手机锁屏后再解锁保存activity状态
  4. Android输入法框架中按键消息的处理流程
  5. Android(安卓)设置EditText显示或隐藏密
  6. Android深入浅出系列课程---Lesson12 AFF
  7. 修正 XE5 Android 键盘三个问题
  8. Android自动测试之Monkey工具
  9. Android中在XML文件里@+id/和@id/的区别
  10. Android:Umeng(友盟)消息推送(二)