Android消息机制

Android的消息机制

Looper MessageQueue Handler

用法演示

闲话不多说,直接上代码
A线程向B线程发送消息

//A线程中

//在A线程中创建一个looper对象
Looper.prepare();                 
//创建handler对象,用于其他线程想这个线程发送消息
Handler aHandler= new Handler(){  
    handleMessage(Message msg)
    {

    }
};
//无限循环的轮训消息队列
Looper.loop();


//B线程中
Message msg = new Message();
msg.what= ;
msg.obj = ;
msg.....     
//通过某种方法获取A线程中的aHandler引用
//通过这个引用 发送一个消息就可以在A线程中获取这个消息
aHandler.sendMessage(msg);
原理剖析

为什么这可以正常工作,Handler怎么和这个looper关联起来的,之前提到的MessageQueue去哪里了 ……
Talk is cheap,show me the code.

  • 首先看一下Looper.prepare()
  //Looper.java
  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));
    }

可以看到实际上调用的是prepare(boolean quitAllowed)这个函数
这个函数里面sThreadLocal,他是一个线程中存储一些不会被共享的数据的地方,我们就把它当作是一个线程的小仓库就好了

// sThreadLocal.get() will return null unless you've called prepare().
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();

如注释所示这个对象的get()方法会返回null,如果从未调用Lopper.prepare()方法,那么prepare(boolean )方法中做了什么呢:首先判断sThreadLocal.get()是否返回null,也就是是否是第一次调用,如果不是那么就抛出异常
throw new RuntimeException("Only one Looper may be created per thread");
如果是第一次调用,那么就new Looper()并且把他存储到sThreadLocal中,(ps:所以以后调用sThreadLocal.get()就不会返回null了)
new Looper()中做了什么呢

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

可以看到创建了一个消息队列MessageQueue

  • 那么Handler怎么和这个Looper对象关联起来的呢?
//Handler.java
 public Handler() {
        this(null, false);
    }
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();
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
    }
  • mLooper = Looper.myLooper();我么到Looper中查看

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

    很明显是把我们调用
    Looper.prepare()时存入到sThreadLocal中的Looper`对象取出来.然后mLooper这个引用就引用到了sThreadLocal中这个Looper对象了

  • 异常判断一下就是, mQueue = mLooper.mQueue;
    这个也很好理解,就是获取之前创建的消息队列
    这样Handler对象就拥有了消息队列的引用,调用Handler.sendMessage就可以把Message放到队列中,然后Looper.loop()不停的轮询就可以把消息分发给目标了
    结构图

Looper.loop()的作用就是死循环去轮循消息队列,如果有消息纠纷发给目标去执行
轮询结构

//todo 未来想到再更新

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值