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).
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.
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
On ICS, I'm getting an android.os.NetworkOnMainThreadException error when using UrlConnection - even though I'm making this request in a Service that runs on it's own process, and was called asyncronously to be done via Messenger.
Changing the StrictPolicy had no effect, I still get the error.
What can I do?
edit: this Service runs in a separate process - has a different pid and everything.
Services still run on the main thread, as per the the documentation:
Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.
Therefore for long running tasks (such as using a UrlConnection) you should either use something like an IntentService or spawn a new Thread yourself.
Do the network stuff in a Thread there. this exception is thrown to prevent some problems, like the UI freezing (because of the network job, that may take time.)
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
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...