My application has some actions that in the first connect a socket to communicate with the server in other actions. This socket is running in a worker thread.
My question is: where can I close this socket when the application ends? For example, using the BACK button ...
I thought to close the socket in onDestroy()
last action, but this action can be destroyed by the system at runtime and close the socket even if the application does not end. I do not want it.
My run()
method of the thread handling the socket connection looks like:
public void run() { if (this.bliveclient.isConnected()){ try { //... while (running) { //waiting for input data and do something... } } catch (IOException ex) { //handle exception } finally{ try { mySocket.close(); } catch (IOException ex) { //handle exception } } }
But the finally
block is never called.
Can someone tell me?
source share