Services – An Android Component

As an android developer you must have know the major components of android in very detailed manner. If not don’t worry!! Today I am writing here to help you guys to get through one of the major component of Android i.e. “Service”. If you are a beginner you would probably think why we would need services in our coding. If this is the case you might becomes the services  FAN by the end of this tutorial. You will understand how important role a service will play for a developer. Eventually you will come to know that they always play a important role in designing when you integrate them with web services, or have an application logic which will bound you beyond the UI i.e. If you need to start long running tasks in response to user input, then you should not expect the UI to wait for the result. A service is an application component that implements and performs long running operations in the background without any need to interact with the user. Services also handle the background processing associated with the application. A service started by an application will be continuing to running in background even when user switches to another application component.   While developing services, you must always have few key questions in your mind like:   How to communicate with the service from an Activity/Fragment? How to communicate with the Activity/Fragment from a service? How to keep my service from being killed?   You will get the answers of all these questions by the end of this tutorial. I will explain each in a very good detail.   In android we have following two states that are essentially taken by a service.   Started  startService() Bound  bindService()   Lets see these states in a bit more detail:   Started: A service is started with calling startService() by an application component (such as an activity). A service is once started it will keep running in the background for indefinite time even if the component that started is destroyed. A single operation is performed by a started service and it doesn’t return any result to the caller.   Bound: A service is bound by an application component by calling bindService( ). A bound service allows an application component to interact with service by offering a client-server interface that enables a component to send requests, get results and even do so across processes with inter-process communication (IPC). A bound service runs as long as another application component bound to it. Multiple components can be bind to a service but when all of them unbind, the service is destroyed.   In the remaining tutorial I will be going to explain you how to start, stop and create a service and the service life cycle. Let’s have a look on each one by one.   How to Start a service   A service can be started from an activity or other application by passing an intent (specifying the service to start) to startService(). The android system calls the service’s onStartCommand() method and passes it the intent. onStartCommand() can be never called directly. Following is an example shows that an activity can start the example service in the previous section (HelloService) using an explicit intent with startServie().  

  If there are multiple requests to start the service then it results in multiple corresponding calls to the service’s onStartCommand(). However, only one request to stop the service (with stopSelf() or stopService()) is required to stop it.   How to Stop a service:    A started service must manage its own lifecycle. It means the system doesn’t stop or destroy the service unless it must recover system memory and service continues to run after onStartCommand() returns. So the service must stop itself by calling stopSelf() or another component can stop it by calling stopService(). System will destroy the service as soon as possible once it requested to stop with stopSelf() or stopService().   How to Create a service (unBounded):   In android a service can be created by extending the service class and adding a service tag to Android Manifest. You must override the methodonCreate(), onStart(),  onStartCommand(), and onDestroy() after extending the class. onStart() and onStartCommand() methods are essentially similar, the only which differs both is that onStart() method has been deprecated in API level 5 and above. J If context.startService() is used to create a service by the system. The method onCreate() is called just before onStart() or onStartCommand(). If the service is bound with a call to context.bindService() method, the onCreate() method is called just before the onbind() method.onStart() and  onStartCommand() are not called in this case.   Creating a started services   A started service can be defined as the service which another component starts by calling startService(), results in a call to service’s onStartCommand() method. This started service has a lifecycle that is independent of the component that started it and service will keep running in background for indefinite time ,even the component is destroyed that started the service. When its job is completed it should stop itself by calling stopSelf() or another component will stop it by calling stopService().   The service can be started by an application component such as an activity by calling startService() and passing an intent that specifies the service and includes any data for service’s use. This intent is received by service onStartCommand() method.   For instance some data needs to be store on online database by an activity. Then activity can start an associated service and deliver the data to save by passing an intent to startService(). The intent is received by the service in onStartCommand(), which connects to the internet and perform a database transaction. Service will stop itself and destroyed when the transaction is completed.   […]

android component

Applying MediaCodec On An Open Source Android Audio Player

OpenMax (in short OMX) is a royalty free, non-proprietary, and a cross platform set of routines. It targets the interfaces of multimedia processing. This was designed with efficiency that makes large quantities of multimedia data to be processed in a controlled and optimised manner which includes video and audio codecs, graphics and so on. This […]


5 Most Used Android Testing Frameworks

As time goes by, Google’s Android environment continues to grow. As we can observe, new mobile OEMs are being launched in different locations on the globe, with different screen sizes, chipsets, ROM/Firmwares, etc. It’s getting harder for the Android developers to adapt with this fragmentation. Android and iOS developers are fortunate to have limitless access […]

3d illustration of a large wrench and screwdriver lying in a cross over top of a framed glowing green Google Android logo

Android Language Highlights A Developers Perspective

Although Web applications are important and widely used these days, but there are several reasons why native applications are sometimes preferable for mobile apps.   You can use graphics and UI controls that are not available in browsers. You can customize the apps for small screen sizes. You can use resources (location, address book, phone, […]

android learning

The Official Android Remote Control Namely KORE

  Once again a new fully working Android remote was released. This remote was started by a developer, Synced Synapse. Kodi Team liked the the simplicity, design and most of all, the fact that it is open source. He was then recruited to join the Kodi team and here it goes, the new remote is […]


Learn How to Position the Toast Message

Today in this tutorial, we will discuss about how to position the toast message in Android applications. Although I assume you must know what is Toast in android, but for beginners, I will define it in brief. A toast is the simple popup message which provides simple feedback about an operation. By default it is displayed […]


Flip Your Views (Image, Button,Text, etc.)

Flipping a view makes a nice effect on your game or app. Sometimes it’s a bit hard to find a good implementation on the internet that explains exactly what you want. But today I will show you how to make the two views flip together. It will explain you how to flip a view when user clicks on it. […]


Extracting Colors In Android 5.0 – “The Palette API”

Before the new update of Palette’s API in android 5.0 developers were allowed to extract colors from images. Now with the release of Android Lollipop, the Palette class makes easy to extract prominent colors from bitmap images. These colors are very useful if you want to style other view components to match colors from your image, […]


Animations In Android KitKat

With the introduction of Android KitKat, Google has brought a number of new additions to Android. In this tutorial I will focus on one of those additions i.e. transition framework. Time by time with the new versions of android it brings new improved animation tools for developers. Animations Added in Honeycomb come with very good and easy API for creating […]