我創建了一個具有服務器套接字的類,該套接字接受來自客戶端的傳入連接。現在我需要在客戶端連接時顯示一個按鈕。我該怎麼做?我有沒有實現一個事件監聽器? 這是服務器類:Android/java - 在Runnable類中更改按鈕的可見性
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();
}
}
}
感謝
感謝您的幫助。我的課不是AndroidServer2的內部。我如何在這種情況下通過參考?我有延長我的課程嗎? – Hieicker
還有一種方法可以在MyServer類中創建一個事件嗎? – Hieicker
我已經添加了一個如何傳遞參考的例子。 –