Implementing Bound Service


Hi !!! In this tutorial we will be implementing a Bound Service in which we are just going to add two numbers together.

  • Here is an example of Bound Service ,so to implement it just follow the steps given below .
  • STEPS:
  1. Create a new project and for example name it ” Service3 ”.
  2. Now your and activity_main.xml files will be generated.
  3. Now create another java class and name it BoundService.
  4. Now just add a button on your graphical layout in your activity_main.xml file.
  5. Add an onClick property to the button for example :
    android:onClick = “addClicked”
  6. Now in the file extend the class MainActivity with Activity.
  7. Now we need a BoundService and hence declare it by creating an object theService .
  8. Also declare isBound boolean as false,which will tell us whether the service is bound or not.
  9. Write a code for ServiceConnection as stated below :
  10. private ServiceConnection connection = new ServiceConnection()
        public void onServiceConnected(ComponentName BoundService, IBinder service)
       			 // TODO Auto-generated method stub
    			//we’ve bound to LocalService ,cast the IBinder and get The LocalService instance
    			LocalBinder binder = (LocalBinder)service;
    			theService = binder.getService();
    			isBound = true;
    	public void onServiceDisconnected(ComponentName arg0)
    		// TODO Auto-generated method stub
    		isBound = false;

  11. Keep the onCreate( ) method as it is don’t make any Changes in it .
  12. Now your file will look as below :
  13. 1

  14. Now control the binding of the services by writing the code for onStart( ) and onPause( ) method which are as follows :
  15. protected void onStart()
    	//setting up a LogCat
    	Log.i("onStart","activity started");
    	//for creating and starting a new intent
    	Intent intent = new Intent(MainActivity.this,BoundService.class);
    	//  use bindService method to bind MainActivity to BoundService	
    protected void onPause()
    			isBound = false;

  16. On onstart( ) method the Service is binded and onServiceConnected( ) gets run.
  17. Simillarly when the service is unbound onServiceDisconnected( ) gets run.
  18. Add the code for the ADD button which we took on our graphical layout , which is as follows :
  19. public void addClicked(View v)
    	//Check to see if the service isBound that is why we have Declared it Boolean :
               //Logout theService.add i.e(2,3) so that we get the answer as 5 :
               Log.i("bound",String.format("sum: %d",theService.add(2,3)));

  20. So now your file with the addition of the code for add button will look as shown below:
  21. 2

  22. Now go to your file and extend the Class BoundService by Service as shown below :
    Public class BoundService extends Service
  23. key to boundService is an object called binder,a binder binds the service to the activity that startup.
  24. Now declare privately an IBinder with an object binder to a new LocalBinder as shown below :
  25. private final IBinder binder = new LocalBinder();
    public class LocalBinder extends Binder 
    	BoundService getService()
    	      return BoundService.this;

  26. : Now for returning a binder we write an onBind( ) method as shown :
  27. public IBinder onBind(Intent arg0)
    	// TODO Auto-generated method stub
    	return binder;	

  28. Now just write an add( ) method to carry out addition:
  29. public int add(int a,int b)
    	return a + b;

  30. Now your file will look as Shown below :
  31. 3

  32. Do not forget to add service in the AndroidManifest.xml file :
  33. <service 

  34. Now run the file and observe the LogCat you will see the following as shown in marked:
  35. 4

  36. Also you will get the user interface :
  37. 5

  38. Now click on ADD button and you will get the result as 5 in your LogCat.
  39. 6

  40. Thus we have implemented BoundService successfully.