安卓android.os.Looper.java分析
来源:互联网 发布:windows 7磁盘整理 编辑:程序博客网 时间:2024/04/30 22:22
looper对象是要是一个消息处理的循环,使用的场景是,附着在一个线程里面进行消息处理。常用的方法也是准备消息队列,进入消息处理循环,操作消息队列。
准备方法:prepare
使用looper的第一步,必须调用prepare方法,创建出looper对象
有几种prepare方法,looper创建出来后,通过sThreadLocal保存起来。
一种可以指定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)); }
一种允许推出的looper
public static void prepare() { prepare(true); }
一种不允许推出的looper
public static void prepareMainLooper() { prepare(false); synchronized (Looper.class) { if (sMainLooper != null) { throw new IllegalStateException("The main Looper has already been prepared."); } sMainLooper = myLooper(); } }
构造方法
构造方法中,初始化一个消息队列。这个是loop主要打交道的对象。
private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread(); }
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(); } }
*其他方法*
大多数looper类的方法都是给予messagequeue的方法来来封装,无需再重复分析。
0 0
- 安卓android.os.Looper.java分析
- 轮询器 android.os.Looper
- Android OS--Looper、Thread、Handler
- 【安卓笔记】Handler,Looper,MessageQueue,Message源码分析
- android Looper源码分析
- Android Looper 分析1
- 安卓笔记android.os.NetworkOnMainThreadException异常
- 安卓倒计时(android.os.Handler.Handler()
- 安卓android.os.MessageQueue 简析
- Android深入分析MessageQueue和Looper(FrameWork JAVA部分)
- 【Android】java.lang.AssertionError use looper thread, must call Looper.prepare() first!异常分析
- 安卓开发-Looper学习
- Android Looper和Handler分析
- Android Looper和Handler分析
- Android Looper和Handler分析
- Android Looper和Handler分析
- Android Looper和Handler分析
- Android Looper和Handler分析
- MotionEvent.getX /Y 与 getRamX/Y 区别。
- 剑指offer-合并排序链表
- iOS UITableView(十) tableViewcell内容重复的解决办法
- nob211,com网上我拿佰家樂玩龍虎,皇家博-彩平台
- 转:4-20mADC(1-5VDC)
- 安卓android.os.Looper.java分析
- python 打印杨辉三角
- BZOJ 2618 CQOI 2006 凸多边形 半平面交
- 线性表-1
- linux下find的作用
- 解决宿主机不能访问虚拟机CentOS中的站点
- EasyUi之menu组件
- linux获取后台进程的控制台数据
- Xcode 在安装插件后若发现不能使用