/**
* 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.
*/publicstaticMessageobtain(Messageorig){Messagem=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;m.workSourceUid=orig.workSourceUid;if(orig.data!=null){m.data=newBundle(orig.data);}m.target=orig.target;m.callback=orig.callback;returnm;}
/**
* Same as {@link #obtain()}, but sets the value for the <em>target</em> member on the Message returned.
* @param h Handler to assign to the returned Message object's <em>target</em> member.
* @return A Message object from the global pool.
*/publicstaticMessageobtain(Handlerh){Messagem=obtain();m.target=h;returnm;}
如果指定了对象,就可以通过sendToTarget()方法直接发送Message了。
callback
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* 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.
*/publicstaticMessageobtain(Handlerh,Runnablecallback){Messagem=obtain();m.target=h;m.callback=callback;returnm;}
publicvoidrecycle(){if(isInUse()){if(gCheckRecycle){thrownewIllegalStateException("This message cannot be recycled because it is still in use.");}return;}recycleUnchecked();}
/**
* 回收一个可能正在使用中的Message。
* 当队列中的Message被释放时,由MessageQueue 和 Looper在内部调用此方法。
*/@UnsupportedAppUsagevoidrecycleUnchecked(){// 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=UID_NONE;workSourceUid=UID_NONE;when=0;target=null;callback=null;data=null;synchronized(sPoolSync){if(sPoolSize<MAX_POOL_SIZE){next=sPool;// 添加入链条
sPool=this;sPoolSize++;}}}