22、消息处理

Android应用程序是通过消息来驱动的,当Android主线程启动时就会在内部创建一个消息队列。然后进入一个无限循环中,轮询是否有新的消息需要处理。如果有新消息就处理新消息。如果没有消息,就进入阻塞状态,直到消息循环被唤醒。

那么在Android系统中,消息处理机制是怎么实现的呢?在程序开发时,我们经常会使用Handler处理Message(消息)。所以可以知道Handler是个消息处理者,Message是消息主体。除此之外还有消息队列和消息轮询两个角色。它们分别是MessageQueue和Looper,MessageQueue就是消息队列,Looper负责轮询消息。

简介:
我们已经知道Android的消息机制处理主要由Handler、Message、MessageQueue、Looper四个类的实现来完成。那么它们之间的关系是怎样的?
其中,Message是消息主体,它负责存储消息的各种信息,包括发送消息的Handler对象、消息信息、消息标识等。MessageQueue就是消息队列,在其内部以队列的形式维护一组Message(消息)。Handler负责发送和处理消息。Looper负责轮询消息队列。

Android的消息机制主要是指Handler的运行机制,它的作用是提供"在某个具体的线程中执行任务”的功能,相当于一种回调吧。
一、为什么有消息机制?主要是为了解决子线程中无法访问UI。
为什么android的子线程无法访问UI?因为UI控件是线程不安全的,之所以不给它加入锁机制,是因为会降低UI的访问效率。所以设计为单线程模型处理UI,再引入Handler。
通过ThreadLocal来管理不同线程的Looper。
二、MessageQueue主要有两个操作:
enqueueMessage:往MessageQueue中插入一条消息
next:从MessageQueue中取出一条消息,并同时移除此条消息
三、Looper:
在Looper的构造方法中会初始化MessageQueue
为当前线程初始化Looper:Looper.prepare()
开始消息循环:Looper.loop()
退出消息循环:quit()和quitSafely(),前者直接退出,后者将消息队列中的消息处理完退出。所以子线程处理完消息后建议退出looper,不然线程结束不了。
loop()方法:会不断的从MessageQueue中取出消息,然后调用msg.target.dispatchMessage(),msg.target就是handler
特殊的:prepareMainLooper()实际也是调用prepare(),getMainLooper()拿取主线程Looper
四、创建线程消息队列
在Android应用程序中,消息处理程序运行前首先要创建消息队列(也就是MessageQueue)。在主线程中,通过调用Looper类的静态成员函数prepareMainLooper()来创建消息队列。在其他子线程中,通过调用静态成员函数prepare()来创建。

prepareMainLooper()与prepare()的实现:

/**
 * Initialize the current thread as a looper, marking it as an
 * application's main looper. The main looper for your application
 * is created by the Android environment, so you should never need
 * to call this function yourself.  See also: {@link #prepare()}
 * 用来初始化主线程中的Looper,有Android环境调用,不应该有用户调用.
 */
public static void prepareMainLooper() {
    prepare(false);
    synchronized (Looper.class) {
        if (sMainLooper != null) {
            throw new IllegalStateException("The main Looper has already been prepared.");
        }
        sMainLooper = myLooper();
    }
}

public static @Nullable Looper myLooper() {
    return sThreadLocal.get();
}

 /** Initialize the current thread as a looper.
  * This gives you a chance to create handlers that then reference
  * this looper, before actually starting the loop. Be sure to call
  * {@link #loop()} after calling this method, and end it by calling
  * {@link #quit()}.
  * 交给用户自己调用,通过loop()方法开启消息循环.同时当不需要处理消息时,需要手动调用quit()方法退出循环.
  */
public static void prepare() {
    prepare(true);
}

private static void prepare(boolean quitAllowed) {
    if (sThreadLocal.get() != null) {
        throw new RuntimeException("Only one Looper may be created per thread");
    }
    sThreadLocal.set(new Looper(quitAllowed));
}

