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().
Intent intent = new Intent(this, HelloService.class);
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. […]