478 CHAPTER 13: Android Service Class and Threads: Background Processing
A Service subclass, as a default, always runs inside the primary (UI) thread of the host application’s
primary process. Services that run inside of this primary process of your application are often termed
Local Services. You will be reading about processes and threads in the next section of the chapter.
A misconception among Android programmers is that Android Service subclasses always run
on their own separate threads. This is certainly possible, if you configure your Service subclass
that way. The Service does not in fact by default create its own thread, and thus does not run in a
separate thread unless you specify otherwise. You will read about processes and threads in the next
section of this chapter, as these are very closely related topics.
What this means is that if your Service is going to do any CPU-intensive work (such as decoding
streaming data in real-time) or blocking operations (such as real-time network access over busy
network protocols), you should create a new thread within your Service in order to do that type of
processing.
It is important to note that you may not need to use another thread for your Service class apart from
the thread it is on (using already); for instance, in the example in this chapter we play a music file
using the MediaPlayer in a Service without needing to spawn another thread.
The only way to really determine if this is needed is to first try using a Service class for your
background processing, and then, if it affects your user experience, consider implementing the
Android Thread class and a Thread object.
Processes and Thread: Foundational Information
When one of your Android application’s components, such as your MainActivity class, starts and
your application does not have any other components currently running, the Android operating
system will launch a new Linux process for your application, using a single thread of execution called
the UI Thread.
A process can generate or launch (I use the popular industry term “spawn”) more than one thread.
There is a Thread class (and therefore a Thread object can be created) in the Android OS. As a rule,
all of your Android application components will run inside the same initial process and thread. This is
generally termed the main thread, the primary thread, or the UI thread.
If one of your Android application components starts, and Android sees that a primary process
already exists for your application, due to the fact that another component from your application
already exists, that new component will also be started within that same application process, and
will also use that same main thread.
To start your own thread, you must do so specifically within your Java code by creating a Thread
object. You can also have different components in your application run in separate processes, and
you can create additional threads for any process. This is what is usually done with Android Service
subclasses. The process in Android is created using XML mark-up, unlike the Thread object, which
is created using Java. You will be taking a look at creating a
of the chapter. Creating a Thread object, and how and when to do so, is currently beyond the scope
of this Absolute Beginner title, however.
As is the default functionality in the Android OS, all of your Android application components will run
in the same process used to launch your application. Most of your basic Android applications will
not need to change this behavior, and should not interfere with this default application launching