Message:
定義:
public final class Message implements Parcelable
Message類是個final類,就是說不能被繼承,同時Message類實現了Parcelable接口,我們知道android提供了一種新的類型:Parcel。本類被用作封裝數據的容器,是鏈表結構,有個屬性next和sPool,這兩個變量是不同的,具體什么不同看下文。
文檔描述:
Defines a message containing a description and arbitrary data object that can be sent to a {@link Handler}. This object contains two extra int fields and an
extra object field that allow you to not do allocations in many cases.
定義一個包含任意類型的描述數據對象,此對象可以發送給Handler。對象包含兩個額外的int字段和一個額外的對象字段,這樣可以使得在很多情況下不用做分配工作。盡管Message的構造器是公開的,但是獲取Message對象的最好方法是調用Message.obtain()或者Handler.obtainMessage(), 這樣是從一個可回收對象池中獲取Message對象。
1.看一下全局變量:有好多存數據的對象。
public int what; public int arg1; public int arg2; public Object obj; public Messenger replyTo; /*package*/ int flags; /*package*/ long when; /*package*/ Bundle data; /*package*/ Handler target; /*package*/ Runnable callback; // sometimes we store linked lists of these things /*package*/ Message next; private static final Object sPoolSync = new Object(); private static Message sPool; private static int sPoolSize = 0; private static final int MAX_POOL_SIZE = 50; private static boolean gCheckRecycle = true;
- what:用戶定義消息代碼以便收件人可以識別這是哪一個Message。每個Handler用它自己的名稱空間為消息代碼,所以您不需要擔心你的Handler與其他handler沖突。
- arg1、arg2:如果只是想向message內放一些整數值,可以使用arg1和arg2來代替setData方法。
- obj:發送給接收器的任意對象。當使用Message對象在線程間傳遞消息時,如果它包含一個Parcelable的結構類(不是由應用程序實現的類),此字段必須為非空(non-null)。其他的數據傳輸則使用setData(Bundle)方法。注意Parcelable對象是從FROYO版本以后才開始支持的。
- replyTo:指明此message發送到何處的可選Messenger對象。具體的使用方法由發送者和接受者決定。
- FLAG_IN_USE:判斷Message是否在使用( default 包內可見)
- FLAG_ASYNCHRONOUS:如果設置message是異步的。
- FLAGS_TO_CLEAR_ON_COPY_FROM:明確在copyFrom方法
- 其他參數都比較簡單,不詳述
Obtain方法:
//從全局池中返回一個新的Message實例。在大多數情況下這樣可以避免分配新的對象。
//是一個靜態方法
public static Message obtain() {
synchronized (sPoolSync) {
if (sPool != null) {
Message m = sPool;
sPool = m.next;
m.next = null;
m.flags = 0; // clear in-use flag
sPoolSize--;
return m;
}
}
return new Message();
}
在看它一系列的重載方法:
/**
* Same as {@link #obtain()}, but copies the values of an existing
* message (including its target) into the new one.
* @param orig Original message to copy.
* @return A Message object from the global pool.
*/
public static Message obtain(Message orig) {
Message m = obtain();
m.what = orig.what;
m.arg1 = orig.arg1;
m.arg2 = orig.arg2;
m.obj = orig.obj;
m.replyTo = orig.replyTo;
m.sendingUid = orig.sendingUid;
if (orig.data != null) {
m.data = new Bundle(orig.data);
}
m.target = orig.target;
m.callback = orig.callback;
return m;
}
/**
設置target
*/
public static Message obtain(Handler h) {
Message m = obtain();
m.target = h;
return m;
}
/**
* Same as {@link #obtain(Handler)}, but assigns a callback Runnable on
* the Message that is returned.
* @param h Handler to assign to the returned Message object's <em>target</em> member.
* @param callback Runnable that will execute when the message is handled.
* @return A Message object from the global pool.
*/
public static Message obtain(Handler h, Runnable callback) {
Message m = obtain();
m.target = h;
m.callback = callback;
return m;
}
/**
* Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>,
* <em>arg1</em>, <em>arg2</em>, and <em>obj</em> members.
。。。。
* @param obj The <em>obj</em> value to set.
* @return A Message object from the global pool.
*/
public static Message obtain(Handler h, int what,
int arg1, int arg2, Object obj) {
Message m = obtain();
m.target = h;
m.what = what;
m.arg1 = arg1;
m.arg2 = arg2;
m.obj = obj;
return m;
}
還有幾個沒列舉出來,都是先調用obtain()方法,然后把獲取的Message實例加上各種參數。代碼一目了然。。。
recycle():回收當前message到全局池
/**
* Return a Message instance to the global pool.
* <p>
* You MUST NOT touch the Message after calling this function because it has
* effectively been freed. It is an error to recycle a message that is currently
* enqueued or that is in the process of being delivered to a Handler.
* </p>
*/
public void recycle() {
if (isInUse()) {
if (gCheckRecycle) {
throw new IllegalStateException("This message cannot be recycled because it "
+ "is still in use.");
}
return;
}
recycleUnchecked();
}
/**
* Recycles a Message that may be in-use.
* Used internally by the MessageQueue and Looper when disposing of queued Messages.
*/
void recycleUnchecked() {
// Mark the message as in use while it remains in the recycled object pool.
// Clear out all other details.
flags = FLAG_IN_USE;
what = 0;
arg1 = 0;
arg2 = 0;
obj = null;
replyTo = null;
sendingUid = -1;
when = 0;
target = null;
callback = null;
data = null;
synchronized (sPoolSync) {
if (sPoolSize < MAX_POOL_SIZE) {
next = sPool;
sPool = this;
sPoolSize++;
}
}
}
向全局池中返回一個Message實例。一定不能在調用此函數后再使用Message——它實際上已經被釋放。
getWhen:
/**
* Return the targeted delivery time of this message, in milliseconds.
*/
public long getWhen() {
return when;
}
返回此消息的傳輸時間,以毫秒為單位。
setTarget,getTarget:
//設置handler和返回handler
public void setTarget(Handler target) {
this.target = target;
}
/**
* Retrieve the a {@link android.os.Handler Handler} implementation that
* will receive this message. The object must implement
* {@link android.os.Handler#handleMessage(android.os.Message)
* Handler.handleMessage()}. Each Handler has its own name-space for
* message codes, so you do not need to
* worry about yours conflicting with other handlers.
*/
public Handler getTarget() {
return target;
}
獲取將接收此消息的Handler對象。此對象必須要實現Handler.handleMessage()方法。每個handler各自包含自己的消息代碼,所以不用擔心自定義的消息跟其他handlers有沖突。
setData:
設置一個可以是任何類型值的bundle。
/**
* Sets a Bundle of arbitrary data values. Use arg1 and arg2 members
* as a lower cost way to send a few simple integer values, if you can.
* @see #getData()
* @see #peekData()
*/
public void setData(Bundle data) {
this.data = data;
}
getData,peekData
public Bundle getData() {
if (data == null) {
data = new Bundle();
}
return data;
}
public Bundle peekData() {
return data;
}
發送消息的一些方法:
/**向Handler發送此消息,getTarget()方法可以獲取此Handler。如果這個字段沒有設置會拋出個空指針異常。
* Sends this Message to the Handler specified by {@link #getTarget}.
* Throws a null pointer exception if this field has not been set.
*/
public void sendToTarget() {
target.sendMessage(this);
}
構造方法:
/** Constructor (but the preferred way to get a Message is to call {@link #obtain() Message.obtain()}).
*/
public Message() {
}
//推薦使用Message.obtain()
writeToParcel:
public void writeToParcel(Parcel dest, int flags) {
if (callback != null) {
throw new RuntimeException(
"Can't marshal callbacks across processes.");
}
dest.writeInt(what);
dest.writeInt(arg1);
dest.writeInt(arg2);
if (obj != null) {
try {
Parcelable p = (Parcelable)obj;
dest.writeInt(1);
dest.writeParcelable(p, flags);
} catch (ClassCastException e) {
throw new RuntimeException(
"Can't marshal non-Parcelable objects across processes.");
}
} else {
dest.writeInt(0);
}
dest.writeLong(when);
dest.writeBundle(data);
Messenger.writeMessengerOrNullToParcel(replyTo, dest);
dest.writeInt(sendingUid);
}
將類的數據寫入外部提供的Parcel中和從Parcel中讀取數據。
Message結束。。。。
