android源码解析--Message

看下类定义:

 

[java]  view plain copy
  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 plain copy
  1. public final class Message implements Parcelable   


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

 

看一下全局变量:

 

[java]  view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  1. public void setTarget(Handler target) {  
  2.         this.target = target;  
  3.     }  


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

 

 

[java]  view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  1. public int describeContents() {  
  2.         return 0;  
  3.     }  

 

 

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

返回值

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

[java]  view plain copy
  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中读取数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的高校科研信息管理系统实现了操作日志管理、字典管理、反馈管理、公告管理、科研成果管理、科研项目管理、通知管理、学术活动管理、学院部门管理、科研人员管理、管理员管理等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让高校科研信息管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值