i put a couple of onclick methods inside of a run() thread method in a sockets program for android.
they don't seem to work. is possible to do put these onclick listeners inside of the run() method or it it now possible. not sure why the buttons are not generating a response when clicking on them. the message is not getting sent
here is the two onclick listeners that i am using. one is sendA and the other is sendB and they send the text messages testA and 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;
}
}
}