Android 消息处理源码分析(2)
来源:互联网 发布:电视播放软件破解版 编辑:程序博客网 时间:2024/04/30 20:59
Android 消息处理源码分析(1)点击打开链接
继续接着分析剩下的类文件
Looper.javapublic final class Looper { final MessageQueue mQueue; //消息队列 final Thread mThread; //Looper联系的线程 public static void prepare() { prepare(true); } private static void prepare(boolean quitAllowed) { //先会检查是否有Looper,若有则抛出异常,没有的话则创建一个Looper实例保存起来 if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); } public static void prepareMainLooper() { prepare(false); synchronized (Looper.class) { if (sMainLooper != null) { throw new IllegalStateException("The main Looper has already been prepared."); } sMainLooper = myLooper(); } } //在这个线程中运行消息队列,调用quit()停止 public static void loop() {...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(); // 从消息队列中取出一条消息 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); //交给msg的handler分发消息处理... } //取出当前线程的Looper,返回空则表示当前线程没有Looper public static Looper myLooper() { return sThreadLocal.get(); }}
Handler.javapublic class Handler { //定义Callback接口 public interface Callback { public boolean handleMessage(Message msg); } //子类要实现的消息处理方法 public void handleMessage(Message msg) { } * Handle system messages here. */ public void dispatchMessage(Message msg) { //分发信息 if (msg.callback != null) { //若指定了msg.callback,则由它处理 handleCallback(msg); } else { if (mCallback != null) { //若指定了Handler.mCallback,则由它处理 if (mCallback.handleMessage(msg)) { //调用mCallback接口的实现方法 return; } } handleMessage(msg); 最后才调用Handler自身重载的handleMessage方法 } } 分发消息函数中,消息先会检查自身有没有处理自身的回调Runnable,若有则由它处理,若没有则会检查该handler有无自身的回调处理,若有则调用,若没有则调用自身重载的handleMessage方法 //Handler的生成总是和它当前所处线程有关的,如果当前线程中没有一个Looper,则会报错,UI线程中默认有产生Looper的函数 public Handler() { this(null, false); } //使用指定的Looper(可以处理那个Looper线程中的消息),不用默认的从当前线程中取出Looper public Handler(Looper looper) { this(looper, null, false); } ...}
0 0
- Android 消息处理源码分析(2)
- Android 消息处理源码分析(1)
- Android 消息处理机制2(从源码分析)
- Android应用程序消息处理机制(Looper、Handler)源码分析
- Android异步消息处理机制(二):源码分析
- Android消息处理机制源码分析(一):整体过程
- Android消息处理机制源码分析(二):本地实现
- android的消息处理机制(图+源码分析)
- Android消息处理-源码分析篇
- Android消息处理机制(源码分析为主)
- Android的消息处理机制源码分析
- TOMCAT源码分析(消息处理)(转)
- 【Android消息处理机制】android的消息处理机制(图+源码分析)——Looper,Handler,Message
- skynet源码分析(5)--消息机制之消息处理
- (消息处理机制)Android的消息处理机制(图+源码分析)——Looper,Handler,Message
- TOMCAT源码分析(消息处理)
- TOMCAT源码分析(消息处理)
- TOMCAT源码分析(消息处理)
- eC 控制台编程
- 生成验证码
- jsp界面中不识别EL表达式
- c语言排序后删除数组中重复的数字再排序输出
- WPF DynammicResource 泄露问题
- Android 消息处理源码分析(2)
- CentOS 6.6下安装OpenOffice4.0
- Maven工程目录
- SAP标准价格修改
- Android Volley入门到精通:初识Volley的基本用法(示例,出错代码)
- 什么是序列化?在什么情况下将类序列化?
- GRE写作必备句型
- android viewpager+Imagerview的可滑动主界面导航栏
- RedHat Linux 安装oracle11g