sendMessage方法流程
/packages/apps/Bluetooth/src/com/android/bluetooth/pbapclient/PbapClientStateMachine.java
351 public void disconnect(BluetoothDevice device) {
352 Log.d(TAG, "Disconnect Request " + device);
// 调用父类StateMachine的sendMessage函数
353 sendMessage(MSG_DISCONNECT, device);
354 }
/frameworks/base/core/java/com/android/internal/util/StateMachine.java
1713 /**
1714 * Enqueue a message to this state machine.
1715 *
1716 * Message is ignored if state machine has quit.
1717 */
1718 public void sendMessage(int what, Object obj) {
1719 // mSmHandler can be null if the state machine has quit.
1720 SmHandler smh = mSmHandler;
1721 if (smh == null) return;
1722 // 把消息加入队列
1723 smh.sendMessage(obtainMessage(what, obj));
1724 // smh.sendMessage(Message) -> sendMessageDelayed(msg, 0);
// private static class SmHandler extends Handler
/frameworks/base/core/java/android/os/Handler.java
339 /**
340 *
341 * Same as {@link #obtainMessage()}, except that it also sets the what and obj members
342 * of the returned Message.
343 *
344 * @param what Value to assign to the returned Message.what field.
345 * @param obj Value to assign to the returned Message.obj field.
346 * @return A Message from the global message pool.
347 */
348 public final Message obtainMessage(int what, Object obj)
349 {
350 return Message.obtain(this, what, obj); // 从消息池中获取消息对象
351 }
352
/frameworks/base/core/java/android/os/Message.java
205 /**
206 * Same as {@link #obtain()}, but sets the values of the <em>target</em>, <em>what</em>, and 207 * <em>obj</em> members.
208 * @param h The <em>target</em> value to set.
209 * @param what The <em>what</em> value to set.
210 * @param obj The <em>object</em> method to set.
211 * @return A Message object from the global pool.
212 */
213 public static Message obtain(Handler h, int what, Object obj) {
214 Message m = obtain();
215 m.target = h;
216 m.what = what;
217 m.obj = obj;
218
219 return m;
220 }
122 /**
123 * Return a new Message instance from the global pool. Allows us to
124 * avoid allocating new objects in many cases.
125 */
126 public static Message obtain() {
127 synchronized (sPoolSync) {
128 if (sPool != null) {
129 Message m = sPool;
130 sPool = m.next;
131 m.next = null;
132 m.flags = 0; // clear in-