多次运行Runnable线程(Android)

时间:2014-04-25 03:31:06

标签: java android tcp tcpclient

我之前发过一个问题,你帮助我并在我的项目中为我提供了很大帮助,并且正在寻找最后一块帮助。

我的笔记本电脑上运行了一个Java TCP Server,它只是不断读取通过该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语句,但不确定它究竟是什么。

提前致谢:)

1 个答案:

答案 0 :(得分:1)

问题是,一旦你进入那个线程..布尔连接设置为true但是当你完成并关闭所有连接时它仍然是真的..

为什么?因为它在捕获内部它永远不应该在那里..如果你想再次点击另一个按钮..你正在等待异常再次点击..

<强>溶液

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;
      }

    }
}
}