Message类obtain()方法和sendToTarget()方法源码解析

public void onMounted(String mountPoint) {
    LogUtils.i(TAG,"onMounted,mountPoint="+mountPoint);
    Message.obtain(mHandler,MSG_DO_MOUNTED,mountPoint).sendToTarget();
}

源码分析:
 
 
Return a new Message instance from the global pool. Allows us to avoid allocating new objects in many cases.

 
 
122
    public static Message obtain() {
123
        synchronized () {
124
            if ( != null) {
125
                Message m = ;
126
                 = m.next;
127
                m.next = null;
128
                m.flags = 0; // clear in-use flag
129
                --;
130
                return m;
131
            }
132
        }
133
        return new Message();
134
    }

    
Same as  obtain(), but copies the values of an existing message (including its target) into the new one.

Parameters:
orig Original message to copy.
Returns:
A Message object from the global pool.
141

 
 
142
    public static Message obtain(Message orig) {
143
        Message m = obtain();
144
        m.what = orig.what;
145
        m.arg1 = orig.arg1;
146
        m.arg2 = orig.arg2;
147
        m.obj = orig.obj;
148
        m.replyTo = orig.replyTo;
149
        m.sendingUid = orig.sendingUid;
150
        if (orig.data != null) {
151
            m.data = new Bundle(orig.data);
152
        }
153
        m.target = orig.target;
154
        m.callback = orig.callback;
155

 
 
156
        return m;
157
    }

    
Same as  obtain(), but sets the value for the  target member on the Message returned.

Parameters:
h Handler to assign to the returned Message object's  target member.
Returns:
A Message object from the global pool.
163

 
 
164
    public static Message obtain(Handler h) {
165
        Message m = obtain();
166
        m.target = h;
167

 
 
168
        return m;
169
    }

    
Same as  obtain(android.os.Handler), but assigns a callback Runnable on the Message that is returned.

Parameters:
h Handler to assign to the returned Message object's  target member.
callback Runnable that will execute when the message is handled.
Returns:
A Message object from the global pool.
177

 
 
178
    public static Message obtain(Handler hRunnable callback) {
179
        Message m = obtain();
180
        m.target = h;
181
        m.callback = callback;
182

 
 
183
        return m;
184
    }

    
Same as  obtain(), but sets the values for both  target and  what members on the Message.

Parameters:
h Value to assign to the  target member.
what Value to assign to the  what member.
Returns:
A Message object from the global pool.
192

 
 
193
    public static Message obtain(Handler hint what) {
194
        Message m = obtain();
195
        m.target = h;
196
        m.what = what;
197

 
 
198
        return m;
199
    }

    
Same as  obtain(), but sets the values of the  targetwhat, and  obj members.

Parameters:
h The  target value to set.
what The  what value to set.
obj The  object method to set.
Returns:
A Message object from the global pool.
208

 
 
209
    public static Message obtain(Handler hint whatObject obj) {
210
        Message m = obtain();
211
        m.target = h;
212
        m.what = what;
213
        m.obj = obj;
214

 
 
215
        return m;
216
    }

    
Same as  obtain(), but sets the values of the  targetwhatarg1, and  arg2 members.

Parameters:
h The  target value to set.
what The  what value to set.
arg1 The  arg1 value to set.
arg2 The  arg2 value to set.
Returns:
A Message object from the global pool.
227

 
 
228
    public static Message obtain(Handler hint whatint arg1int arg2) {
229
        Message m = obtain();
230
        m.target = h;
231
        m.what = what;
232
        m.arg1 = arg1;
233
        m.arg2 = arg2;
234

 
 
235
        return m;
236
    }

    
Same as  obtain(), but sets the values of the  targetwhatarg1arg2, and  obj members.

Parameters:
h The  target value to set.
what The  what value to set.
arg1 The  arg1 value to set.
arg2 The  arg2 value to set.
obj The  obj value to set.
Returns:
A Message object from the global pool.
248

 
 
249
    public static Message obtain(Handler hint what
250
            int arg1int arg2Object obj) {
251
        Message m = obtain();
252
        m.target = h;
253
        m.what = what;
254
        m.arg1 = arg1;
255
        m.arg2 = arg2;
256
        m.obj = obj;
257

 
 
258
        return m;
259
    }

 
 
 
Sends this Message to the Handler specified by  getTarget(). Throws a null pointer exception if this field has not been set.
414

 
 
415
    public void sendToTarget() {
416
        .sendMessage(this);
417
    }

 
 
 
Pushes a message onto the end of the message queue after all pending messages before the current time. It will be received in handleMessage(android.os.Message), in the thread attached to this handler.

Returns:
Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting.
504

 
 
505
    public final boolean sendMessage(Message msg)
506
    {
507
        return sendMessageDelayed(msg, 0);
508
    }

 
 
 
Enqueue a message into the message queue after all pending messages before (current time + delayMillis). You will receive it in handleMessage(android.os.Message), in the thread attached to this handler.

Returns:
Returns true if the message was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the message will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.
564

 
 
565
    public final boolean sendMessageDelayed(Message msglong delayMillis)
566
    {
567
        if (delayMillis < 0) {
568
            delayMillis = 0;
569
        }
570
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
571
    }


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值