`

android源码解析--Message

阅读更多

看下类定义:

 

[java] view plaincopy
  1. Defines a message containing a description and arbitrary data object that can be sent to a Handler. This object contains two extra int fields and an extra object field that allow you to not do allocations in many cases.  
  2.   
  3. While the constructor of Message is public, the best way to get one of these is to call Message.obtain() or one of the Handler.obtainMessage() methods, which will pull them from a pool of recycled objects.  

 

 

定义一个包含任意类型的描述数据对象,此对象可以发送给Handler。对象包含两个额外的int字段和一个额外的对象字段,这样可以使得在很多情况下不用做分配工作。尽管Message的构造器是公开的,但是获取Message对象的最好方法是调用Message.obtain()或者Handler.obtainMessage(), 这样是从一个可回收对象池中获取Message对象。

 

[java] view plaincopy
  1. public final class Message implements Parcelable   


Message类是个final类,就是说不能被继承,同时Message类实现了Parcelable接口,我们知道android提供了一种新的类型:Parcel。本类被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以外,只有实现了Parcelable接口的类才能被放入Parcel中。

 

看一下全局变量:

 

[java] view plaincopy
  1. /** 
  2.      * User-defined message code so that the recipient can identify  
  3.      * what this message is about. Each {@link Handler} has its own name-space 
  4.      * for message codes, so you do not need to worry about yours conflicting 
  5.      * with other handlers. 
  6.      */  
  7.     public int what;  
  8.   
  9.     /** 
  10.      * arg1 and arg2 are lower-cost alternatives to using 
  11.      * {@link #setData(Bundle) setData()} if you only need to store a 
  12.      * few integer values. 
  13.      */  
  14.     public int arg1;   
  15.   
  16.     /** 
  17.      * arg1 and arg2 are lower-cost alternatives to using 
  18.      * {@link #setData(Bundle) setData()} if you only need to store a 
  19.      * few integer values. 
  20.      */  
  21.     public int arg2;  
  22.   
  23.     /** 
  24.      * An arbitrary object to send to the recipient.  When using 
  25.      * {@link Messenger} to send the message across processes this can only 
  26.      * be non-null if it contains a Parcelable of a framework class (not one 
  27.      * implemented by the application).   For other data transfer use 
  28.      * {@link #setData}. 
  29.      *  
  30.      * <p>Note that Parcelable objects here are not supported prior to 
  31.      * the {@link android.os.Build.VERSION_CODES#FROYO} release. 
  32.      */  
  33.     public Object obj;  
  34.   
  35.     /** 
  36.      * Optional Messenger where replies to this message can be sent.  The 
  37.      * semantics of exactly how this is used are up to the sender and 
  38.      * receiver. 
  39.      */  
  40.     public Messenger replyTo;  
  41.   
  42.     /** If set message is in use */  
  43.     /*package*/ static final int FLAG_IN_USE = 1;  
  44.   
  45.     /** Flags reserved for future use (All are reserved for now) */  
  46.     /*package*/ static final int FLAGS_RESERVED = ~FLAG_IN_USE;  
  47.   
  48.     /** Flags to clear in the copyFrom method */  
  49.     /*package*/ static final int FLAGS_TO_CLEAR_ON_COPY_FROM = FLAGS_RESERVED | FLAG_IN_USE;  
  50.   
  51.     /*package*/ int flags;  
  52.   
  53.     /*package*/ long when;  
  54.       
  55.     /*package*/ Bundle data;  
  56.       
  57.     /*package*/ Handler target;       
  58.       
  59.     /*package*/ Runnable callback;     
  60.       
  61.     // sometimes we store linked lists of these things  
  62.     /*package*/ Message next;  
  63.   
  64.     private static final Object sPoolSync = new Object();  
  65.     private static Message sPool;  
  66.     private static int sPoolSize = 0;  
  67.   
  68.     private static final int MAX_POOL_SIZE = 10;  

 

  1. what:用户定义消息代码以便收件人可以识别这是哪一个Message。每个Handler用它自己的名称空间为消息代码,所以您不需要担心你的Handler与其他handler冲突。
  2. arg1、arg2:如果只是想向message内放一些整数值,可以使用arg1和arg2来代替setData方法。
  3. obj:发送给接收器的任意对象。当使用Message对象在线程间传递消息时,如果它包含一个Parcelable的结构类(不是由应用程序实现的类),此字段必须为非空(non-null)。其他的数据传输则使用setData(Bundle)方法。注意Parcelable对象是从FROYO版本以后才开始支持的。
  4. replyTo:指明此message发送到何处的可选Messenger对象。具体的使用方法由发送者和接受者决定。
  5. FLAG_IN_USE:判断Message是否在使用( default 包内可见
  6. FLAGS_RESERVED:留个将来使用??
  7. FLAGS_TO_CLEAR_ON_COPY_FROM:明确在copyFrom方法
  8. 其他参数都比较简单,不详述

 

下面看obtain方法:

 

[java] view plaincopy
  1. /** 
  2.      * Return a new Message instance from the global pool. Allows us to 
  3.      * avoid allocating new objects in many cases. 
  4.      */  
  5.     public static Message obtain() {  
  6.         synchronized (sPoolSync) {  
  7.             if (sPool != null) {  
  8.                 Message m = sPool;  
  9.                 sPool = m.next;  
  10.                 m.next = null;  
  11.                 sPoolSize--;  
  12.                 return m;  
  13.             }  
  14.         }  
  15.         return new Message();  
  16.     }  


从全局池中返回一个新的Message实例。在大多数情况下这样可以避免分配新的对象。

 

在看它一系列的重载方法:

 

[java] view plaincopy
  1. /** 
  2.  * Same as {@link #obtain(Handler)}, but assigns a callback Runnable on 
  3.  * the Message that is returned. 
  4.  * @param h  Handler to assign to the returned Message object's <em>target</em> member. 
  5.  * @param callback Runnable that will execute when the message is handled. 
  6.  * @return A Message object from the global pool. 
  7.  */  
  8. public static Message obtain(Handler h, Runnable callback) {  
  9.     Message m = obtain();  
  10.     m.target = h;  
  11.     m.callback = callback;  
  12.   
  13.     return m;  
  14. }  
  15.   
  16. /** 
  17.  * Same as {@link #obtain()}, but sets the values for both <em>target</em> and 
  18.  * <em>what</em> members on the Message. 
  19.  * @param h  Value to assign to the <em>target</em> member. 
  20.  * @param what  Value to assign to the <em>what</em> member. 
  21.  * @return A Message object from the global pool. 
  22.  */  
  23. public static Message obtain(Handler h, int what) {  
  24.     Message m = obtain();  
  25.     m.target = h;  
  26.     m.what = what;  
  27.   
  28.     return m;  
  29. }  
  30.   
  31. /** 
  32.  * Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>, and <em>obj</em> 
  33.  * members. 
  34.  * @param h  The <em>target</em> value to set. 
  35.  * @param what  The <em>what</em> value to set. 
  36.  * @param obj  The <em>object</em> method to set. 
  37.  * @return  A Message object from the global pool. 
  38.  */  
  39. public static Message obtain(Handler h, int what, Object obj) {  
  40.     Message m = obtain();  
  41.     m.target = h;  
  42.     m.what = what;  
  43.     m.obj = obj;  
  44.   
  45.     return m;  
  46. }  
  47.   
  48. /** 
  49.  * Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>,  
  50.  * <em>arg1</em>, and <em>arg2</em> members. 
  51.  *  
  52.  * @param h  The <em>target</em> value to set. 
  53.  * @param what  The <em>what</em> value to set. 
  54.  * @param arg1  The <em>arg1</em> value to set. 
  55.  * @param arg2  The <em>arg2</em> value to set. 
  56.  * @return  A Message object from the global pool. 
  57.  */  
  58. public static Message obtain(Handler h, int what, int arg1, int arg2) {  
  59.     Message m = obtain();  
  60.     m.target = h;  
  61.     m.what = what;  
  62.     m.arg1 = arg1;  
  63.     m.arg2 = arg2;  
  64.   
  65.     return m;  
  66. }  
  67.   
  68. /** 
  69.  * Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>,  
  70.  * <em>arg1</em>, <em>arg2</em>, and <em>obj</em> members. 
  71.  *  
  72.  * @param h  The <em>target</em> value to set. 
  73.  * @param what  The <em>what</em> value to set. 
  74.  * @param arg1  The <em>arg1</em> value to set. 
  75.  * @param arg2  The <em>arg2</em> value to set. 
  76.  * @param obj  The <em>obj</em> value to set. 
  77.  * @return  A Message object from the global pool. 
  78.  */  
  79. public static Message obtain(Handler h, int what,   
  80.         int arg1, int arg2, Object obj) {  
  81.     Message m = obtain();  
  82.     m.target = h;  
  83.     m.what = what;  
  84.     m.arg1 = arg1;  
  85.     m.arg2 = arg2;  
  86.     m.obj = obj;  
  87.   
  88.     return m;  
  89. }  


都是先调用obtain()方法,然后把获取的Message实例加上各种参数。

 

 

[java] view plaincopy
  1. /** 
  2.  * Return a Message instance to the global pool.  You MUST NOT touch 
  3.  * the Message after calling this function -- it has effectively been 
  4.  * freed. 
  5.  */  
  6. public void recycle() {  
  7.     clearForRecycle();  
  8.   
  9.     synchronized (sPoolSync) {  
  10.         if (sPoolSize < MAX_POOL_SIZE) {  
  11.             next = sPool;  
  12.             sPool = this;  
  13.             sPoolSize++;  
  14.         }  
  15.     }  
  16. }  


向全局池中返回一个Message实例。一定不能在调用此函数后再使用Message——它会立即被释放。

 

 

[java] view plaincopy
  1. /** 
  2.  * Make this message like o.  Performs a shallow copy of the data field. 
  3.  * Does not copy the linked list fields, nor the timestamp or 
  4.  * target/callback of the original message. 
  5.  */  
  6. public void copyFrom(Message o) {  
  7.     this.flags = o.flags & ~FLAGS_TO_CLEAR_ON_COPY_FROM;  
  8.     this.what = o.what;  
  9.     this.arg1 = o.arg1;  
  10.     this.arg2 = o.arg2;  
  11.     this.obj = o.obj;  
  12.     this.replyTo = o.replyTo;  
  13.   
  14.     if (o.data != null) {  
  15.         this.data = (Bundle) o.data.clone();  
  16.     } else {  
  17.         this.data = null;  
  18.     }  
  19. }  


使此message跟参数o相似。浅拷贝数据域。不拷贝源message的链表字段,时间戳和目标/回调。

 

 

[java] view plaincopy
  1. /** 
  2.      * Return the targeted delivery time of this message, in milliseconds. 
  3.      */  
  4.     public long getWhen() {  
  5.         return when;  
  6.     }  

 

设置一个任意数据值的Bundle对象。如果可以,使用arg1arg2域发送一些整型值以减少消耗。

参考

         getData()

         peekData()


返回此消息的传输时间,以毫秒为单位。

 

[java] view plaincopy
  1. public void setTarget(Handler target) {  
  2.         this.target = target;  
  3.     }  


设置目标handler(接收其消息的Handler)。

 

 

[java] view plaincopy
  1. /** 
  2.   * Retrieve the a {@link android.os.Handler Handler} implementation that 
  3.   * will receive this message. The object must implement 
  4.   * {@link android.os.Handler#handleMessage(android.os.Message) 
  5.   * Handler.handleMessage()}. Each Handler has its own name-space for 
  6.   * message codes, so you do not need to 
  7.   * worry about yours conflicting with other handlers. 
  8.   */  
  9.  public Handler getTarget() {  
  10.      return target;  
  11.  }  


获取将接收此消息的Handler对象。此对象必须要实现Handler.handleMessage()方法。每个handler各自包含自己的消息代码,所以不用担心自定义的消息跟其他handlers有冲突。

 

 

[java] view plaincopy
  1. /** 
  2.     * Retrieve callback object that will execute when this message is handled. 
  3.     * This object must implement Runnable. This is called by 
  4.     * the <em>target</em> {@link Handler} that is receiving this Message to 
  5.     * dispatch it.  If 
  6.     * not set, the message will be dispatched to the receiving Handler's 
  7.     * {@link Handler#handleMessage(Message Handler.handleMessage())}. 
  8.     */  
  9.    public Runnable getCallback() {  
  10.        return callback;  
  11.    }  


获取回调对象,此对象会在message处理时执行。此对象必须实现Runnable接口。回调由接收此消息并分发的目标handler调用。如果没有设置回调,此消息会分发到接收handlerhandleMessage(Message)

 

 

[java] view plaincopy
  1. /**  
  2.      * Obtains a Bundle of arbitrary data associated with this 
  3.      * event, lazily creating it if necessary. Set this value by calling 
  4.      * {@link #setData(Bundle)}.  Note that when transferring data across 
  5.      * processes via {@link Messenger}, you will need to set your ClassLoader 
  6.      * on the Bundle via {@link Bundle#setClassLoader(ClassLoader) 
  7.      * Bundle.setClassLoader()} so that it can instantiate your objects when 
  8.      * you retrieve them. 
  9.      * @see #peekData() 
  10.      * @see #setData(Bundle) 
  11.      */  
  12.     public Bundle getData() {  
  13.         if (data == null) {  
  14.             data = new Bundle();  
  15.         }  
  16.           
  17.         return data;  
  18.     }  

 

 

获取附加在此事件上的任意数据的Bundle对象,需要时延迟创建。通过调用setData(Bundle)来设置Bundle的值。需要注意的是,如果通过Messenger对象在进程间传递数据时,需要调用Bundle类的Bundle.setClassLoader()方法来设置ClassLoader,这样当接收到消息时可以实例化Bundle里的对象。

         参考

                  peekData()

                  setData(Bundle)

[java] view plaincopy
  1. /**  
  2.  * Like getData(), but does not lazily create the Bundle.  A null 
  3.  * is returned if the Bundle does not already exist.  See 
  4.  * {@link #getData} for further information on this. 
  5.  * @see #getData() 
  6.  * @see #setData(Bundle) 
  7.  */  
  8. public Bundle peekData() {  
  9.     return data;  
  10. }  

 

getData()相似,但是并不延迟创建Bundle。如果Bundle对象不存在返回null。更多信息见getData()

         参考

                   getData()

                   setData(Bundle)

[java] view plaincopy
  1. /** 
  2.  * Sets a Bundle of arbitrary data values. Use arg1 and arg1 members  
  3.  * as a lower cost way to send a few simple integer values, if you can. 
  4.  * @see #getData()  
  5.  * @see #peekData() 
  6.  */  
  7. public void setData(Bundle data) {  
  8.     this.data = data;  
  9. }  

 

设置一个任意数据值的Bundle对象。如果可以,使用arg1arg2域发送一些整型值以减少消耗。

参考

         getData()

         peekData()

[java] view plaincopy
  1. /** 
  2.  * Sends this Message to the Handler specified by {@link #getTarget}. 
  3.  * Throws a null pointer exception if this field has not been set. 
  4.  */  
  5. public void sendToTarget() {  
  6.     target.sendMessage(this);  
  7. }  


Handler发送此消息,getTarget()方法可以获取此Handler。如果这个字段没有设置会抛出个空指针异常。

 

[java] view plaincopy
  1. void clearForRecycle() {  
  2.         flags = 0;  
  3.         what = 0;  
  4.         arg1 = 0;  
  5.         arg2 = 0;  
  6.         obj = null;  
  7.         replyTo = null;  
  8.         when = 0;  
  9.         target = null;  
  10.         callback = null;  
  11.         data = null;  
  12.     }  


default方法,包内可见,清空所有数据。

 

 

[java] view plaincopy
  1. /*package*/ boolean isInUse() {  
  2.      return ((flags & FLAG_IN_USE) == FLAG_IN_USE);  
  3.  }  
  4.   
  5.  /*package*/ void markInUse() {  
  6.      flags |= FLAG_IN_USE;  
  7.  }  


获取Message是否在使用和标记为使用。

 

构造方法:

 

[java] view plaincopy
  1. /** Constructor (but the preferred way to get a Message is to call {@link #obtain() Message.obtain()}). 
  2.     */  
  3.     public Message() {  
  4.     }  


跟推荐使用Message.obtain()方法。

 

toString方法:

 

[java] view plaincopy
  1. public String toString() {  
  2.         return toString(SystemClock.uptimeMillis());  
  3.     }  
  4.   
  5.     String toString(long now) {  
  6.         StringBuilder   b = new StringBuilder();  
  7.           
  8.         b.append("{ what=");  
  9.         b.append(what);  
  10.   
  11.         b.append(" when=");  
  12.         TimeUtils.formatDuration(when-now, b);  
  13.   
  14.         if (arg1 != 0) {  
  15.             b.append(" arg1=");  
  16.             b.append(arg1);  
  17.         }  
  18.   
  19.         if (arg2 != 0) {  
  20.             b.append(" arg2=");  
  21.             b.append(arg2);  
  22.         }  
  23.   
  24.         if (obj != null) {  
  25.             b.append(" obj=");  
  26.             b.append(obj);  
  27.         }  
  28.   
  29.         b.append(" }");  
  30.           
  31.         return b.toString();  
  32.     }  

 

[java] view plaincopy
  1. public static final Parcelable.Creator<Message> CREATOR  
  2.             = new Parcelable.Creator<Message>() {  
  3.         public Message createFromParcel(Parcel source) {  
  4.             Message msg = Message.obtain();  
  5.             msg.readFromParcel(source);  
  6.             return msg;  
  7.         }  
  8.           
  9.         public Message[] newArray(int size) {  
  10.             return new Message[size];  
  11.         }  
  12.     };  


什么作用?

 

 

[java] view plaincopy
  1. public int describeContents() {  
  2.         return 0;  
  3.     }  

 

 

描述了包含在Parcelable对象排列信息中的特殊对象的类型。

返回值

         一个标志位,表明Parcelable对象特殊对象类型集合的排列。

[java] view plaincopy
  1. public void writeToParcel(Parcel dest, int flags) {  
  2.         if (callback != null) {  
  3.             throw new RuntimeException(  
  4.                 "Can't marshal callbacks across processes.");  
  5.         }  
  6.         dest.writeInt(what);  
  7.         dest.writeInt(arg1);  
  8.         dest.writeInt(arg2);  
  9.         if (obj != null) {  
  10.             try {  
  11.                 Parcelable p = (Parcelable)obj;  
  12.                 dest.writeInt(1);  
  13.                 dest.writeParcelable(p, flags);  
  14.             } catch (ClassCastException e) {  
  15.                 throw new RuntimeException(  
  16.                     "Can't marshal non-Parcelable objects across processes.");  
  17.             }  
  18.         } else {  
  19.             dest.writeInt(0);  
  20.         }  
  21.         dest.writeLong(when);  
  22.         dest.writeBundle(data);  
  23.         Messenger.writeMessengerOrNullToParcel(replyTo, dest);  
  24.     }  
  25.   
  26.     private final void readFromParcel(Parcel source) {  
  27.         what = source.readInt();  
  28.         arg1 = source.readInt();  
  29.         arg2 = source.readInt();  
  30.         if (source.readInt() != 0) {  
  31.             obj = source.readParcelable(getClass().getClassLoader());  
  32.         }  
  33.         when = source.readLong();  
  34.         data = source.readBundle();  
  35.         replyTo = Messenger.readMessengerOrNullFromParcel(source);  
  36.     }  


将类的数据写入外部提供的Parcel中和从Parcel中读取数据。

分享到:
评论

相关推荐

    Android代码-Android 一些重要知识点解析整理

    Android AsyncTask 完全解析,带你从源码的角度彻底理解 Android 异步消息处理机制完全解析,带你从源码角度彻底理解 Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系 Android消息...

    Android ApiDemos示例解析(26):App->Notification->IncomingMessage

    NULL 博文链接:https://kakukemeit.iteye.com/blog/1806388

    Handler Message源码分析及手写实现02.mp4

    Android Handler Message源码解析和手写实现

    消息循环 Looper 及其源码解析

    Android 消息处理机制之四: 消息循环 Looper 及其源码解析 http://blog.csdn.net/ahuier/article/details/17103517

    老罗android视频开发源码和ppt经典

    以下为视频源码目录: 一、Android入门介绍 视频教程 1.1 android系统介绍 1.2 android系统框架的介绍 1.3 如何搭建android开发环境 1.4 android生命周期的介绍 1.5 android使用全局变量传递数据 1.6 android使用...

    深入Android HandlerThread 使用及其源码完全解析

    关联篇:深入Android的消息机制源码详解-Handler,MessageQueue与Looper关系 关联篇:Handler内存泄漏及其解决方案 本篇我们将来给大家介绍HandlerThread这个类,以前我们在使用线程执行一个耗时任务时总会new一个...

    Android 初学中阶高阶书籍_集合打包2

    Android系统概述,Android系统的开发综述,Android的Linux内核与驱动程序,Android的底层库和程序,Android的JAVA虚拟机 ...图,Android的XML解析,牛人Android提高篇,手把手教你用Android开发新浪微博客户端

    Android 初学中阶高阶书籍_集合打包3

    Android系统概述,Android系统的开发综述,Android的Linux内核与驱动程序,Android的底层库和程序,Android的JAVA虚拟机 ...图,Android的XML解析,牛人Android提高篇,手把手教你用Android开发新浪微博客户端

    AsyncTask 源码解析.pdf

    Handler 常用来多线程之间传递消息,AsyncTask 内部实现 InternalHandler,用来发送和处理消息 MESSAGE_POST_RESULT、MESSAGE_POST_PROGRESS,对应 AsyncTask 的回调方法 onProgressUpdate 和 onPostExecute,这两个...

    android 消息机制

    从源码出发,详细的解析了android中的消息机制,分析清楚Looper和MessageQueue以及Handler三者之间的关系。

    百度地图开发java源码-blog-backup:学习文章,也是我博客的备份

    的事件分发机制实例和源码解析。 本篇参照郭神 Blog 中的实例,来分析 ViewGroup 的事件分发机制。 本章介绍 View(视图) 动画相关概念以及应用。 本篇介绍 Handler 和 Message 以及 Looper 的基本用法和工作原理。 ...

    aosp-asb

    一个简单的脚本,可将Android安全公告抓取为易于解析的CSV。 您可以在此存储库中找到各种输出。 用法: usage: main.py [-h] --patch-level PATCH_LEVEL [--android-version ANDROID_VERSION] [--out-dir OUT_DIR...

    深入Android Handler,MessageQueue与Looper关系

    关联篇:HandlerThread 使用及其源码完全解析 关联篇:Handler内存泄漏详解及其解决方案 一说到Android的消息机制,自然就会联想到Handler,我们知道Handler是Android消息机制的上层接口,因此我们在开发过程中也只...

    全面总结Android中线程的异步处理方式

    一、概述 Handler 、 Looper 、Message 这三者都与Android异步消息处理线程相关的概念。那么什么叫异步消息处理线程呢? 异步消息处理线程启动后会进入一个无限的循环体之中,每循环一次,从其内部的...二、源码解析

    android异步消息机制 源码层面彻底解析(1)

    Handler、Message、Loopler、MessageQueen 首先看一下我们平常使用Handler的一个最常见用法。 Handler handler =new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg);...

    JAVA上百实例源码以及开源项目

    此时此景,笔者只专注Android、Iphone等移动平台开发,看着这些源码心中有万分感慨,写此文章纪念那时那景! Java 源码包 Applet钢琴模拟程序java源码 2个目标文件,提供基本的音乐编辑功能。编辑音乐软件的朋友,这...

    leetcode安卓-jfson.github.io:关于安卓

    Handler、Looper、MessageQueue 7 Fragment、FragmentManager、Activity 8 LRU算法 9 Window、ViewRootImpl ... 待续 ... 待续 ... Binder Framework 梳理Android Binder的IPC机制,如何进行进程间通信 序号 文章名 ...

    JAVA上百实例源码以及开源项目源代码

     Message-Driven Bean EJB实例源代码,演示一个接收购物订单的消息驱动Bean,处理这个订单同时通过e-mail的形式  //给客户发一个感谢消息,消息驱动Bean必须实现两个接口MessageDrivenBean和MessageListener  在...

Global site tag (gtag.js) - Google Analytics