0

我创建了一个带有服务器套接字的类,它接受来自客户端的传入连接。现在我需要在连接客户端时显示一个按钮。我该怎么做?我要实现一个事件监听器吗?这是服务器类:

public class MyServer implements Runnable {

    public int serverPort = 8080;
    public String serverIp = "http://192.168.1.115";
    public Handler handler = new Handler();
    public TextView serverStatus;
    public ServerSocket serverSocket;
    MyServerMethods myServerMethods = new MyServerMethods();

    @Override
    public void run() {
        try{
          ServerSocket parent = new ServerSocket(); //create a new socket
          parent.setReuseAddress(true);
          parent.bind(new InetSocketAddress(serverPort)); //bind the server port and reuse it if necessary
            if ( serverIp != null){
                Log.i("Status","READY");
                while (true){
                    Socket client = parent.accept(); //accept the incoming connection
                        try{
                            String path = myServerMethods.readRequest(parent, client);
                            Log.i("PATH",""+path);
                            if (path.contains("FitListXml")){
                                myServerMethods.sendXmlFile(client);
                            } else {
                                myServerMethods.sendPhotoFile(client, path);
                            }

                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }
            } else{
                Log.i("Error","Internet connection not present");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我声明按钮的主要活动(我已经为我的问题删除了无用的元素)。

public class AndroidServer2 extends Activity {

private Button closeConnectionButton;
int serverPort = 8080;
Thread fst = new Thread(new MyServer()); //declaration of a new thread

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

@Override
protected void onResume(){
    super.onResume();
    if (fst.isAlive() == false){
        fst.start();
    }
}

@Override
  protected void onPause() {
    super.onPause();
    try {
        fst.interrupt();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }

}

谢谢

4

2 回答 2

1

您可以使用runOnUiThreadActivity 的方法来设置按钮的可见性。

public class MyServer implements Runnable {

public int serverPort = 8080;
public String serverIp = "http://192.168.1.115";
public Handler handler = new Handler();
public TextView serverStatus;
public ServerSocket serverSocket;
MyServerMethods myServerMethods = new MyServerMethods();

private AndroidServer2 mActivity;

MyServer(AndroidServer2 activity) {
    mActivity = activity;
}

@Override
public void run() {
    try{
      ServerSocket parent = new ServerSocket(); //create a new socket
      parent.setReuseAddress(true);
      parent.bind(new InetSocketAddress(serverPort)); //bind the server port and reuse it if necessary
        if ( serverIp != null){
            Log.i("Status","READY");
            while (true){
                Socket client = parent.accept(); //accept the incoming connection

                // Client connected now set the button visibilty
                mActivity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        mActivity.setButtonVisible();
                    }
                });

                try{
                    String path = myServerMethods.readRequest(parent, client);
                    Log.i("PATH",""+path);
                    if (path.contains("FitListXml")){
                        myServerMethods.sendXmlFile(client);
                    } else {
                        myServerMethods.sendPhotoFile(client, path);
                    }

                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        } else{
            Log.i("Error","Internet connection not present");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

AndroidServer2 类:

public class AndroidServer2 extends Activity {

    private Button closeConnectionButton;
    int serverPort = 8080;
    Thread fst = new Thread(new MyServer(this)); //declaration of a new thread

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

    @Override
    protected void onResume(){
        super.onResume();
        if (fst.isAlive() == false){
            fst.start();
        }
    }

    @Override
      protected void onPause() {
        super.onPause();
        try {
            fst.interrupt();
        } catch (Exception e) {
            e.printStackTrace();
        }
      }

    public void setButtonVisible() {
        closeConnectionButton.setVisibility(View.VISIBLE);
    }
}
于 2013-07-17T06:52:56.597 回答
0

您可以将按钮添加到主线程中的布局中,然后您可以像这样更改按钮的可见性:

closeConnectionButton.post(
   new Runnable() {
      closeConnectionButton.setVisibility(View.VISIBLE);
   }
)
于 2013-07-16T15:49:38.947 回答