我有一个Android库类。此类用于管理蓝牙连接,当创建套接字连接时,将启动一个新线程,该线程在蓝牙套接字上等待从蓝牙设备接收的传入消息。
当我收到一条新消息时,我会调用一个在调用线程上运行的函数,我尝试使用Handler
来执行此操作,但handleMessage()
函数始终在辅助线程上运行。
public class LibraryClass {
private ConnectedThread ct;
LibraryClass() {
// start blutooth connection
ct = new ConnectedThread();
ct.start();
}
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
// this code is running on ConnectionThread but I need to handle
// message on primary thread
}
}
};
private class ConnectedThread extends Thread {
public void run() {
while (true) {
// Read from the InputStream
bytes = mmInStream.read(buffer);
mHandler.dispatchMessage(msg);
}
}
}
}
你能帮我解决这个问题吗?