Processes and Threads

Processes ?

Android operating system is built on Linux kernel,So Linux kernel process concepts are valid for android too.

When an app component start,and the application does not have any other components running,the android system initiate a new linux process for the application with a single thread of execution.This thread is called “Main Thread” .

To identify which processes should be removed or retained,The system places each processes into an “Importance Hierarchy”.The processed with the lowest importance are removed first.

Types ?

Active Processes,
Visible Processes,
Service Processes,
Background Processes,
Empty Processes,


An android application automatically creates  a “main” thread when it starts.This is also called the UI thread.

UI thread is used for user interaction.All android applications runs on the UI main application thread.

Running all the components on a single thread will result in poor response time.

If UI thread is blocked for morethan 5 secs ,“Application Not Responding” dialog box will reveal.

Reason for ANR?

1.Doing Network operation.
2.Performing Long Calculation.
3.Accessing Database Operation on the UI thread.

Using Background Thread?

Android UI is not “Thread-Safe”.
Should not access UI operations on outside main thread or any other Thread.

Single Thread Model

1.Don’t block the UI thread – Long running operations should not perform on UI thread,It should be done in Worker thread.
2.Don’t access UI from outside main thread.Use “Message Passing” methods.

Message Passing?

To establish communication between main and worker thread,Android using message passing approach.

1.Handler : Handler object registers itself with the thread in which it is created.It provides a channel to send data to the thread.

2.Looper: It is a class which turns thread to pipeline thread.It implements the “loop” – It takes assigned task,execute it,then takes the next one,So on.
Use Looper class if you want to pass multiple messages.3.PNG

Android Service & Background Thread ?

For Long running operation you can use either background thread or Async Task

Background Thread:

1.Worker Thread,
2.Both UI and background thread runs Simultaneously.
3.Used for Performing Long running operations.

Android Service:

1.An application component.
2.Performs Long running operations in the background.
3.No user interface


What is Service?

A Service is an application component,that perform some operations in the backgroud
It does not have user interface
An activity starts a service
Service can be started by component which belong to other application.

Service can perform

1.Downloading & Uploading file over network interface.
2.Play music in background.
3.File operations.

Types of Service?

1.Started .

Component calls startService(),Once start service can run in background even if application component which started it moves to background or destroyed.




It provides client-server interface.
It runs as long as other application component bound to it.
Multiple application component can be bind to a single service.
Service is destroyed when all application component unBind.



How to create Service?

1.Must be declared in Android manifest.xml file
2. added as a child in tag
4.Defined service class should be the sub Class of Service
5.Override default methods in defined service class.

Intent Service?

Android Service that uses a single background thread to perform an operation
To start an operation request is sent through startService(),and It is handled through onHandleIntent().

Limitation of Intent Service?

1.Cannot directly send result to UI. It has to first communicate with activity.
2.Operation once started cannot interrupt in mid way.
3.Cannot run multiple operations Asynchronously.

Creating Intent Service?

Creating by extending IntentService
It overrides onHandleIntent().it will execute each time a new request comes in.
on Request completion,the response is sent back to the activity via LocalBroadcastManager method.