我创建了一个带有服务器套接字的类,它接受来自客户端的传入连接。现在我需要在连接客户端时显示一个按钮。我该怎么做?我要实现一个事件监听器吗?这是服务器类:
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();
}
}
}
谢谢