gpt4 book ai didi

java - 多次运行 Runnable Thead (Android)

转载 作者:行者123 更新时间:2023-12-01 13:05:44 24 4
gpt4 key购买 nike

我之前发布了一个问题,您帮助了我,并在我的项目中使我取得了显着的进步,并且正在寻找最后的帮助。

我的笔记本电脑上运行着一个 Java TCP 服务器,它只是不断地读取通过该 TCP 端口给出的行。我 100% 肯定该功能正在运行,我在没有 Android 应用程序的情况下对其进行了测试。

我的问题出在这段代码中:

public class MainActivity extends Activity {

private String serverIpAddress = "10.30.13.110";
private int serverPort = 50505;
private boolean connected = false;
private char launchercontrolcode;
Button button1;
ImageButton button2, button3, button4, button5, button6;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button1 = (Button)findViewById(R.id.armbutton);
button1.setOnClickListener(onClickListener);
button2 = (ImageButton)findViewById(R.id.firebutton);
button2.setOnClickListener(onClickListener);
button3 = (ImageButton)findViewById(R.id.upbutton);
button3.setOnClickListener(onClickListener);
button4 = (ImageButton)findViewById(R.id.downbutton);
button4.setOnClickListener(onClickListener);
button5 = (ImageButton)findViewById(R.id.leftbutton);
button5.setOnClickListener(onClickListener);
button6 = (ImageButton)findViewById(R.id.rightbutton);
button6.setOnClickListener(onClickListener);
}


private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.armbutton:
if (!connected) {
launchercontrolcode = 'A';
Thread cThread = new Thread(new ClientThread());
cThread.start();
for(int i =0 ; i<10; i++);
}
break;
case R.id.firebutton:
if (!connected) {
launchercontrolcode = 'F';
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
break;
case R.id.upbutton:
if (!connected) {
launchercontrolcode = 'U';
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
break;
case R.id.downbutton:
if (!connected) {
launchercontrolcode = 'D';
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
break;
case R.id.leftbutton:
if (!connected) {
launchercontrolcode = 'L';
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
break;
case R.id.rightbutton:
if (!connected) {
launchercontrolcode = 'R';
Thread cThread = new Thread(new ClientThread());
cThread.start();
}
break;
}
}
};

public class ClientThread implements Runnable {
public void run() {
try {
Socket socket = new Socket(serverIpAddress, serverPort);
connected = true;
Log.d("ClientActivity", "Client Connected");
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(launchercontrolcode);
Log.d("ClientActivity", "Cliend Sent Code");
out.flush();
out.close();
} catch (Exception e) {}
socket.close();
Log.d("ClientActivity", "Post-socket close");
} catch (Exception e) {
connected = false;

}
}
}
}

我的问题是,如果我按第一个按钮,它就会正常发送到服务器;但是,当我按第二次(或 X 次)时,由于某种原因它不会发送到服务器。我相当有信心问题出在我的 switch 语句中,但不确定它到底是什么。

提前致谢:)

最佳答案

问题是,一旦您进入该线程.. boolean 连接设置为true,但是当您完成并关闭所有连接时它仍然是true..

为什么?因为它在 catch 内,所以它永远不应该在那里..如果您想再次单击另一个按钮..您正在等待再次单击的异常..

解决方案:

public class ClientThread implements Runnable {
public void run() {
try {
Socket socket = new Socket(serverIpAddress, serverPort);
connected = true;
Log.d("ClientActivity", "Client Connected");
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
out.println(launchercontrolcode);
Log.d("ClientActivity", "Cliend Sent Code");
out.flush();
out.close();
} catch (Exception e) {}
socket.close();
Log.d("ClientActivity", "Post-socket close");
} catch (Exception e) {

}finally{
connected = false;
}

}
}
}

关于java - 多次运行 Runnable Thead (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23283664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com