在这两个函数调用的过程中,sThreadLocal变量都有被使用。这个变量是ThreadLocal类型的,用来保存当前线程中的Looper对象。也就是说在Android应用程序中每创建一个消息队列,都有一个并且是唯一 一个与之对应的Looper对象。而且我们可以从源码中看到当对象不唯一时就会抛出异常。

private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed); //创建消息队列
mThread = Thread.currentThread();
}

消息循环过程
在消息队列建立完成之后,调用Looper对象的静态成员方法loop()就开始了消息循环。

/**
 * Run the message queue in this thread. Be sure to call
 * {@link #quit()} to end the loop.
 */
public static void loop() {
    final Looper me = myLooper();
    if (me == null) {
        throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
    }
    final MessageQueue queue = me.mQueue;

    // 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();
    final long ident = Binder.clearCallingIdentity();

    for (;;) { //开始消息循环
        Message msg = queue.next(); // 在接收消息时有可能阻塞
        if (msg == null) {
            //message为null时,退出消息循环
            return;
        }

        // This must be in a local variable, in case a UI event sets the logger
        final Printer logging = me.mLogging;
        if (logging != null) {...}

        final long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;

        final long traceTag = me.mTraceTag;
        if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {...}
        final long start = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
        final long end;
        try {
            msg.target.dispatchMessage(msg);  //处理消息
            end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
        } finally {
            if (traceTag != 0) {
                Trace.traceEnd(traceTag);
            }
        }
        if (slowDispatchThresholdMs > 0) {
            final long time = end - start;
            if (time > slowDispatchThresholdMs) {...}
        }

        if (logging != null) {...}

        // Make sure that during the course of dispatching the
        // identity of the thread wasn't corrupted.
        final long newIdent = Binder.clearCallingIdentity();
        if (ident != newIdent) {...}
        msg.recycleUnchecked();
    }
}

上面的源码就是消息循环的过程,只用调用了loop()方法消息循环才开始起作用。当循环开始时:
获取当前线程的Looper对象,如果为null,抛出异常;
获取消息队列,开始进入消息循环;
从消息队列中获取消息(调用MessageQueue的next()方法),如果为null,结束循环;否则,继续执行;
处理消息,回收消息资源( msg.recycleUnchecked())。
在消息循环过程中,通过MessageQueue的next()方法提供消息,在没有信息时进入睡眠状态,同时处理其他接口。这个过程至关重要,通过next()方法也决定了消息循环是否退出。

Message next() {
final long ptr = mPtr; //与native方法相关,当mPtr为0时返回null,退出消息循环
if (ptr == 0) {
return null;
}

    int pendingIdleHandlerCount = -1; // -1 only during first iteration
    int nextPollTimeoutMillis = 0;  //0不进入睡眠,-1进入书面
    for (;;) {
        if (nextPollTimeoutMillis != 0) {
            //处理当前线程中待处理的Binder进程间通信请求
            Binder.flushPendingCommands();  
        }
        //native方法,nextPollTimeoutMillis为-1时进入睡眠状态
        nativePollOnce(ptr, nextPollTimeoutMillis); 
        synchronized (this) {
            final long now = 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();
                    return msg; //返回消息
                }
            } else {
                // No more messages.
                nextPollTimeoutMillis = -1; //更新到睡眠状态
            }

            // Process the quit message now that all pending messages have been handled.
            //消息循环退出
            if (mQuitting) {
                dispose();
                return null;
            }

            // 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 = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
            }
            mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
        }

        // Run the idle handlers.
        // We only ever reach this code block during the first iteration.
        //非睡眠状态下处理IdleHandler接口
        for (int i = 0; i < pendingIdleHandlerCount; i++) {
            final IdleHandler idler = mPendingIdleHandlers[i];
            mPendingIdleHandlers[i] = null; // release the reference to the handler

            boolean keep = 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;
    }
}

消息循环退出过程
从上面可以看到loop()方法是一个死循环,只有当MessageQueue的next()方法返回null时才会结束循环。那么MessageQueue的next()方法何时为null呢?

