- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在帮助我的 friend 开发一个 Android 应用程序,我不明白为什么我无法获取 Handle
函数,handleMessage()
被调用。我目前正在尝试通过蓝牙进行通信,我有一个处理写入的线程和一个处理读取的线程,所以我想我在某个地方搞砸了。我真的不熟悉 Thread
,我想知道是否有人能发现我的错误!我知道写入功能正在工作,因为我看到蓝牙芯片在写入"$$$"
下面的代码后进入命令模式。
主要 Activity :
public class MainActivity extends Activity{
....
private final Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
System.out.println("in mHandler");//NOT CALLED
switch (msg.what) {
case (MESSAGE_READING):
byte[] readBuf = (byte[])msg.obj;
String readMessage = new String(readBuf,0,msg.arg1);
System.out.println("READ: " + readMessage);
break;
default:
System.out.println("default!");
break;
}
}
};
...
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mConnectThread = new ConnectThread(device, mHandler);
mConnectThread.run();
ListenerThread mListener = new ListenerThread(mConnectThread,mHandler);
mListener.run();
}
连接线程:
public class ConnectThread{
public final BluetoothSocket mmSocket;
public final BluetoothDevice mmDevice;
private final Handler mHandler;
public static ConnectedThread mConnectedThread;
public ConnectThread(BluetoothDevice device, Handler mHandler) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
BluetoothSocket bs = null;
mmDevice = device;
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard Serial Port Service ID
bs = device.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
System.err.println("IOException in ConnectThread");
}
this.mHandler = mHandler;
mmSocket = bs;
}
public void run() {
//TODO Cancel discovery because it will slow down the connection
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection (in a separate thread)
mConnectedThread = new ConnectedThread(mmSocket, mHandler);
mConnectedThread.run();
}
...
}
连接线程:
public class ConnectedThread extends Thread {
....
public void manageConnectedSocket() {
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);
System.out.println("Reading bytes.");
status = MESSAGE_READING;
// Send the obtained bytes to the UI activity
Message msg = Message.obtain(mHandler, MESSAGE_READING, bytes, -1, buffer);
mHandler.sendMessage(msg);
} catch (IOException e) {
System.err.println("Error reading input");
break;
}
status = READY;
}
}
...
}
监听线程:
public class ListenerThread extends Thread {
...
public final BluetoothSocket mmSocket;
private final Handler mHandler;
private final ConnectedThread mConnectedThread;
public ListenerThread(ConnectThread mConnectThread, Handler mHandler){
this.mHandler = mHandler;
mmSocket = mConnectThread.mmSocket;
this.mConnectedThread = mConnectThread.getConnectedThread();
}
public void run(){
while (true){
if (getStatus() == READY){
write("$$$".getBytes());
break;
}
}
}
...
}
我查看了一些关于 handleMessage
的其他问题,但没有帮助。有什么想法吗?
编辑:刚刚意识到这是很多代码。基本上我将 mHandler
传递给我的不同线程,我认为这是发生了一些不好的事情的地方。我有 ConnectThread
、ConnectedThread
和 ListenerThread
。我正在查看蓝牙的 Android 文档说它在后台运行,因为一些调用(write
,read
,device.connect()
) 正在阻塞调用。
最佳答案
我认为您的代码存在问题,您是通过 run()
方法启动线程,这意味着您只是在一个对象上执行 run()
方法.但是您必须调用 start()
方法,这将启动一个新线程并自动调用您的 run()
方法。
这里引用自 Thread.start()
方法文档
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
关于java - 未调用 handleMessage 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17256654/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!