A Service runs in background to perform long-running operations or to perform work for remote processes. A service does not provide any user interface it runs only in background with User’s input. For example a service can play music in the background while the user is in a different App, or it might download data from the internet without blocking user’s interaction with the Android device.
If you have not defined your service in your AndroidManifest.xml, you will receive a ServiceNotFoundException when attempting to start it.
The first thing to do is to add the service to AndroidManifest.xml, inside the <application> tag:
If your intend to manage your service class in a separate package (eg: .AllServices.RecordingService) then you will need to specify where your service is located. So, in above case we will modify:
or the easiest way of doing so is to specify the full package name.
Then we create the actual service class:
All this service does is show a notification when it's running, and it can display toasts when its doSomething() method is called.
As you'll notice, it's implemented as a singleton, keeping track of its own instance - but without the usual static singleton factory method because services are naturally singletons and are created by intents. The instance is useful to the outside to get a "handle" to the service when it's running.
Last, we need to start and stop the service from an activity:
In this example, the service is started and stopped by the same method, depending on it's current state.
We can also invoke the doSomething() method from our activity:
Creating Bound Service with help of Binder
Create a class which extends Service class and in overridden method onBind return your local binder instance:
Then in your activity bind to service in onStart callback, using ServiceConnection instance and unbind from it in onStop:
Creating Remote Service (via AIDL)
Describe your service access interface through .aidl file:
Now after build application, sdk tools will generate appropriate .java file. This file will contain Stub class which implements our aidl interface, and which we need to extend:
Then in activity:
Defining the process of a service
The android:process field defines the name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. However, a component can override the default with its own process attribute, allowing you to spread your application across multiple processes.
If the name assigned to this attribute begins with a colon (':'), the service will run in its own separate process.
If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.
Lifecycle of a Service
The services lifecycle has the following callbacks
Executed when the service is first created in order to set up the initial configurations you might need. This method is executed only if the service is not already running.
Executed every time startService() is invoked by another component, like an Activity or a BroadcastReceiver. When you use this method, the Service will run until you call stopSelf() or stopService(). Note that regardless of how many times you call onStartCommand(), the methods stopSelf() and stopService() must be invoked only once in order to stop the service.
Executed when a component calls bindService() and returns an instance of IBInder, providing a communication channel to the Service. A call to bindService() will keep the service running as long as there are clients bound to it.
Executed when the service is no longer in use and allows for disposal of resources that have been allocated.
It is important to note that during the lifecycle of a service other callbacks might be invoked such as onConfigurationChanged() and onLowMemory()