在handler.obtainMessage()的参数是这样写的:
Message android.os.Handler.obtainMessage(int what, int arg1, intarg2, Object obj)
public final Message obtainMessage (int what, int arg1, int arg2,Object obj)
Since: API Level 1
Same as obtainMessage(), except that it also sets the what, obj,arg1,and arg2 values on the returned Message.
Parameters
whatValue to assign to thereturned Message.what field.
arg1Value to assign to thereturned Message.arg1 field.
arg2Value to assign to thereturned Message.arg2 field.
objValue to assign to thereturned Message.obj field.
而Handler中obtainMessage与new Message的区别:
obtainmessage()是从消息池中拿来一个msg不需要另开辟空间new
new需要重新申请,效率低,obtianmessage可以循环利用;
//useHandler.obtainMessage(),insteadofmsg=newMessage();
//becauseifthereisalreadyanMessageobject,thatnotbeusedby
//anyone,thesystemwillhandusethatobject,soyoudon'thaveto
//createandobjectandallocatememory.
//itisalsoanotherexampleofobjectrecyclingandreusinginandroid.
Messagemsg=mHandler.obtainMessage();
msg.what=UPDATE_LISTVIEW;
msg.obj=current+"/"+total+"songs";
//thismethodiscalledfromworkerThread,sowecannotupdateUIfromhere.
msg.sendToTarget();
再看下面代码:Messagemsg=handler.obtainMessage();
msg.arg1=i;
msg.sendToTarget();
Messagemsg=newMessage();
msg.arg1=i;
handler.sendMessage(msg);
第一种写法是message从handler 类获取,从而可以直接向该handler 对象发送消息,第二种写法是直接调用 handler的发送消息方法发送消息。