What is the difference between Service, Async Task & Thread?
A Service is an application component that can perform long-running operations in the background and does not provide a user interface.
Services can run the even if the activity that triggers it, is no longer active.
When to use?
For the task which does not require UI.
Beware of ANR as Services run on main Thread of its hosting process. The service does not create its own thread and does not run in a separate process (unless you specify otherwise)
Use threads within service for long tasks.
Threads run parallel to the main thread.
Thread can’t update a UI component from the main thread.
To update the UI you need to use Handler. Handler is itself a thread but it can handle UI.
Updating UI thread from other thread is a common task in Android. So Android provides an API(Async task) for this task as to ease our task.
There is no need of handler.
When we have Thread, then why we need service? What is that,a Service can do but a Thread can’t?
My rule of thumb is that an
AsyncTask is for when I want to do something tied to single
Activity and a
Service is for when I want to do something that will carry on after the Activity which started it is in the background.
1) A service of your application is usable not only by other components of your application, but by other applications, too.
2) The main benefit of a Service is access to a Context object which has an independent life cycle. This allows for reuse of common code by many activities and, in the case of public or exposed services, many applications.
3) Service – if it is destroyed while performing its job, in the middle by Android due to low memory scenario. Then Android will make sure that it will restart your service, if you have returned START_STICKY or START_REDELIVER_INTENT from onStartCommand().
Thread – if it is destroyed by Android in the middle due to low memory, then Android will not guarantee to restart it again. That means the user lost his half work.
4) Service – is a component of Android, so it has priority levels to be considered while destroying an application due to low memory.
Thread – is not a component of Android( is a O.S level feature ), so Android will not take thread priority into consideration while killing an application due to low memory.
So bottom line is: If you want to do some heavy background functionality then it is always better to have a service with thread. If you feel that that background functionality to be alive as long as your activity is alive, then go for activity with thread or activity with async task
Looper is a class that turns a thread into a Pipeline Thread and Handler gives you a mechanism to push tasks into it from any other threads.
So a PipeLine Thread is a thread which can accept more tasks from other threads through a Handler.
The Looper is named so because it implements the loop – takes the next task, executes it, then takes the next one and so on. The Handler is called a handler because it is used to handle or accept that next task each time from any other thread and pass to Looper (Thread or PipeLine Thread).
A Looper and Handler or PipeLine Thread’s very perfect example is to download more than one images or upload them to a server (Http) one by one in a single thread instead of starting a new Thread for each network call in the background
A thread can have any number of handler, but only one looper.
Make a Thread into looper thread:- https://www.programering.com/a/MDN4gjNwATQ.html