我需要创建一个类来实现等待来自客户端(在本例中为 Web 浏览器)的连接请求的服务器套接字。之后我接受了连接,我想向用户显示一个带有两个按钮的警报对话框。所以,目前,我已经准备好用于服务器套接字的类,以及实现警报对话框的方法。如何使用服务器套接字类中的警报对话框方法来显示带有两个按钮的消息?这是服务器类:
更新:在服务器接受连接后,我添加了用于创建警报对话框的代码
public class ServerThread extends AndroidApp2 implements Runnable {
public void run() {
try{
if ( SERVERIP != null){
handler.post(new Runnable(){
@Override
public void run(){
serverStatus.setText("Listening on IP: " + ip_address_conversion + ":" + SERVERPORT);
}
});
serverSocket = new ServerSocket(SERVERPORT);
while (true){
Socket client = serverSocket.accept();
handler.post(new Runnable(){
@Override
public void run(){
serverStatus.setText("Connected");
}
});
try{
AlertDialog.Builder connection_alert3 = new AlertDialog.Builder(this);
connection_alert3.setTitle("Connection Incoming");
connection_alert3.setMessage("bla bla boa");
connection_alert3.setCancelable(false);
connection_alert3.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
}
});
connection_alert3.setNegativeButton("No", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id){
}
});
AlertDialog alert = connection_alert3.create();
alert.show();
} catch (Exception e){
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
}
});
e.printStackTrace();
}
}
}else{
handler.post(new Runnable(){
@Override
public void run(){
serverStatus.setText("Couldn't detect internet connection."); }
});
}
}catch (Exception e) {
handler.post(new Runnable(){
@Override
public void run(){
serverStatus.setText("Error");
}
});
e.printStackTrace();
}
}
}
谢谢