文章标题

第一步:
Looper.prepare()
源码:
public static final void prepare() {
if (sThreadLocal.get() != null) {
throw new RuntimeException(“Only one Looper may be created per thread”);
}
sThreadLocal.set(new Looper(true));
}

创建一个Looper对象并且设置给所在的线程,并且prepare只能被调用一次,一个线程中只能有一个Looper

private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mRun = true;
mThread = Thread.currentThread();
}

在构造方法中创建一个消息队列,并且把当前线程赋值给mThread 让Looper MessageQueue 和当前Thread关联起来

第二步:Looper.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(); // 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.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.  
        final long newIdent = 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.recycle();  
    }  

这个方法里做了以下几件事:
1. final Looper me = myLooper(); 取出当前线程的looper
2. 取出当前线程的MessageQueue
3. 进入死循环中Message msg = queue.next(); 取出消息队列中的消息
4. msg.target.dispatchMessage(msg); 把消息交给msg的target也就是Handler的dispatchMessage()方法去处理

总结:Looper主要作用:
1、 与当前线程绑定,保证一个线程只会有一个Looper实例,同时一个Looper实例也只有一个MessageQueue。
2、 loop()方法,不断从MessageQueue中去取消息,交给消息的target属性的dispatchMessage去处理。
好了,我们的异步消息处理线程已经有了消息队列(MessageQueue),也有了在无限循环体中取出消息的哥们,现在缺的就是发送消息的对象了,于是乎:Handler登场了。

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

接下来介绍Handler

public Handler() {
this(null, false);
}
public Handler(Callback callback, boolean async) {
if (FIND_POTENTIAL_LEAKS) {
final Class

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值