2011-02-01 96 views
1

我有以下主要類和線程TCP客戶端。客戶端運行在一個循環中,接收消息並將其傳遞給主類。在主類中,我解析消息並嘗試根據收到的消息的名稱和值顯示不同的圖像。 例:shiftDirection1 名:shiftDirection &值:1Android:不能顯示多個圖像

但是我可以顯示對應於所述第一接收消息只有圖像和不能顯示對應於剩餘接收到的消息的圖像。

請仔細閱讀下面的代碼,並提出錯誤/問題和其他方法。

謝謝你的時間和精力。

馬杜

主類:

public class TCPListen extends Activity implements TCPListener { 
    private TextView mTitle; 
    public String recData[] = new String[2]; 
    String PresentGear = "0"; 

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

      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(); 
         recData[0] = msgName; 
         recData[1] = tmpVal; 
         if (recData[0].equals("currGear")) PresentGear = recData[1]; 
         Log.d("TCP", "Inside Std parser " + recData[0] + " " + recData[1]); 
         actionOnData(recData[0], recData[1]); 
         } 
        catch(Exception e){ 
        e.printStackTrace(); 
       }     
       Log.d("TCP", "Just outside Std parser " + recData[0] + " " + recData[1]); 
       return recData; 
       //} 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; 
      setContentView(R.layout.image); 
      ImageView showImage = (ImageView) findViewById(R.id.imageView1); 
      //Log.d("TCP", "------------>" + tempName + " " + tempVal);    

      if (tempName.equals("shiftDirection") && tempVal.equals("1")) { 
       //setContentView(R.layout.image); 
       //TextView text_top = (TextView) findViewById(R.id.textView1); 
       //showImage = (ImageView) findViewById(R.id.imageView1);    
       //text_bottom.setText(Info[1]); 
       showImage.setImageResource(R.drawable.shift_up); 
       Log.d("TCP", "1------------>" + showImage); 
      } else if (tempName.equals("shiftDirection") && tempVal.equals("-1")) {     
       //setContentView(R.layout.image); 
       //TextView text_bottom = (TextView) findViewById(R.id.textView2); 
       //Resources res = getResources(); 
       //Drawable drawable = res.getDrawable(R.drawable.shift_down); 

       //showImage = (ImageView) findViewById(R.id.imageView1);    
       //text_bottom.setText(Info[1]); 
       showImage.setImageResource(R.drawable.shift_down);    
      } else if (tempName.equals("recomGear") && tempVal != null) { 
       Log.d("TCP", "3------------>" + tempName + " " + tempVal); 
       Integer msgValue = Integer.parseInt(recData[1]); 
       //Integer CurrentGear = (msgValue) - 1; 
       //Log.d("TCP","in DA Images. Current gear: " + CurrentGear); 
       //String Gear = Integer.toString(CurrentGear); 
       setContentView(R.layout.image); 
       TextView text_top = (TextView) findViewById(R.id.textView1); 
       TextView text_bottom = (TextView) findViewById(R.id.textView2); 
       showImage = (ImageView) findViewById(R.id.imageView1);   
       showImage.setImageResource(R.drawable.shift_up); 
       text_bottom.setText(PresentGear); 
       text_top.setText(tempVal); 
      } else if (tempName.equals("currGear") && tempVal != null) { 
       Log.d("TCP", "4------------>" + tempName + " " + tempVal); 
       PresentGear = tempVal;   
       //Log.d("TCP","in DA Images. Present gear1: " + PresentGear); 
       setContentView(R.layout.image); 
       TextView text_bottom = (TextView) findViewById(R.id.textView2); 
       text_bottom.setText(PresentGear);      
      } else if (tempName.equals("shiftDirection") && tempVal.equals("0")) { 
       Log.d("TCP", "5------------>" + tempName + " " + tempVal); 
       Log.d("TCP","in DA Images. Present gear: " + PresentGear); 
       setContentView(R.layout.image); 
       TextView text_bottom = (TextView) findViewById(R.id.textView2); 
       //TextView text_top = (TextView) findViewById(R.id.textView1); 
       //text_top.setText("Go on"); 
       text_bottom.setText(PresentGear);   
      }   
      } 
} 

只有當被顯示的情況下對應於所述第一圖像。程序控制進入第二個循環,但沒有在那裏顯示圖像。

接口:

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

線程(TCP客戶端):

public class TcpServiceHandler implements Runnable { 
    TCPListener _listener;    
    private Activity _act; 
    public BufferedReader in; 
    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.62.23", 1200, true); 
     // 
       //while(true){ 
        try {       
         in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
         final int delay = 100; 
         final Timer _timer = new Timer(); 
         _timer.scheduleAtFixedRate(new TimerTask() {      
          public void run(){ 
           String str; 
           try { 
            str = in.readLine(); 
            _listener.callCompleted(str); 
           } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
           } 

          } 
         }, 0, delay); 
         //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

您可能會檢查兩兩件事:

  1. TcpServiceHandler是一個線程的Runnable但在run()沒有循環。你在這個方法中定義的_timer可能在它完成工作之前就已經死了並離開了。

  2. 您是否正在從後臺線程更改UI?一般來說,這不是一個好主意。

  3. 檢查AsyncTask這是一個在後臺運行操作的方便工具。

+0

TcpServiceHandler中的計時器工作正常。實際上,所有傳入的消息也傳遞給主類。但圖像不顯示。我認爲我不會從後臺線程更改UI。我早些時候嘗試過異步任務,但是這對我來說很複雜。 – 2011-02-03 09:11:49