You already have the answers to your question, but I think this is worth the summary ...
What you need
If you want to run a piece of code that takes some time, you should always run it in a separate thread from the user interface thread.
There are two ways to do this:
Using Thread :
This is the easiest if you donβt need a lot of communication from the new thread to the user interface thread. If you need a message, you may have to use the Handler to do this.
Using AsyncTask :
It also works in a separate stream and already implements some communication channels with the user interface stream. Therefore, this option is preferable if you need this connection back to the user interface.
What you don't need
Service
This serves mainly to ensure that some code works even after exiting the main application, and it will work in the user interface thread if you do not create a new thread using the parameters described above. You said that your thread will be disconnected when you exit the application, so this is not what you need.
IntentService
This can be triggered by an external event (i.e. BroadcastReceiver ), which can run part of the code you define, even if your application is not running. Once again, based on your requirements, this is not what you are looking for.
Sincerely.
source share