A (175)

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

Foreground Process


The highest priority process level is the foreground process. This is the primary process that is
currently running (actively processing) and that is required for the application task that the user is
currently engaging in. A process is considered a foreground process if it contains an activity (user
interface) that a user is actively interfacing with or if it hosts a Service that is bound to an activity
that the user is interfacing with. A process is also considered a foreground process if it is actively
processing a Service that is running in the foreground, which means that the Service object has
called the .startForeground( ) method.


If a Service object is currently executing one of the .onCreate( ), .onDestroy( ), or .onStart( )
Service lifecycle callbacks, which you will be learning about in this chapter, or is broadcasting a
BroadcastReceiver object that is calling its .onReceive( ) method, it will be given a top foreground
process priority level status by Android. In an optimal Android operating system scenario, only a
few foreground processes will be running at any given time. Processes are terminated only as a last
resort if memory gets scarce and the OS can’t continue to run optimally.


Visible Process


The next highest priority process level is a visible process. This is a process that doesn’t contain
any foreground process components, but that can still affect what users are seeing on their displays.
A process is deemed to be visible if it contains an activity that is not in the foreground, but that is
still visible to the user display screen.—for example, an activity whose .onPause( ) method has been
invoked. A great example of this is a foreground process activity that starts a dialog that permits
another activity to be seen in the background.


A process that contains a Service subclass that has been bound to a visible process would also
be able to get visible process priority! The visible process is considered to be almost as important
as the foreground process, and thus a visible process is not terminated unless absolutely required
to keep all foreground processes running in the operating system’s memory space. Visible and
foreground processes are thus extremely similar in the Android OS.


Service Process


The middle priority process level in these five levels is the Service process. This is a process
that contains a Service that has been started with a .startService( ) method call, but that Android
does not classify in either of the two higher process priority level categories. These Service class


Table 13-1. Android’s five process priority levels, and what type of priority characteristics they exhibit on your app
Process Priority Characteristics
Foreground process A primary process that is currently actively processing your UI
Visible process A secondary process that still affects what’s visible on screen
Service process A started process that contains a background processing service
Background process A process containing an activity that’s not visible onscreen
Empty process A process that does not hold any active application components
Free download pdf