gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:20:08 25 4
gpt4 key购买 nike

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

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

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

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

}

谢谢

最佳答案

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

关于java - Android/java - 更改 Runnable 类中的按钮可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17681088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com