AlexKulr
AlexKulr

Reputation: 69

Create a service inside Thread or Thread inside Service

What is recommended to do for networking job that should fetch and update new data? I see different answers on that issue.

To create a Service that will run inside a new Thread that should make all the network job OR to create a new Thread that will start a Service inside that Thread?

Upvotes: 1

Views: 1243

Answers (5)

Ishan Khanduja
Ishan Khanduja

Reputation: 141

It is always better to call thread inside service rather than service inside thread because service is a component of android so it is having some priority level. On the other hand Thread is not a part of Android component so it is having no priority compared to service.

So in case of low memory when Android system starts killing application, it will first kill thread that contains the service because of its lower or no priority. And if you have used Service containing thread then you are good. Hope it helps :)

Upvotes: 0

Ranjit
Ranjit

Reputation: 5150

I think you need IntentService. No need to confuse on Service inside Thread. Start Service inside background thread or start a service with background thread .Both are same.

Upvotes: 0

iamreptar
iamreptar

Reputation: 1451

IntentService initializes on the main thread in onCreate and runs in a separate thread otherwise. No need to deal with extra threads in this case. A Service runs in the main thread and requires that you handle the threading (if needed). It would be best to prototype an IntentService since you're just making a network call. If the network call is short, then just use an AsyncTask.

Upvotes: 0

Praveen
Praveen

Reputation: 1

In android all The network operations are to be done within a thread. Even if you are creating a service all network operations will be done within a thread. Service within a thread is not possible.

Now if You want your network operations to be available in multiple activities you should create a service otherwise A separate network thread within an activity will do the job.

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93708

You can't make a Service that runs inside a Thread, so that possibility is impossible. You need a Thread (or AsyncTask) to do any network request. If you want the app to continue to do network requests even if the current Activity is finished, then yes you need to create that Thread (or AsyncTask) in a Service.

Upvotes: 3

Related Questions