Android: background thread

Command

I need to start a background thread in my application. Could you share the best practices in which to initiate a stream so that it continues to work regardless of what activity is displayed and what needs to be considered. The purpose of this background thread is to start transactions from the Simulator to the server and receive a response from the host.

thank you Ramesh

+4
source share
2 answers

I solved this by having a special application object containing a link to a background thread (AsyncTask in my case). The application object remains alive as long as your process runs. In other words, he will not be killed if your activity dies, for example, if you change the orientation of the phone. Activity set the current activity in AsyncTask at creation and disabled activity in AsyncTask when activity disappeared. AsyncTask will clear its own link from the application object after it is completed. I wrote about it here .

+4
source

It looks like you want a service. Check out the Services section of the Application Basics page.

+3
source

Source: https://habr.com/ru/post/1310454/


All Articles