Java 无法理解 Android 蓝牙示例中的 mHandler.obtainMessage()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18030942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't Understand mHandler.obtainMessage() in Android Bluetooth sample
提问by
I'm Working on Bluetooth rfcomm connection. There's a line in Android Sample that I can't understand and unfortunately I couldn't find a good answer in other questions and resources.
我正在处理蓝牙 rfcomm 连接。Android 示例中有一行我无法理解,不幸的是我在其他问题和资源中找不到好的答案。
Here is the whole code:
这是整个代码:
public void run() {
byte[] buffer = new byte[1024]; // buffer store for the stream
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
/* Call this from the main activity to send data to the remote device */
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) { }
}
I can't Understand this line:
我无法理解这一行:
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
mHandleris not defined in this code and also MESSAGE_READ
mHandler未在此代码中定义,而且 MESSAGE_READ
I can't understand what does bytesdo?
我不明白有什么作用bytes?
I think and as mentioned in comment it sends the received Bytes to the Activity which I set as my Main Activity. Can I make a Static TextViewin my main Activity instead of sendToTarget() to show the received message?
我认为,正如评论中提到的,它将接收到的字节发送到我设置为我的主要活动的活动。我可以Static TextView在我的主活动中创建一个而不是 sendToTarget() 来显示收到的消息吗?
回答by Maxim Shoustin
The main goal of Handleris to provide interface between producer and consumer thread, here, between UI thread and worker thread. Implementation of Handlergoes into consumer thread.
的主要目标Handler是提供生产者和消费者线程之间的接口,这里是 UI 线程和工作线程之间的接口。的实现Handler进入消费者线程。
In your case, you want to communicate MESSAGE_READbetween threads.
在您的情况下,您希望MESSAGE_READ在线程之间进行通信。
Without handler you can do nothing out of your main Activity Thread.
如果没有处理程序,您将无法从主活动线程中执行任何操作。
Therefore, look for mHandlerinitiation into main Activity.
因此,寻找mHandler进入主要活动的启动。
The default handler init should be like:
默认处理程序 init 应该是这样的:
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
/**/
}
};
If you use Eclipse, click on your Project -> Ctrl+H -> File Search -> "Handler".
如果您使用 Eclipse,请单击您的项目 -> Ctrl+H -> 文件搜索 -> “处理程序”。
Or in Notepad++ -> Serch -> Find in files ....
或者在 Notepad++ -> Serch -> Find in files ....
[EDIT]
[编辑]
final int MESSAGE_READ = 9999; // its only identifier to tell to handler what to do with data you passed through.
// Handler in DataTransferActivity
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case SOCKET_CONNECTED: {
mBluetoothConnection = (ConnectionThread) msg.obj;
if (!mServerMode)
mBluetoothConnection.write("this is a message".getBytes());
break;
}
case DATA_RECEIVED: {
data = (String) msg.obj;
tv.setText(data);
if (mServerMode)
mBluetoothConnection.write(data.getBytes());
}
case MESSAGE_READ:
// your code goes here
I'm sure you must implement something like:
我确定您必须实现以下内容:
new ConnectionThread(mBluetoothSocket, mHandler);
sources i found here
我在这里找到的来源

