2011-01-25 13 views
0

我有一個運行在Android手機中的TCP客戶端。它連接和接收來自Windows應用程序的數據。一旦我收到消息,我就解析它,然後嘗試顯示圖像。我必須繼續檢查這個Windows應用程序的新消息。Android手機中的TCP客戶端:連續運行,基於從服務器收到的消息顯示圖像

客戶端線程繼續運行。收到消息時,我將它傳遞給主要活動,解析它,但我無法顯示圖像。我想讓客戶端每隔100毫秒檢查一次新消息。目前,隨着線程繼續運行,LogCat被淹沒,我無法真正看到LogCat的內容。

基本上我想運行客戶端,每隔100毫秒檢查一次新消息,當有新消息時,將它傳遞給主要活動,解析它並顯示圖像。請仔細閱讀下面的代碼,並在必要時提出更正或更好的方法。

客戶端代碼如下。

主要活動:

public class TCPListen extends Activity implements TCPListener { 
private TextView mTitle; 
public String data[] = new String[2]; 

    /** Called when the activity is first created. */ 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      //setContentView(R.layout.main); 

     // Set up the window layout 
      requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
      setContentView(R.layout.main); 
      getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); 

      // Set up the custom title 
      mTitle = (TextView) findViewById(R.id.title_left_text); 
      mTitle.setText(R.string.app_name); 
      mTitle = (TextView) findViewById(R.id.title_right_text); 

      //TcpServiceHandler handler=new TcpServiceHandler(this); 
      //handler.execute("192.168.62.23"); 

      TcpServiceHandler handler = new TcpServiceHandler(this,this); 
      Thread th = new Thread(handler); 
      th.start(); 
     } 

     public String[] callCompleted(String source){ 
      Log.d("TCP", "Std parser " + source); 
      mTitle.setText(source); 
      //String data[] = new String[2]; 

      //if (source.matches("<MSG><N>.*</N><V>.*</V></MSG>")) { 
       Document doc = null; 
       try{ 
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder db = dbf.newDocumentBuilder(); 
       doc = (Document) db.parse(new ByteArrayInputStream(source.getBytes())); 
       NodeList n = doc.getElementsByTagName("N"); 
       Node nd = n.item(0); 
       String msgName = nd.getFirstChild().getNodeValue(); 
       NodeList n1 = doc.getElementsByTagName("V"); 
       Node nd1 = n1.item(0); 
       String tmpVal = nd1.getFirstChild().getNodeValue(); 
       data[0] = msgName; 
       data[1] = tmpVal; 
       Log.d("TCP", "Inside Std parser " + data[0] + " " + data[1]); 
       actionOnData(data[0], data[1]); 
       } 
       catch(Exception e){ 
       e.printStackTrace(); 
      } 
      Log.d("TCP", "Just outside Std parser " + data[0] + " " + data[1]); 
      return data; 
      //} else Log.d("TCP", "Message in wrong format " + source); 
      //mTitle.setText("Message in wrong format " + source); 
      //return data; 
     } 


    //Function to display driver messages/images based on individual messages 
     public void actionOnData(String name, String value) { 
     String tempName = name; 
     String tempVal = value; 
     //while (true) { 
     if(tempName.equals("shiftDirection") && tempVal.equals("1")) { 
     Log.d("TCP","in actionOnData " + data[0] + " " + data[1]); 
     mTitle.setText("Change to next higher gear"); 
       Intent myIntent = new Intent(); 
       myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images"); 
       //myIntent.putExtra("Change gear", "Shift to next gear!"); // key/value pair, where key needs current package prefix. 
       startActivity(myIntent); 
       try { 
       wait(3000); 
       } catch(InterruptedException e) { 
       System.out.println("InterruptedException caught"); 
       } 
      } else if(tempName.equals("vehicleSpeed") && tempVal.equals("120")) { 
     Log.d("TCP","in actionOnData " + data[0] + " " + data[1]); 
     mTitle.setText("Drive like a man"); 
       //Intent myIntent = new Intent(); 
       //myIntent.setClassName("com.example.android.TCPListen", "com.example.android.TCPListen.Images"); 
       ////myIntent.putExtra("Change gear", "Shift to next gear!"); // key/value pair, where key needs current package prefix. 
       //startActivity(myIntent); 
      } else Log.d("TCP", "Just show an image"); 

     //} 

     } 
} 

接口:

public interface TCPListener { 
public String[] callCompleted(String msg); 
} 

主題:

public class TcpServiceHandler implements Runnable { 
    TCPListener _listener; 
    private Activity _act; 
    public TcpServiceHandler(TCPListener listener, Activity act){ 
     _listener = listener; 
     _act = act; 
    } 

    public synchronized void run() { 
     // TODO Auto-generated method stub 
     //if(socket==null){ 
      try { 
       //InetAddress serverAddr = InetAddress.getByName("192.168.178.25"); 
       Socket socket = new Socket("192.168.2.103", 1200, true); 
     // 
       while(true){ 
        try { 
         BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
         final String str = in.readLine(); 
         this._act.runOnUiThread(new Runnable(){ 

         public void run() { 
          _listener.callCompleted(str); 
          } 
         }); 
        } 
        catch(Exception e){ 
         e.printStackTrace(); 
        } 
       } 
      } catch (UnknownHostException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 
} 

回答

1

既然你問了建議,爲更好的方法,你可以考慮有一個服務是TCP監聽器而不是活動,並且當檢測到消息時它可以打開顯示的活動你想要的圖像。這更復雜,但如果我的理解正確,它似乎更符合傳統上使用的活動和服務。我想這取決於諸如消息之間等待多長時間以及用戶在等待時發生了什麼。

相關問題