服务器套接字接受连接时显示警报对话框

时间:2013-06-21 11:05:07

标签: java android sockets android-dialog

我需要创建一个实现服务器套接字的类,该套接字等待来自客户端的连接请求(在这种情况下是Web浏览器)。之后我接受了连接,我想向用户显示一个带有两个按钮的警告对话框。所以,目前,我已准备好用于服务器套接字的类,以及实现Alert Dialog的方法。如何使用服务器套接字类中的警告对话框方法显示带有两个按钮的消息?这是服务器类:

更新:我已经添加了用于创建警报对话框的代码,之后服务器已接受连接

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

由于

2 个答案:

答案 0 :(得分:0)

public void showDialog(Activity activity, String title, CharSequence message)
{
 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
 if (title != null)
 builder.setTitle(title);

 builder.setMessage(message);
 builder.setPositiveButton("OK", null);
 builder.setNegativeButton("Cancel", null);
 builder.show();

}

答案 1 :(得分:0)

handler.post(new Runnable() {
            @Override
            public void run() {
                // create the dialog here
            }
});

如果已经在ui线程上创建了处理程序,这将是有效的,我猜是这样的,因为你正在使用它来设置文本。