• 什么是Handler(三)


    1.Looper当中loop()方法的作用

    2.什么是Message对象的Target

    3.处理一个Message的方法

     1 public static void loop() {  //静态方法
     2         final Looper me = myLooper(); //myLooper是根据已知的线程对象取出对应的Looper对象
     3         if (me == null) { //判断取出的Looper对象是否为空
     4             throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
     5         }
     6         final MessageQueue queue = me.mQueue; //取出Looper的消息队列
     7 
     8         // Make sure the identity of this thread is that of the local process,
     9         // and keep track of what that identity token actually is.
    10         Binder.clearCallingIdentity();
    11         final long ident = Binder.clearCallingIdentity();
    12 
    13         for (;;) {  //此循环是不停的从消息队列中取出消息,如果没有值就会阻塞
    14             Message msg = queue.next(); // might block 
    15             if (msg == null) {
    16                 // No message indicates that the message queue is quitting.
    17                 return;
    18             }
    19 
    20             // This must be in a local variable, in case a UI event sets the logger
    21             Printer logging = me.mLogging;
    22             if (logging != null) {
    23                 logging.println(">>>>> Dispatching to " + msg.target + " " +
    24                         msg.callback + ": " + msg.what);
    25             }
    26 
    27             msg.target.dispatchMessage(msg);//查看Message.java源码, 
    28             //一个Handler对应一个looper对象,一个looper对应一个MessageQueue对象,使用Handler生成Message, 所生成的Message对象的target属性, 就是该Handler对象
    29            //查看Handler的dispatchMessage方法, 得知用handlerMessage处理消息
    30 
    31             if (logging != null) {
    32                 logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
    33             }
    34 
    35             // Make sure that during the course of dispatching the
    36             // identity of the thread wasn't corrupted.
    37             final long newIdent = Binder.clearCallingIdentity();
    38             if (ident != newIdent) {
    39                 Log.wtf(TAG, "Thread identity changed from 0x"
    40                         + Long.toHexString(ident) + " to 0x"
    41                         + Long.toHexString(newIdent) + " while dispatching to "
    42                         + msg.target.getClass().getName() + " "
    43                         + msg.callback + " what=" + msg.what);
    44             }
    45 
    46             msg.recycle();
    47         }
    48     }    
  • 相关阅读:
    苹果开发者账号证书、授权配置文件设置流程
    JQuery 总结(1) 选择器的使用
    PC和移动端自动识别
    前端获取ip的接口
    AJAX 跨域解决办法
    linux
    复制剪切板实现
    css animation 制作打开动画效果
    弹出层播放视频
    盛世龙图项目总结
  • 原文地址:https://www.cnblogs.com/iMirror/p/3963964.html
Copyright © 2020-2023  润新知