A (175)

(Tuis.) #1
CHAPTER 13: Android Service Class and Threads: Background Processing 479

and running functionality, unless there is a very compelling (and too advanced for this book title)
functional reason for doing so.


For advanced applications (which we are not going to be covering in this book), I will cover this
concept here, to be thorough regarding Android processes. If you happen to find yourself in a
development situation where you absolutely need to control which Android process a certain
application component belongs to and functions in, you can specify your own custom processes in,
you guessed it, your application’s AndroidManifest.xml file.


Let’s take a look at how to spawn a process using the android:process parameter inside of the four
major areas of Android. These are the Activity, the Service, the Broadcast Receiver (messaging), and
the Content Provider (databases). These use the , the , the , and the


parent tags, respectively.

Spawning a Process: The android:process XML Parameter


Your AndroidManifest.xml application component tags for each major type of application
component, whether it is an Activity tag, a Service tag, a Broadcast
Receiver tag, or a Content Provider tag, can include the optional
android:process parameter. This parameter will allow you to control the process that a component
will run inside of (or under, if you prefer to visualize it that way).


The android:process parameter should be utilized to specify the process under which the application
component needs to run. You can set up this process parameter in such a way that each of the
application’s components run inside its own process, or you can “mix and match” components and
processes in such a way that some of your application components will share a given process, while
others will not share that process, or will even have their own process altogether.


If you wanted to get really complex, you could also set these android:process parameters so that
components from different Android applications can execute together inside of the same Android
process. This can only be accomplished when those particular applications share the same
Linux User ID, and which are signed with the same certificates, so this topic is also a bit too complex
for this book. I mention it here just so that you know about it in case you wish to research it further.


It is also interesting to note that the global tag in your AndroidManifest XML file
will also accept the android:process parameter. Using the android:process parameter inside of
your tag will set the default process value for your application, which would then be
applied to all of your application’s components inside of your XML application component definition
(nested) hierarchy. Of course, this would not include those application components that then utilize
their own android:process parameter.


In this situation, the application component child tag that utilized the android:process parameter
to specify a different process for that particular application component would “override” the global
or default process that you set as the process for your application to use via the android:process
parameter inside the tag. Thus, if you want all your components, except for one or
two, to share the same custom process, define the process in your tag and then
override it selectively in one of the component child tags with android:process.


It is important to note that the Android OS has the option to shut down any of your processes at any
given time. This is so that it can efficiently manage your system hardware resources (memory and
processing cycles). This could be important when system memory is running low or if the memory

Free download pdf