zoukankan      html  css  js  c++  java
  • 什么是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     }    
  • 相关阅读:
    Android 使用WebView显示网页
    Android 使用ProgressBar实现进度条
    Android 使用Spinner实现下拉列表
    Android 使用GridView以表格的形式显示多张图片
    Android 使用DatePicker以及TimePicker显示当前日期和时间
    Android 使用ListView显示信息列表
    IIS配置步骤,绝对有用,百度上的不全面,是百度的补充
    冒烟测试
    广度优先和深度优先
    排序算法二(时间复杂度为O(N*logN))
  • 原文地址:https://www.cnblogs.com/iMirror/p/3963964.html
Copyright © 2011-2022 走看看