Search code examples
javaandroideventsevent-handlingserversocket

Android/java - Change button visibility in a Runnable class


I have created a class with a server socket that accept incoming connection from clients. Now i need to show a button when a client is connected. How can i do it? Have i to implement an event listener? This is the server class:

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

This is the main activity where i have declared the button ( i have removed the useless elements for my question).

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

}

Thanks


Solution

  • You can use the runOnUiThread method of your Activity to set the button visibility.

    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 class:

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