How to start a service in a separate thread?

I need to start the service in a separate thread. This is because it is accessing a web service, which can take 5-10 seconds, and I do not want to receive ANR. I will also be attached to this service. I found a way to start the service in a separate thread with something like:

Thread t = new Thread(new Runnable() { public void run() { //Launch and/or Bind to service here } }); t.start(); 

However, I believe that this only runs the initial code in the new thread, and the service itself works in the main thread. So, how could I run all the code from the service in another thread?

Thanks in advance

+7
source share
1 answer

You can use IntentService

IntentService is the base class for Services that process asynchronous requests (expressed as intentions) for demand. Clients send requests through startService (Intent); the service starts as needed, processes each intention, in turn, using the workflow and stops when it starts out of work

http://developer.android.com/reference/android/app/IntentService.html

+7
source

All Articles