Android TutorialsImplementing Bound Service

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 MainActivity.java 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 MainActivity.java 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. [code language=”java”]private ServiceConnection connection = new ServiceConnection()
    {
    @Override
    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;

    }

    @Override
    public void onServiceDisconnected(ComponentName arg0)
    {
    // TODO Auto-generated method stub
    isBound = false;

    }
    };

    [/code]

  11. Keep the onCreate( ) method as it is don’t make any Changes in it .
  12. Now your MainActivity.java 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. [code language=”java”]protected void onStart()
    {
    super.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
    bindService(intent,connection,Context.BIND_AUTO_CREATE);
    }

    protected void onPause()
    {
    super.onPause();
    if(isBound)
    {
    unbindService(connection);
    isBound = false;
    }
    }
    [/code]

  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. [code language=”java”]public void addClicked(View v)
    {
    //Check to see if the service isBound that is why we have Declared it Boolean :
    if(isBound)
    {
    //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)));
    }
    }
    [/code]

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

  22. Now go to your BoundService.java 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. [code language=”java”]private final IBinder binder = new LocalBinder();

    public class LocalBinder extends Binder
    {
    BoundService getService()
    {
    return BoundService.this;
    }
    }
    [/code]

  26. : Now for returning a binder we write an onBind( ) method as shown :
  27. [code language=”java”]public IBinder onBind(Intent arg0)
    {
    // TODO Auto-generated method stub
    Log.i("service","bound!!!");
    return binder;
    }
    [/code]

  28. Now just write an add( ) method to carry out addition:
  29. [code language=”java”]public int add(int a,int b)
    {
    return a + b;
    }
    [/code]

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

  32. Do not forget to add service in the AndroidManifest.xml file :
  33. [code language=”xml”]<service
    android:name=".BoundService">
    </service>
    [/code]

  34. Now run the MainActivity.java 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.
Previous articleImplementing ContentProvider
Next articleLearning SQL

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -