Sunday, 3 April 2011

Android service - Bounded

Note:This post form part of the post about Android service, which explain generally how the service works in Android.

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)

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

Example of the manifest file:
http://www.jiahaoliuliu.com/2011/04/android-service-bounded-manifest.html

2.Modify the activity file
The packages needed are:
  • android.app.Activity
  • android.content.ComponentName
  • android.content.Context
  • android.content.Intent
  • android.content.ServiceConnection
  • android.os.Bundle
  • android.os.IBinder
  • android.view.View
The four compulsory component for the activity files are:
  • LocalService
    • Variable used to save the connection
  • ServiceConnection
    • 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).
  • unbindService(ServiceConnection conn).
    • Method used to unbind the service. Normally the service will die after this operation.
Example of the Activity file
http://www.jiahaoliuliu.com/2011/04/android-service-bounded-activity.html

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:
  • android.app.Service
  • android.content.Internet
  • android.os.Binder
  • android.os.IBinder
This class should extend the class Service.
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 
    • new LocalBinder()
    • 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.
Example of the service here:
http://www.jiahaoliuliu.com/2011/04/android-service-bounded-service-example.html

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
Bound Services:http://developer.android.com/guide/topics/fundamentals/bound-services.html
This blog: http://www.jiahaoliuliu.com/2011/03/android-service-one-who-is-hidden.html

No comments:

Post a Comment