标签:
public final Message obtainMessage (int what, int arg1, int arg2, Object obj)
这里面的4个参数求解释啊。。。
byte[] buffer = new byte[1024];
int bytes;
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer) .sendToTarget();
这个是实例的代码
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;//这是把缓冲区给了readBuf吗?
String ss = new String(readBuf, 0,msg.arg1);//这一句什么意思,貌似arg1存的就是读入的值
--------------------------------------------------------------------------------------
-就是你的Message传给Handler的值,
Message msg = handler.obtainMessage();
msg.arg1 = 1;
msg.arg2 = 2;
msg.what = 3;
msg.obj = Object;
在Handler中接收
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//接收Message发送的消息 msg.what msg.arg1 msg.arg2 msg.obj
//这里msg.obj需要强转成你传过来的数据类型
}
};
-------------------------------------------------------
mHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer) .sendToTarget();
-------------------------------------------------------
new String(readBuf, 0,msg.arg1); String (byte[] data, int offset, int byteCount)
msg.arg1充当了byteCount,也就是字符长度。msg.arg1不是就是在传消息之前的bytes的赋值吗,而bytes = mmInStream.read(buffer),也就是把单字符给了bytes
android中Handler中的obtainMessage()
标签:
原文地址:http://www.cnblogs.com/tonglingqijie/p/4786873.html