在Looper类中我们看到了两个结束的方法quit()和quitSalely()。两者的区别就是quit()方法直接结束循环,处理掉MessageQueue中所有的消息,而quitSafely()在处理完消息队列中的剩余的非延时消息(延时消息(延迟发送的消息)直接回收)时才退出。这两个方法都调用了MessageQueue的quit()方法。

void quit(boolean safe) {
if (!mQuitAllowed) {
throw new IllegalStateException(“Main thread not allowed to quit.”);
}

    synchronized (this) {
        if (mQuitting) {
            return;
        }
        mQuitting = true; //设置退出状态

		//处理消息队列中的消息
        if (safe) {
            removeAllFutureMessagesLocked();  //处理掉所有延时消息
        } else {
            removeAllMessagesLocked(); //处理掉所有消息
        }

        // We can assume mPtr != 0 because mQuitting was previously false.
        nativeWake(mPtr);  // 唤醒消息循环
    }
}

消息发送过程
在Android应用程序中,通过Handler类向线程的消息队列发送消息。在每个Handler对象中持有一个Looper对象和MessageQueue对象。
public Handler(Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}

    }
    mLooper = Looper.myLooper(); //获取Looper对象
    if (mLooper == null) {...}
    mQueue = mLooper.mQueue;  //获取消息队列
    mCallback = callback;
    mAsynchronous = async;
}

在Handler类中,我们可以看到多种sendMessage方法,而它们最终都调用了同一个方法sendMessageAtTime()方法。
public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
MessageQueue queue = mQueue;
if (queue == null) {
RuntimeException e = new RuntimeException(
this + " sendMessageAtTime() called with no mQueue");
Log.w(“Looper”, e.getMessage(), e);
return false;
}
//向消息队列中添加消息
return enqueueMessage(queue, msg, uptimeMillis);
}

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
    msg.target = this;
    if (mAsynchronous) {
        msg.setAsynchronous(true);
    }
    return queue.enqueueMessage(msg, uptimeMillis);
}

可以看出这两个方法十分容易理解,就是通过MessageQueue对象调用enqueueMessage()方法向消息队列中添加消息。
boolean enqueueMessage(Message msg, long when) {
// Handler为null
if (msg.target == null) {
throw new IllegalArgumentException(“Message must have a target.”);
}
//消息已经被消费
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}

    synchronized (this) {
        //是否退出
        if (mQuitting) {
            IllegalStateException e = new IllegalStateException(
                    msg.target + " sending message to a Handler on a dead thread");
            Log.w(TAG, e.getMessage(), e);
            msg.recycle();
            return false;
        }

        msg.markInUse();
        msg.when = when;
        Message p = mMessages;
        boolean needWake;
        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);  //唤醒消息循环
        }
    }
    return true;
}

从源码可以看出,一个消息插入到消息队列中需要以下步骤:
1、消息持有的Handler对象为null,抛出异常;当消息已经被消费,抛出异常;
2、当消息队列没有消息时,直接插入;
3、当消息队列存在消息时,通过比较消息的执行时间,将消息插入到相应的位置;
4、判断是否需要唤醒消息循环。

消息处理过程
在消息循环过程中,如果有新的消息加入,就开始处理消息。从上面的分析中,我们可以看到在消息循环中,目标消息会调用其Handler对象的dispatchMessage()方法,这个就是处理消息的方法。
/**
* Handle system messages here.
*/
public void dispatchMessage(Message msg) {
// 消息Callback接口不为null,执行Callback接口
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
//Handler Callback接口不为null,执行接口方法
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg); //处理消息
}
}
从源码可以看出,Handler处理消息分为3中情况。
1、当Message中的callback不为null时,执行Message中的callback中的方法。这个callback时一个Runnable接口。
2、当Handler中的Callback接口不为null时,执行Callback接口中的方法。
3、直接执行Handler中的handleMessage()方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值