Playing with Service Component in Android

2 min read

Dear Fellow Developers


Types of services


Services Example


Service is one of the only four components in Android. Since its a component in android and you want it to work , please declare it in Android manifest.xml .


<service android:name=".MyService" />


Look at the above declaration in Android Manifest xml . Besides android:name there are other attributes for which values can be given but to make it easier to understand , i will add them when they are required.

Creating service in Android is very simple , just extend Service class  and you are ready to go.

public class MyService extends Service  {


But wait ?

Why it is required ? 


As per

Service is an application component that can perform long-running operations in the background, and it does not provide a user interface. 

So we can say whenever we want to perform a long-running operation in background we use service. Remember the keyword here is background. Unlike activity,  service is a component that remains alive even if your app is in background.

Activities handle long running operations by not executing them on UIThread , rather they spawn a worker thread and execute long running operation them. And reason for doing so is not private to Activities. To know more about this you may visit my other blog post on  Asynctasks.  But still these long running asynctasks might get killed by OS if app is not running in foreground as per requirements by OS.

Services rescue our app with this dreaded (not so dreaded) way how OS used to perform.

Enough of theory lets dive deeper into code.

public class MyService extends Service {
    public IBinder onBind(Intent intent) {
        return null;


So as soon as your class  extend Service ,  your class has to implement onBind() method. This is required in case your service is running in different process and some other applications want to connect to your service. Rest how this is done we do it later on.

Like activity , service has callback methods like onCreate() , onDestory() etc. Apart from them there is an additional method called onStartCommand()  . An int value returned from this method impacts period of service existence. A point to be noted here that the statement regarding onStartCommand () is valid for started Service.

public void onCreate() {
    Log.v("TAG","Service created");

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    return START_NOT_STICKY;



As you see in above lines, START_NOT_STICKY is returned from onStartCommand method of service which is a request to OS that not to start this service if in some conditions it is killed by OS. Now if START_STICKY is returned from onStartCommand method you know what request is made to the OS. Say for instance you are having a service that tracks user’s activity on device every second and you can query results as required. In this case you want your service to be available all the time. So even if OS kills your service and at the same time you already returned START_STICKY from onStartCommand method , your service will automatically be restarted.

For instance , suppose we have a service that updates our local DB from server every hour.

Remember even if you are running a long running operation in Service  , this operation has to be on a worker thread, else this will impact app’s speed in the same way as that activity does.



To conclude this blog post, i will like to add one more point that like activity , service too has some resource requirements. Unnecessary running services in system impacts device’s battery negatively. Even if service is not implemented properly can be one of the reason of application failure and negative feedbacks.






More examples related to service in action . Stay tuned.


Happy Coding.