As we know difference between Service and IntentService is that IntentService implicitly spawn worker thread whereas Service run on process main (UI) thread.
That is apparently showing what the difference between them.
But my question is that is there any difference between
Service + android:process "vs" IntentService
I mean to say if we launch Service in a separate process than still any difference between them except Service uses Process whereas IntentService uses Thread.
You are completely misundertand the Concept of Process and Thread.
IntentService run in a diffrent Thread but run in main process.
For Every Application by default there is a only one process,in which all of your code will run including Services,Activitys And IntentService too.
When ever you start your Service in a new process then that Service has allocated with separate memory and heap area.It will not directly communicate with the main process.For communication purpose you need to use AIDL
For more info check this Process and Threads
Related
In a recent answer, I read
A service does not run on its own thread, it runs on the UI thread
I've read similar statements in many other service-related questions.
If I call startService() from an Activity, will the Service run on the Activity's UI Thread or does it spawn a new process with it's own UI Thread?
If it runs on the Activity's UI Thread, doesn't that risk making the Activity unresponsive?
Yes services does run on main thread , About ANR(application not responding ) concern , there will higher chances of ANRs if your service is doing a long processing work using CPU so the official docs also recommends to use Worker Threads for efficiency. docs
You must read the caution paragraph in the docs to confirm
If your task is one time shot then Use IntentService which implicitly use threading mechanism for processing but IntentService will destroy itself automatically when the task is done so you will have to start it again for the next time and so.
android:process : Using this to start a service in an another process will cause usage of more memory. This will cause creating two processes for your app in separate heap memory space which also require other maintenance as well.So this is rarely used and i would not recommend to use this for a regular service task.
If I call startService() from an Activity, will the Service run on
the Activity's UI Thread or does it spawn a new process with it's
own UI Thread?
If it's a Service (not an IntentService), it will run on the main Thread of your app's process, as long as you do not define it as a separate process explicitly (by adding the android:process tag in the Manifest).
If it runs on the Activity's UI Thread, doesn't that risk making the
Activity unresponsive?
Yes it does, if you perform CPU intensive/blocking operations.
To prevent that, you can either start new Thread(s) from your Service, or use an IntentService, which will automatically spawn a new worker Thread for its work.
From the Services Docs it clearly explains your answer.
Caution: A service runs in the 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). This means that, if your
service is going to do any CPU intensive work or blocking operations
(such as MP3 playback or networking), you should create a new thread
within the service to do that work. By using a separate thread, you
will reduce the risk of Application Not Responding (ANR) errors and
the application's main thread can remain dedicated to user interaction
with your activities.
In a recent answer, I read
A service does not run on its own thread, it runs on the UI thread
This is just plain wrong. A Service is an object (an instance of a class). It doesn't run on any thread, it doesn't run at all. It just "is". The methods of the Service may run on any thread, depending...
Specifically, the lifecycle methods of a Service (onCreate(), onStartCommand(), etc.) that are called directly from the Android framework run on the main (UI) thread, which may or may not be the same thread that called startService(). However, a Service can (and usually does) start other background threads, as many as it needs to, to perform the necessary work.
If I call startService() from an Activity, will the Service run on the
Activity's UI Thread or does it spawn a new process with it's own UI
Thread?
See above. If you want your Service to run in a separate process, you can do this by specifying android:process=":service" in the <service> declaration in the manifest. By default, services run in the same process as the other components (Activity, BroadcastReceiver, etc.) of your application.
If it runs on the Activity's UI Thread, doesn't that risk making the
Activity unresponsive?
That depends on what your Service is doing! Obviously if your Service is doing a lot of compute-intensive processing on the UI thread it will make your app unresponsive. If your Service is doing I/O on the UI thread, Android will usually throw exceptions (in more recent versions of Android). This is the reason that services usually start background threads to do work. However, if your Service is lightweight, then there is no reason why methods of your Service cannot run on the UI thread. As I said, it depends...
IntentService runs on the background thread, so, if you want to do one-off tasks then use that.
I have a lot of async tasks in my activity, and I need to override AsyncTask for every call to do it async.
Can I replace all the AsyncTasks with BoundService + AIDL or I need to do it only with AsyncTask?
Service run in background but still run in main thread (AKA. UI thread), you will get ANR exception. according to API here:
Most confusion about the Service class actually revolves around what it is not:
A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
You can use service, but you still need implement your thread logic in either service or activity,if you want something run in a separate thread.
Service is a daemon, AsynkTask is background thread
This question already has answers here:
Service vs IntentService in the Android platform
(11 answers)
Closed 6 years ago.
Can you please help me understand what the difference between an IntentService and a Service is?
Service is a base class of service implementation. Service runs on the application's main thread which may reduce the application performance. Thus, IntentService, which is a direct subclass of Service is available to make things easier.
The IntentService is used to perform a certain task in the background. Once done, the instance of IntentService terminates itself automatically. Examples for its usage would be to download a certain resource from the Internet.
Differences
Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service.
IntentService creates a queue that passes one intent at a time to onHandleIntent(). Thus, implementing a multi-thread should be made by extending Service class directly.
Service class needs a manual stop using stopSelf(). Meanwhile, IntentService automatically stops itself when it finishes execution.
IntentService implements onBind() that returns null. This means that the IntentService can not be bound by default.
IntentService implements onStartCommand() that sends Intent to queue and to onHandleIntent().
In brief, there are only two things to do to use IntentService. Firstly, to implement the constructor. And secondly, to implement onHandleIntent(). For other callback methods, the super is needed to be called so that it can be tracked properly.
In short, a Service is a broader implementation for the developer to set up background operations, while an IntentService is useful for "fire and forget" operations, taking care of background Thread creation and cleanup.
From the docs:
Service
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
IntentService
Service is a base class for IntentService Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
Refer this doc - http://developer.android.com/reference/android/app/IntentService.html
service: It runs in the background on your system. For example,
If you went to a hotel and you give your order for a soup to a server
The server gets your order and sends to chef
You don't know how the soup is made in the kitchen and what processes are required for making the soup
Once your order is ready, the server brings you the soup.
background process: chef making soup
IntentService:- it's consecutive service.. (i.e) when you order many food items at a time to server but the server delivers those items one by one and not deliver them all at once.
See Tejas Lagvankar's post about this subject.
Below are some key differences between Service and IntentService and other components.
Service
Task with no UI,but should not use for long Task. Use Thread within service for long Task
Invoke by onStartService()
Triggered from any Thread
Runs On Main Thread
May block main(UI) thread
IntentService
Long task usually no communication with main thread if communication is needed then it is done by Handler or broadcast
Invoke via Intent
triggered from Main Thread (Intent is received on main Thread and worker thread is spawned)
Runs on separate thread
We can't run task in parallel and multiple intents are Queued on the same worker thread.
Service runs actually in the same thread of your app; when you extends Service, you must manually spawn new threads to run CPU blocking operations.
vs
IntentService is a subclass of Service which spawns a thread to do background work from there(No need to create a new thread to do CPU blocking operations).
Service: Works in the main thread so it will cause an ANR (Android Not Responding) after a few seconds.
IntentService: Service with another background thread working separately to do something without interacting with the main thread.
Intent service is child of Service
IntentService: If you want to download a bunch of images at the start of opening your app. It's a one-time process and can clean itself up once everything is downloaded.
Service: A Service which will constantly be used to communicate between your app and back-end with web API calls. Even if it is finished with its current task, you still want it to be around a few minutes later, for more communication
Is a "Service" in android same as a separate thread ?
From Service javadoc:
Note that services, like other
application objects, run in the main
thread of their hosting process.
So the answer is No.
A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
Is an Android "Service" considered a process or a thread according to operating system?
It is neither, any more than an activity is "a process or a thread".
All components of an Android application run inside a process and by default utilize one main application thread. You can create your own threads as needed.
A Service can be implemented as a separate process depending on the requirement.
Here is a good explanation when to implement the service as a separate process or as a component inside an existing app (Service with Activities in same process).
Service is not a process nor a thread. Its a part of the process and a main thread. Like all other components - service runs in the main thread.. Lets say - your application has an Activity and a Service then your Application is a unix process which has a process id (pid) and runs in a instance of DVM... Process spawns a first thread which is main thread.. All your application components runs in main thread by default - you can call separate thread to do parallel jobs.
Well, processes host services in the Android OS, so it's not a process. Maybe have a look at the definition of a Service for more details...