头条 科技 产经 家电 智能 手机 芯片 数码 电商 WAP版
中国IT产经新闻网-移动互联网与智能搜索领域是未来IT产业发展的趋势!
中国IT产经新闻/工控机电/正文
在android线程中进行消息处理looper.prepare方法详解
来源:
2023-08-18
编辑:晓露

      Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理。handler其实可以看做是一个工具类,用来向消息队列中插入消息的。
      Looper.prepare()方法:


public static void prepare() {
          prepare(true);
      }
prepare又调用它的重载方法,并将true作为默认形参传入,如果线程未绑定Looper对象则调用sThreadLocal.set(new Looper(quitAllowed))方法。如果已经绑定则抛出一个异常信息“每一个线程只能绑定一个Looper对象”。
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.set()方法,以quitAllowed(也就是true)作为形参并调用Looper的有参构造然后传入一个Looper实例对象。
    sThreadLocal.set(new Looper(quitAllowed));
}
Looper的有参构造内以true作为形参传入并生成一个信息队列对象引用保存至mQueue成员变量,然后将当前线程引用保存至mThread成员变量。
private Looper(boolean quitAllowed) {
        mQueue = new MessageQueue(quitAllowed);
        mThread = Thread.currentThread();
    }
在MessageQueue有参构造中将quitAllowed(true)保存至mQuitAllowed成员变量。
MessageQueue(boolean quitAllowed) {
        mQuitAllowed = quitAllowed;
        mPtr = nativeInit();
    }

飞利浦夸克CT全球首发亮相2024春季CMEF

编辑:
2024-04-13
0
8
点评(1)

Liese莉婕×闪耀暖暖 共启秀发循色之旅

编辑:
2024-04-13
0
7
点评(1)
产业点评更多
厂商动态更多
热门综合更多
CopyRight @ 2008-2023 中国IT产经新闻网 All Right Reserved 违者必究 湘ICP备2022017330号-2