Android has two types of services: Bounded and Started. The main difference between them is that Bounded will die with the component which has bound it, and service started will continue running after the component which has started is has died. This post is going to explain the steps to bound a service.
The main characteristics of a bounded service are:
- This service will be created when some component call bindService().
- Multiple component can bind the service at once
- To unbind the service, the clients calls unbindService()
- When no component is binding a service, this service will be destroyed automatically
- It is possible to provide a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC)
1.Notify it to the manifest.
In the manifest file, we should write down the name of the service that we want to use. For example, to use a service called "LocalService", we should include this line in the manifest (Inside of Application tag and outside of Activity tag)
Example of the manifest file:
2.Modify the activity file
The packages needed are:
- Variable used to save the connection
- Class which contains methods to connect and disconnect the service. The methods are
- public void onServiceConnected (ComponentName className, IBinder Service)
- This method is called when the connection with the service has been established, giving us the service object we can use to interact with the service
- public void onServiceDisconnected *ComponentName className)
- This method is called when the connection with the service has been unexpected disconnected, that is, its process crashed
- bindService(Intent service, ServiceConnection conn, int flags).
- Method used to bind with the service.
- The parameter are:
- Intent: The intent which call the service
- ServiceConnection: The variable where the connection should be saved
- Flags: Operation options for the binding. It could be
- More information here: http://developer.android.com/reference/android/content/Context.html#bindService%28android.content.Intent,%20android.content.ServiceConnection,%20int%29
- unbindService(ServiceConnection conn).
- Method used to unbind the service. Normally the service will die after this operation.
3. Create the LocalService file
3.1 Right click on the package and select New ->Class
3.2 Write the name of the service and press Finish. The name could be "LocalService". Remember, it should match with the name in the Manifest file
3.3 Modify the content
The packages needed are:
The elements needed are:
- public class LocalBinder extends Binder.
- It is a class for the clients to access the service. The method that it could has is
- LocalService getService()
- It returns LocalService.this
- For more complicated services, it could use IPC
- private final IBinder:
- It is a variable which contains the object which receive interactions from clients
- It could consist as simple as
- For more information about the interaction, visit here.
- public IBinder onBind(Intent intent):
- Method called when a component calls bindService().
- Method which returns the object that interact with the client. For example, "private final IBinder"
- If you don't want to allow binding, then you should return null.
For more info:
Official guide reference: http://developer.android.com/reference/android/app/Service.html
Official dev guide: http://developer.android.com/guide/topics/fundamentals/services.html
This blog: http://www.jiahaoliuliu.com/2011/03/android-service-one-who-is-hidden.html