sendMessage方法流程

本文详细介绍了Android中sendMessage方法的使用,从将Message放入队列开始,探讨hasMessages和removeMessages的实现细节,揭示Android消息传递机制的核心流程。
摘要由CSDN通过智能技术生成

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-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值