Hander Message 传递对象

我们在Message的源码中发现

 
  
  1. public final class Message implements Parcelable { 
  2.     /** 
  3.      * User-defined message code so that the recipient can identify  
  4.      * what this message is about. Each {@link Handler} has its own name-space 
  5.      * for message codes, so you do not need to worry about yours conflicting 
  6.      * with other handlers. 
  7.      */ 
  8.     public int what; 
  9.  
  10.     /** 
  11.      * arg1 and arg2 are lower-cost alternatives to using 
  12.      * {@link #setData(Bundle) setData()} if you only need to store a 
  13.      * few integer values. 
  14.      */ 
  15.     public int arg1;  
  16.  
  17.     /** 
  18.      * arg1 and arg2 are lower-cost alternatives to using 
  19.      * {@link #setData(Bundle) setData()} if you only need to store a 
  20.      * few integer values. 
  21.      */ 
  22.     public int arg2; 
  23.  
  24.     /** 
  25.      * An arbitrary object to send to the recipient.  When using 
  26.      * {@link Messenger} to send the message across processes this can only 
  27.      * be non-null if it contains a Parcelable of a framework class (not one 
  28.      * implemented by the application).   For other data transfer use 
  29.      * {@link #setData}. 
  30.      *  
  31.      * <p>Note that Parcelable objects here are not supported prior to 
  32.      * the {@link android.os.Build.VERSION_CODES#FROYO} release. 
  33.      */ 
  34.     public Object obj;

 

    以上值得注意的地方是:
    //可以注意这里的解释,这是一个任意的对象,用来传递给接收者
    //对于其他的数据,如基本类型可以使用setData
    //注意点是:这里不支持Parcelable类型的对象

于是可以像下面这样来使用

 
  
  1. if(handler!=null){  
  2.             Message message = handler.obtainMessage();  
  3.             message.what = 123123;  
  4.             message.obj=new Object();  
  5.             handler.sendMessageDelayed(message, 1000);  
  6.         }