Android / java - 在Runnable类中更改按钮可见性

时间:2013-07-16 15:43:19

标签: java android events event-handling serversocket

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

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

}

由于

2 个答案:

答案 0 :(得分:1)

您可以使用“活动”的runOnUiThread方法设置按钮可见性。

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

答案 1 :(得分:0)

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

closeConnectionButton.post(
   new Runnable() {
      closeConnectionButton.setVisibility(View.VISIBLE);
   }
)