Skip to content Skip to sidebar Skip to footer

Service Vs Thread

What should I use to make an application that will: Ask the user for username and password Authorize Run an infinite loop in which it will fetch some data from the website every 1

Solution 1:

Services and Threads are totally different concepts. A Thread is a separate process that executes in parallel. A Service is a component of an app that doesn't have a UI and runs with a separate life cycle. A service does not run on its own thread, it runs on the UI thread (although it can launch a Thread if it wishes).

You use a Service if you want to do some task but not be bound to the Android Activity lifecycle. You use a Thread if you want to run in parallel. If you want both, then you use a Service that launches a Thread.

From what I'm reading (you don't want the Thread to continue after the Activity is finished), you want a Thread and not a Service.

Solution 2:

A service can run in isolation (while your app is not necessarily running). A thread can be spun off from either your app itself, or a service.

Post a Comment for "Service Vs Thread"