Reputation: 926
I have created Intent service.In this service i want to take data from server so i am using AsyncTask in Intent service but it is not working because IntentService runs in another thread (worker thread) not in main thread while OnPreExecute() and onPostExecute() run in main thread.
Then I extend Service class and write AsyncTask in it.It is working fine because Service runs in main thread so that No error found when i use onPreExecute() and onPostExcute().
When i use service and Intent service?In my case what is suitable Intent service or service.
Upvotes: 2
Views: 237
Reputation: 38168
As you said, a Service will run on the UI Thread. Thus, if you want to deport some background processing (like a network request), you could use a service and manage your own thread to achieve the background job.
No need for an AsyncTask here, just use a normal thread.
But using an IntentService will be much easier as, as you said again, they have their own thread to do background job. Again, do not use AsyncTask here, just perform your query inside your intent service, that's it. The intent service should terminate only when your request is processed. Nothing else to do.
Some libraries will help you to achieve this, like RoboSpice.
Upvotes: 1