編輯:關於android開發
定義:
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對象。
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;
//從全局池中返回一個新的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實例加上各種參數。代碼一目了然。。。
/** * 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有沖突。
設置一個可以是任何類型值的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()
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結束。。。。
Java 單例模式,Java模式前言:昨天公司計劃把項目中的部分功能做出SDK的形式,供其他公司的產品使用,所以不得不重新研究一下單例模式。 為什麼單例 1
Linux內核系列—7.操作系統開發之中斷和異常,linux內核a.概述 中斷門和陷阱門的作用機理幾乎是一樣的,只不過使用調用門時使用call指令,而這裡我們使用int指
Android React-Native系列之(一)零基礎搭建React-Native開發環境 很早就聽說過大名鼎鼎的React-Native了(不大清楚的童靴們可以自行
QQ視差特效和ListView側滑刪除,視差特效listview側如圖所示是效果圖,當向下拉時,圖片會被拉出來,松手後恢復。和ListView的側滑刪除 &n
location of the android sdk has not