I have an App that has a thread handler receiving some socket data all the time.
I want this thread to be closed along with the termination of socket whenever the Apps is not on foreground. I’m assuming this can be done on Android without issues.
How can this be done?
I would also like the socket to be reopened and the reception of data to begin once the UI comes to the foreground(or the Apps comes to foreground). At the moment, my socket opening calls are all done in such a way that during “onCreate” the thread handler is started which takes care of socket and getting data from it.
Check the life cycle of the Activity class. When an Activity comes to foreground onResume() is called when it gets into the background onPause() is called. So you can add the logic for opening an closing the socket in these both methods.