Professional Documents
Culture Documents
Lecture #06
Services
Previous Lecture
Android Intents
Today’s Lecture
Services
Implementing a Service
Service Lifecycle
Service Examples
AsyncTask
IntentService
Service
A service is a component that runs in the background to perform long
running operations without needing to interact with the user and its
work even if the application is destroyed.
An application component can start a service and it continue to run in
the background even if you switch to another application
It runs in main application thread until it is specified otherwise
A Service in Android can be started from an Activity, from a
Broadcast receiver and other services too
Just like an Activity, a Service must be declared inside
AndroidManifest.xml file
Service States
A Service can essentially two states:
1. Started (Un-Bound)
2. Bound
Started
A service is started when an application component, such as an activity, starts it by calling
startService()
Once started, a service can run in the background indefinitely, even if the component that started it
is destroyed.
It is stopped by using the stopService() method. The service can also stop itself by calling the
stopSelf() method.
Bound
A service is bound when an application component binds to it by calling bindService()
A bound service offers a client-server interface that allows components to interact with the service,
send requests and get results
Even bound service can communicate across processes with Inter Process Communication (IPC)#
The client can unbind the service by calling the unbindService() methos
startService()
vs
bindService()
A service is start by calling startService() method and it is stop by
stopService() method. A service can stop itself by calling the stopSelf()
method.
A service is bound, when another component calls bindService()
method. The client can unbind the service by calling the
unbindService() method. The service cannot be stopped until the client
unbind the service.
Service
Main objective of a service is to run in background and perform some long
running task not requiring user to interact with it
It runs in main application thread until it is specified otherwise
A service can be implemented using its lifecycle’s callback methods
public void onCreate()
public void onStartCommand()
public void onBind()
public void onDestroy()
A Service in Android can be started from an Activity, from a Broadcast receiver
and other services too
Just like an Activity, a Service must be declared inside AndroidManifest.xml file
Service …
A service that requires to perform CPU intensive tasks (MP3 playback, intense
calculations, process animations etc.) or blocking operations, it must create its
own thread to perform that task
Implementing a Service
A class can be implemented as a Service:
public class TestService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
We have to implement only one method called onBind()
Similar to an Activity, a Service has its lifecycle and we can override some
callback methods so that we can handle its different states
Service Lifecycle … Code Example
public class TestService extends Service {
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Service Lifecycle
The method onCreate() is called only one time
when the Service has to created. Android system
calls this method
onStartCommand() is the most important method
because it is called when we required to start the
Service
In this method we have the Intent passed at
time we run the Service, in this way we can
exchange some information with the Service
Here, we implement our logic that can be
execute directly inside this method if it isn’t
time expensive otherwise we can create a
thread
One can observe that this method requires us to
return an Integer as result. This integer represents
how the Service should be handled by the OS
Service … Example … MyService1.Java
public class MyService1 extends Service {
public IBinder onBind(Intent intent){
return null;
}
Extend your class from IntentService if you want your background code to
execute in separate thread
Implement constructor of class calling super constructor using a String
Implement onHandleIntent(Intent), it is executed in a worker thread
When the code has finished executing, thread ends terminating the
IntentService automatically