我在 android 的套接字程序中的 run() 线程方法中放置了几个 onclick 方法。
他们似乎没有工作。可以将这些 onclick 侦听器放在 run() 方法中,或者现在可以。不知道为什么按钮在单击它们时没有产生响应。消息未发送
这是我正在使用的两个 onclick 侦听器。一个是 sendA,另一个是 sendB,他们发送短信 testA 和 testB
public class ClientThread implements Runnable {
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, 8080);
connected = true;
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
final PrintWriter out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream())), true);
sendA.setOnClickListener(new View.OnClickListener() {
// ananamous inner class override for on click
public void onClick(View v) {
out.println("testA");
}
});
sendB.setOnClickListener(new View.OnClickListener() {
// ananamous inner class override for on click
public void onClick(View v) {
out.println("testB");
}
});
// where you issue the commands
// out.println("testX");
Log.d("ClientActivity", "C: Sent.");
} catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
}
socket.close();
Log.d("ClientActivity", "C: Closed.");
} catch (Exception e) {
Log.e("ClientActivity", "C: Error", e);
connected = false;
}
}
}