2012-11-26 69 views
0

我正在我的Galaxy S3上調試我的應用程序,並且每當我監視處理時。我注意到負載很高的應用程序處理。應用處理進入有時80%,這是令人難以置信的HIGHTAndroid Socket Thread在處理器處添加非常高的負載

下面是代碼:

package com.example.socketclient; 



import android.app.Activity; 
import android.os.Bundle; 

import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import android.util.Log; 

public class SocketCode extends Activity { 
    private boolean connected = false; 
    //private Handler handler = new Handler(); 
    public TextView txt; 
    int doit=0; 
    protected SocketCore Conn; 
    public Button b; 
    public EditText TextToSend; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_socket_code); 
     b = (Button)findViewById(R.id.button1); 
     txt = (TextView)findViewById(R.id.textView1); 
     TextToSend = (EditText)findViewById(R.id.editText1); 
     //Conn = new SocketCore(this,txt); 
     b.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Log.e("ErrorButton","Button Pressed Before Toggle"+doit); 
      doit=1; 
      Log.e("ErrorButton","Button Pressed "+doit); 
      } 
     }); 

     Thread cThread = new Thread(new ClientThread()); 
     cThread.start(); 

    } 
    public class ClientThread implements Runnable { 
     Socket socket ; 
String finall="",text; 
PrintWriter out = null; 
BufferedReader in = null; 

     public void run() { 
      try { 
       InetAddress serverAddr = InetAddress.getByName("192.168.0.150"); 
       Log.d("ClientActivity", "C: Connecting..."); 
       socket= new Socket(serverAddr,4444); 
       connected = true; 

       while (connected) { 
        try { 



         if(doit==1) 
         { 
          Log.e("ErrorButton","If"); 

          out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); 
         in = new BufferedReader(new InputStreamReader(socket.getInputStream()));     

         out.println(TextToSend.getText().toString()); 
         finall=""; 
         while ((text = in.readLine()) != null) { 
          finall += text; 
          Log.e("Test","Final: "+finall); 
          if(text=="quit") 
          { 
           socket.close(); 
          } 
         Log.e("ClientActivity", "After Read "+doit+" "+finall); 

         break; 
         } 

         doit=0; 

         Log.e("ClientActivity", "Out Of IF "+doit); 
        runOnUiThread(new Runnable() { 
         public void run() { 
          txt.setText(finall); 
         } 
        }); 

         }  



        } catch (Exception e) { 
         Log.e("ClientActivity", "S: Error", e); 
        } 

       } 
       socket.close(); 
       txt.setText("Closed Socket"); 
       Log.d("ClientActivity", "C: Closed."); 
      } catch (Exception e) { 
       Log.e("ClientActivity", "C: Error", e); 
       connected = false; 
      } 
     } 

     public void ClientHandler(String Send) 
     { 
      try{ 
       PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket 
         .getOutputStream())), true); 
      out.println(Send); 
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      finall = in.readLine(); 

      txt.setText(finall); 
      } 
      catch(IOException e) 
      {txt.setText("Exception");} 
     } 


    } 


} 

回答

1

的CPU是這樣做的,因爲你正在運行不停的循環,保持飼養可運行到UI線程,所以CPU保持非常高的負載。相反,我建議您考慮使用推送策略而不是像GCM(Google Cloud Messaging)這樣的投票策略,當您的應用將新數據推送到設備時,您的應用會被喚醒。如果這不是一個可行的解決方案,我會將輪詢速率限制在較低的速率,並將其限制爲每分鐘(或更少)幾次,在run()中定期使用Thread.sleep()以避免UI線程氾濫可運行。

+0

非常感謝。使用Thread.sleep修復 –

0

固定 使用Thread.Sleep(200);

while (connected) { 
        try { 
         Thread.sleep(200); 
         Log.e("ErrorButton","If"); 
         if(doit==1) 
         {