你好。我有一個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();
}
}
}
我應該在哪裏調用onPause方法? – Dany19
你應該不要明確地調用onPause。它會自動調用。請看看:http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle – Mike
邁克感謝您的編輯.. – erik