2014-07-16 48 views
2

你好。我有一個android應用程序使用TCP連接連接到一個C++服務器。 我的第一個活動是讓用戶寫出ip地址和端口與服務器連接,然後這個活動使用一個意圖調用另一個活動。 第二個活動執行套接字連接並運行一些線程。如何在關閉活動時停止活動線程和套接字

第二個活動還有一個斷開按鈕;當你按下該按鈕時,它應該停止所有正在運行的線程,關閉套接字連接並返回活動一,並讓用戶在需要時再次連接。我一直無法這樣做。 我嘗試過socket.close(),但後來我的線程崩潰,不允許重新連接。我也試過this.onDestroy();this.finish();活動關閉但仍連接到服務器。

完成活動並返回到上一個活動時,我該如何完成所有線程和套接字?

爲了使它更清楚這裏是我的代碼:

第一項活動

public class FirstActivity extends Activity { 
EditText editTextAddress, editTextPort; 
Button buttonConnect, buttonClear; 
Intent intent; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_ipclient); 

     editTextAddress = (EditText)findViewById(R.id.address); 
     editTextPort = (EditText)findViewById(R.id.port); 
     buttonConnect = (Button)findViewById(R.id.connect); 
} 
public void onClickConnect(View v){ 
    String ip=editTextAddress.getText().toString(); 
    int port=Integer.valueOf(editTextPort.getText().toString()); 
    intent=new Intent(this,SecondActivity.class); 
    Bundle extrasB=new Bundle(); 
    extrasB.putString("ip",ip); 
    extrasB.putInt("port",port); 
    intent.putExtras(extrasB); 
    startActivity(intent); 
} 
} 

SecondActivity

public class SecondActivity extends Activity { 
.... 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_client_late); 
    ..... 
    updateConversationHandler = new Handler(); 
    new Thread(new ClientThread()).start(); 
    sendImage=new SendImage(); 
    sendImage.execute(); 
} 
    public class SendImage extends AsyncTask<Void,Void,Void>{ 
    @Override 
    protected Void doInBackground(Void... arg0){ 
     ..... 
    } 
} 
class ClientThread implements Runnable { 

    @Override 
    public void run() { 

     thread1=Thread.currentThread(); 
     try { 
      InetAddress serverAddr = InetAddress.getByName(SERVER_IP); 
      socket = new Socket(serverAddr, SERVERPORT); 

      socketTE = new Socket(serverAddr, SERVERPORT); 
      CommunicationThread commThread = new CommunicationThread(socketTE); 
      new Thread(commThread).start(); 
     } catch (UnknownHostException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
    } 

} 

class CommunicationThread implements Runnable 
{ 

    private Socket clientSocket; 
    private BufferedReader input; 
    public CommunicationThread(Socket clientSocket) 
    { 
     thread2=Thread.currentThread(); 
     this.clientSocket = clientSocket; 
     .... 

    } 
    public void run() 
    { 
     while (!Thread.currentThread().isInterrupted()) 
     { 
      .....     
     } 
    } 
} 

class updateUIThread implements Runnable 
{ 
    private String msg; 
    public updateUIThread(String str) 
    { 
     thread3=Thread.currentThread(); 
     ... 
    } 
    @Override 
    public void run() 
    {      
      .... 
    } 
} 

**public void disconnectButtonOnCLick(View v) throws IOException{ 
    super.onStop(); 
    super.finish(); 
}** 
@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    sendImage.cancel(true); 
    thread1.interrupt();   
    thread2.interrupt(); 
    thread3.interrupt(); 
    try { 
     socket.close(); 
     socketTE.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

} 

回答

1

的異步任務:

@Override 
    public void onPause(){ 
     super.onPause(); 
     if (sendImage!=null) { 
      sendImage.cancel(true); 
      } 
} 
+0

我應該在哪裏調用onPause方法? – Dany19

+0

你應該不要明確地調用onPause。它會自動調用。請看看:http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle – Mike

+0

邁克感謝您的編輯.. – erik

1

那麼,首先你不應該明確地調用this.onDestroy()。你應該調用finish()來關閉這個活動。 onDestroy()是一個回調,當acvitity被破壞時被調用。所以你應該像onCreate()一樣覆蓋onDestroy:

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    // close all your threads 
} 

其次。關閉所有線程你可以看看這個線程 - android best and safe way to stop thread。您還可以使用布爾變量來指示線程是否停止:public static boolean isStopped;就像回答上面Malcolm鏈接。 以及如何關閉插座 - How can a socket be both connected and closed?。 希望這有助於。