Android关于looper的几个方法的个人理解

    最近在看android的looper这个类与android的消息队列的处理有一定的紧密关系,今天写一些关于这个类的几个常用的方法,主要是起到自己巩固学习的作用,方便以后自己的查看。

1、prepare()  源代码如下

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是否为空,不为空时报出异常否侧讲一个Looper实例放入其中。这也可以从中看出不可以多次的去调用这个方法,第一次调用后sThreadLocal中就已经是有值的了,如果再次调用就会报错。
 
 
2、getMainLooper() 源码如下
public static Looper getMainLooper() {
    synchronized (Looper.class) {
        return sMainLooper;
    }
}
该方法主要是返回当前进程中的主线程的消息队列。
 
 
3、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.recycleUnchecked();
    }
}
该方法先是判断myLooper的方法返回的是否为空,为空就报错证明你没有向sThreadLocal中放入实例及没有调用到prepare()方法或者调用它的方法。而后获取其消息队列,进行无限循环。
 
 
4、Looper() 源码如下
private Looper(boolean quitAllowed) {
    mQueue = new MessageQueue(quitAllowed);
    mThread = Thread.currentThread();
}
Looper类的构造方法,创建了一个消息队列。
 
 
5、myLooper 源码如下
public static @Nullable Looper myLooper() {
    return sThreadLocal.get();
}
返回存储在sThreadLocal中的looper实例,可以看出在调用该方法前,必须调用prepare()方法或者调用它的方法,不然sThreadLocal中是空的会报错。


6、quit() 源码如下
public void quit() {
    mQueue.quit(false);
}
   停止looper。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值