2013-07-16 74 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方法,即使在另一個可運行的類中也是如此。 –