向 UI 个话题发送消息
Send message to UI thread
我创建了一个线程来监听串口的输入流。
当有输入数据到来时,我想向 UI 线程发送消息。
在执行任务之前,我尝试以 5000 毫秒的间隔从线程发送消息,并向 UI 线程和 Toast
线程发送消息。
从日志中,我知道线程已创建并且 运行 成功,但是 UI 线程中的数字不是 toasted
。
我觉得可能是线程向UI线程发送消息失败,或者UI线程的MessageQueue
处理消息失败。
我的代码有什么问题?
非常感谢!
MainActivity.java:
public class MainActivity extends Activity {
...
@Override
protected void onCreate(Bundle savedInstancesState) {
...
setMessageProcessor();
SerialPortListener.start();
}
private void setMessageProcessor() {
new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
Toast.makeText(MainActivity.this,
"Message: " + msg.obj.toString(),
Toast.LENGTH_SHORT).show();
}
};
}
}
SerialPortListener.java:
public class SerialPortListener {
...
private static int testNumber = 1;
public static void start() {
...
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Message msg = new Message();
msg.obj = testNumber++;
new Handler(Looper.getMainLooper()).sendMessage(msg);
Log.i("TEST", "TEST);
try {
Thread.sleep(5000);
} catche(Exception e) {
...
}
}
}
});
thread.start();
}
}
这是因为您正在从 "reader thread" 中创建第二个 Handler
绑定到主线程。不要在主线程中为处理程序使用匿名对象,将其设为成员字段。然后在您的 reader 线程中,将 Message
发送到特定的 Handler
:
private void setMessageProcessor() {
mHandler = new Handler() {
...
}
}
public class SerialPortListener {
...
private static int testNumber = 1;
public static void start() {
...
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Message msg = mHandler.obtainMessage();
msg.obj = testNumber++;
msg.sendToTarget();
Log.i("TEST", "TEST);
...
}
}
}
我创建了一个线程来监听串口的输入流。 当有输入数据到来时,我想向 UI 线程发送消息。
在执行任务之前,我尝试以 5000 毫秒的间隔从线程发送消息,并向 UI 线程和 Toast
线程发送消息。
从日志中,我知道线程已创建并且 运行 成功,但是 UI 线程中的数字不是 toasted
。
我觉得可能是线程向UI线程发送消息失败,或者UI线程的MessageQueue
处理消息失败。
我的代码有什么问题? 非常感谢!
MainActivity.java:
public class MainActivity extends Activity {
...
@Override
protected void onCreate(Bundle savedInstancesState) {
...
setMessageProcessor();
SerialPortListener.start();
}
private void setMessageProcessor() {
new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
Toast.makeText(MainActivity.this,
"Message: " + msg.obj.toString(),
Toast.LENGTH_SHORT).show();
}
};
}
}
SerialPortListener.java:
public class SerialPortListener {
...
private static int testNumber = 1;
public static void start() {
...
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Message msg = new Message();
msg.obj = testNumber++;
new Handler(Looper.getMainLooper()).sendMessage(msg);
Log.i("TEST", "TEST);
try {
Thread.sleep(5000);
} catche(Exception e) {
...
}
}
}
});
thread.start();
}
}
这是因为您正在从 "reader thread" 中创建第二个 Handler
绑定到主线程。不要在主线程中为处理程序使用匿名对象,将其设为成员字段。然后在您的 reader 线程中,将 Message
发送到特定的 Handler
:
private void setMessageProcessor() {
mHandler = new Handler() {
...
}
}
public class SerialPortListener {
...
private static int testNumber = 1;
public static void start() {
...
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Message msg = mHandler.obtainMessage();
msg.obj = testNumber++;
msg.sendToTarget();
Log.i("TEST", "TEST);
...
}
}
}