1 package android.os; 2 import android.os.Bundle; 3 import android.os.Parcel; 4 import android.os.Parcelable; 5 public final class Message implements Parcelable {//可序列化; 6 public int what;//消息标识; 7 public int arg1;//附带Integer参数 1; 8 public int arg2;//附带Integer参数 2; 9 public Object obj;//附带对象参数 obj; 10 public Messenger replyTo; 11 long when; //时间; 12 Bundle data;//Bundle数据结构; 13 Handler target;//目标Handler主要管理消息的接受与发送; 14 Runnable callback; // Runnable对象; 15 Message next;//下一条; 16 private static Object mPoolSync = new Object();//同步锁; 17 private static Message mPool;//消息池的消息; 18 private static int mPoolSize = 0;//默认消息数; 19 private static final int MAX_POOL_SIZE = 10;//总数; 20 21 /** 22 * 类方法,返回一个Message; 23 */ 24 public static Message obtain() { 25 synchronized (mPoolSync) {//同步; 26 if (mPool != null) { 27 Message m = mPool;//得到mPool; 28 mPool = m.next; 29 m.next = null; 30 return m; 31 } 32 } 33 return new Message(); 34 } 35 36 /** 37 * 类方法,复制一个Message;; 38 */ 39 public static Message obtain(Message orig) { 40 Message m = obtain(); 41 m.what = orig.what; 42 m.arg1 = orig.arg1; 43 m.arg2 = orig.arg2; 44 m.obj = orig.obj; 45 m.replyTo = orig.replyTo; 46 if (orig.data != null) { 47 m.data = new Bundle(orig.data); 48 } 49 m.target = orig.target; 50 m.callback = orig.callback; 51 return m; 52 } 53 54 /** 55 * 类方法,绑定一个Handler的Message; 56 */ 57 public static Message obtain(Handler h) { 58 Message m = obtain(); 59 m.target = h; 60 return m; 61 } 62 63 /** 64 * 类方法,绑定一个Handler和Runnalbe的Message; 65 */ 66 public static Message obtain(Handler h, Runnable callback) { 67 Message m = obtain(); 68 m.target = h; 69 m.callback = callback; 70 return m; 71 } 72 73 /** 74 * 类方法,绑定一个Handler和一个消息标识; 75 */ 76 public static Message obtain(Handler h, int what) { 77 Message m = obtain(); 78 m.target = h; 79 m.what = what; 80 return m; 81 } 82 83 /** 84 * 类方法,绑定一个Handler和一个消息标识,和一个对象形参; 85 */ 86 public static Message obtain(Handler h, int what, Object obj) { 87 Message m = obtain(); 88 m.target = h; 89 m.what = what; 90 m.obj = obj; 91 return m; 92 } 93 94 /** 95 * 类方法,绑定一个Handler和两个消息标识,和一个对象形参; 96 */ 97 public static Message obtain(Handler h, int what, int arg1, int arg2) { 98 Message m = obtain(); 99 m.target = h; 100 m.what = what; 101 m.arg1 = arg1; 102 m.arg2 = arg2; 103 return m; 104 } 105 public static Message obtain(Handler h, int what, 106 int arg1, int arg2, Object obj) { 107 Message m = obtain(); 108 m.target = h; 109 m.what = what; 110 m.arg1 = arg1; 111 m.arg2 = arg2; 112 m.obj = obj; 113 114 return m; 115 } 116 /** 117 * 回收消息; 118 */ 119 public void recycle() { 120 synchronized (mPoolSync) { 121 if (mPoolSize < MAX_POOL_SIZE) { 122 clearForRecycle(); 123 next = mPool; 124 mPool = this; 125 } 126 } 127 } 128 129 /** 130 * 复制消息; 131 */ 132 public void copyFrom(Message o) { 133 this.what = o.what; 134 this.arg1 = o.arg1; 135 this.arg2 = o.arg2; 136 this.obj = o.obj; 137 this.replyTo = o.replyTo; 138 139 if (o.data != null) { 140 this.data = (Bundle) o.data.clone(); 141 } else { 142 this.data = null; 143 } 144 } 145 146 //设置时间; 147 public long getWhen() { 148 return when; 149 } 150 151 //设置handler; 152 public void setTarget(Handler target) { 153 this.target = target; 154 } 155 156 public Handler getTarget() { 157 return target; 158 } 159 160 public Runnable getCallback() { 161 return callback; 162 } 163 164 public Bundle getData() { 165 if (data == null) { 166 data = new Bundle(); 167 } 168 169 return data; 170 } 171 172 public Bundle peekData() { 173 return data; 174 } 175 176 //设置data; 177 public void setData(Bundle data) { 178 this.data = data; 179 } 180 181 //通过目标Handler target发送消息; 182 public void sendToTarget() { 183 target.sendMessage(this); 184 } 185 186 /*package*/ void clearForRecycle() { 187 what = 0; 188 arg1 = 0; 189 arg2 = 0; 190 obj = null; 191 replyTo = null; 192 when = 0; 193 target = null; 194 callback = null; 195 data = null; 196 } 197 198 public Message() { 199 } 200 201 public String toString() { 202 StringBuilder b = new StringBuilder(); 203 204 b.append("{ what="); 205 b.append(what); 206 207 b.append(" when="); 208 b.append(when); 209 210 if (arg1 != 0) { 211 b.append(" arg1="); 212 b.append(arg1); 213 } 214 215 if (arg2 != 0) { 216 b.append(" arg2="); 217 b.append(arg2); 218 } 219 220 if (obj != null) { 221 b.append(" obj="); 222 b.append(obj); 223 } 224 225 b.append(" }"); 226 227 return b.toString(); 228 } 229 230 //消息的序列化; 231 public static final Parcelable.Creator<Message> CREATOR 232 = new Parcelable.Creator<Message>() { 233 public Message createFromParcel(Parcel source) { 234 Message msg = Message.obtain(); 235 msg.readFromParcel(source); 236 return msg; 237 } 238 239 public Message[] newArray(int size) { 240 return new Message[size]; 241 } 242 }; 243 244 public int describeContents() { 245 return 0; 246 } 247 248 public void writeToParcel(Parcel dest, int flags) { 249 if (callback != null) { 250 throw new RuntimeException( 251 "Can't marshal callbacks across processes."); 252 } 253 dest.writeInt(what); 254 dest.writeInt(arg1); 255 dest.writeInt(arg2); 256 if (obj != null) { 257 try { 258 Parcelable p = (Parcelable)obj; 259 dest.writeInt(1); 260 dest.writeParcelable(p, flags); 261 } catch (ClassCastException e) { 262 throw new RuntimeException( 263 "Can't marshal non-Parcelable objects across processes."); 264 } 265 } else { 266 dest.writeInt(0); 267 } 268 dest.writeLong(when); 269 dest.writeBundle(data); 270 Messenger.writeMessengerOrNullToParcel(replyTo, dest); 271 } 272 273 private final void readFromParcel(Parcel source) { 274 what = source.readInt(); 275 arg1 = source.readInt(); 276 arg2 = source.readInt(); 277 if (source.readInt() != 0) { 278 obj = source.readParcelable(getClass().getClassLoader()); 279 } 280 when = source.readLong(); 281 data = source.readBundle(); 282 replyTo = Messenger.readMessengerOrNullFromParcel(source); 283 } 284 }