你好。我有一个使用 TCP 连接连接到 c++ 服务器的 android 应用程序。 我的第一个 Activity 让用户写入 ip 地址和端口以连接服务器,然后此 Activity 使用 Intent 调用另一个 Activity 。 第二个 Activity 进行套接字连接并运行几个线程。
第二个 Activity 也有一个断开按钮;当您按下该按钮时,它应该停止所有正在运行的线程,关闭套接字连接并返回 Activity 一,并在需要时让用户再次连接。我没能这样做。
我试过 socket.close()
但后来我的线程崩溃了,不允许重新连接。我也尝试过 this.onDestroy();
或 this.finish()
; Activity 关闭但它仍然连接到服务器。
如何在完成一个 Activity 时完成所有线程和套接字并返回到上一个 Activity ?
为了更清楚这里是我的代码:
第一个 Activity :
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);
}
}
第二个 Activity
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();
}
}
}
最佳答案
对于异步任务:
@Override
public void onPause(){
super.onPause();
if (sendImage!=null) {
sendImage.cancel(true);
}
}
关于android - 关闭 Activity 时如何停止 Activity 的线程和套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24785969/