2013-07-16 49 views
0

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

} 

感谢

回答

1

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

感谢您的帮助。我的课不是AndroidServer2的内部。我如何在这种情况下通过参考?我有延长我的课程吗? – Hieicker

+0

还有一种方法可以在MyServer类中创建一个事件吗? – Hieicker

+0

我已经添加了一个如何传递参考的例子。 –

0

您可以在主线程将按钮添加到您的布局,然后你可以改变按钮的知名度是这样的:

closeConnectionButton.post(
    new Runnable() { 
     closeConnectionButton.setVisibility(View.VISIBLE); 
    } 
) 
+0

你在我的活动是什么意思?这是主要的:public class AndroidServer2 extends Activity。但在这种情况下,我如何才能透露传入连接已被接受?我为此使用可运行类。谢谢 – Hieicker

+0

您可以从任何线程调用post方法,即使在另一个可运行的类中也是如此。 –