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:
- Create a new project and for example name it ” Service3 ”.
- Now your MainActivity.java and activity_main.xml files will be generated.
- Now create another java class and name it BoundService.
- Now just add a button on your graphical layout in your activity_main.xml file.
- Add an onClick property to the button for example :
android:onClick = “addClicked” - Now in the MainActivity.java file extend the class MainActivity with Activity.
- Now we need a BoundService and hence declare it by creating an object theService .
- Also declare isBound boolean as false,which will tell us whether the service is bound or not.
- Write a code for ServiceConnection as stated below :
- Keep the onCreate( ) method as it is don’t make any Changes in it .
- Now your MainActivity.java file will look as below :
- Now control the binding of the services by writing the code for onStart( ) and onPause( ) method which are as follows :
- On onstart( ) method the Service is binded and onServiceConnected( ) gets run.
- Simillarly when the service is unbound onServiceDisconnected( ) gets run.
- Add the code for the ADD button which we took on our graphical layout , which is as follows :
- So now your MainActivity.java file with the addition of the code for add button will look as shown below:
- Now go to your BoundService.java file and extend the Class BoundService by Service as shown below :
Public class BoundService extends Service - key to boundService is an object called binder,a binder binds the service to the activity that startup.
- Now declare privately an IBinder with an object binder to a new LocalBinder as shown below :
- : Now for returning a binder we write an onBind( ) method as shown :
- Now just write an add( ) method to carry out addition:
- Now your BoundService.java file will look as Shown below :
- Do not forget to add service in the AndroidManifest.xml file :
- Now run the MainActivity.java file and observe the LogCat you will see the following as shown in marked:
- Also you will get the user interface :
- Now click on ADD button and you will get the result as 5 in your LogCat.
- Thus we have implemented BoundService successfully.
[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]
[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]
[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]
[code language=”java”]private final IBinder binder = new LocalBinder();
public class LocalBinder extends Binder
{
BoundService getService()
{
return BoundService.this;
}
}
[/code]
[code language=”java”]public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
Log.i("service","bound!!!");
return binder;
}
[/code]
[code language=”java”]public int add(int a,int b)
{
return a + b;
}
[/code]
[code language=”xml”]<service
android:name=".BoundService">
</service>
[/code]