Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android/java-在可运行类中更改按钮可见性_Java_Android_Events_Event Handling_Serversocket - Fatal编程技术网

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

Android/java-在可运行类中更改按钮可见性,java,android,events,event-handling,serversocket,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 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();
        }
    }
}
这是我声明按钮的主要活动(我删除了问题中无用的元素)


谢谢

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

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

您可以使用活动的
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);
    }
}

你是说在我的活动中?这是主要的:公共类AndroidServer2扩展了活动。但在这种情况下,我如何显示传入连接何时被接受?为此,我使用runnable类。感谢您可以从任何线程调用post方法,即使是在另一个可运行类中。感谢您的帮助。我的类不是AndroidServer2的内部。在这种情况下,我如何传递引用?我必须扩展我的类吗?是否还有一种方法可以在MyServer类中创建事件?我添加了一个示例,说明如何传递引用。这对我也很有用,谢谢。要在片段中使用它,只需添加getActivity().runOnUiThread(newrunnable(){…),其余的都一样
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);
    }
}