Introduction
In previous articles, I have discussed about Unbound Services. In this article, we will learn the lifecycle of Bound Services. For creating a Bound Service, we need to create Bound Service class, a Binder for binding the Service, and Service Connection.
Bound Service
A service can be bound by application component by calling the BindService() function. It provides an interface that allows the bound component and services to interact with each other. When there are no clients bound to the service, Android will shut down that service.
Lifecycle of Bounded Service
Service class contains six types of methods as described in the following table.
Method
|
Use
|
OnCreate()
|
This method is used for initializing service. It can be used for initializing variable and objects required by service.
|
OnBind()
|
This method must be implemented by all the bound services. It will first call when any client try to connect with bound service. For Unbounded service, this method return null.
|
OnUnbind()
|
This method is called when all clients are unbound.
|
OnRebind()
|
This method is called when new clients get connected to service, after it had disconneted by OnUnbind().
|
OnStartCommand()
|
This method must be implemented by all the started services. |
OnDestroy()
|
This method is called when service is destroyed by StopService() or StopSelf() method from application component.
|
The following steps are required for creating a Bound Service type for Android application.
Step 1 Create Blank Android Project
I have selected “Blank App(Android)” from template for this article.
Step 2 Create Class, Binder, and ServiceConnection for Bound Service
Add new class in project by right clicking on project directory, select “Add->New Item”. Select “Class” from template. I have given “BoundService” as the file name. In the same way, we need to add class for Binder and Service Connection.
Now, we need to extend “BoundService” class from base class “Service” and also implement base method required by Bound Service. Required methods for Bound Service are OnBind() (the starting point of Bound Service), OnUnBind(), and OnDestroy(). We can add OnCreate(), OnRebind() methods optionally.
Here, as I said, I am implementing Bound Service so OnBind() method returns “binder” object of “MyBoundServiceBinder” extended from Binder base class. We need to add “BoundServiceConnetion” extended from IServiceConnection base class. Snipped of code as follow,
- [Service]
- Class BoundService : Service
- {
- private MyBoundServiceBinder binder;
- public override void OnCreate()
- {
- base.OnCreate();
- }
- public override IBinder OnBind(Intent intent)
- {
- binder = new MyBoundServiceBinder(this);
- Toast.MakeText(this, "OnBind() method start from BoundService", ToastLength.Long).Show();
- Toast.MakeText(this, "Bound Service is started",ToastLength.Long).Show();
- return binder;
- }
- public override bool OnUnbind(Intent intent)
- {
- Toast.MakeText(this, "OnUnBind() Method Called from BoundService.cs", ToastLength.Long).Show();
- return base.OnUnbind(intent);
- }
- public override void OnDestroy()
- {
- Toast.MakeText(this, "Bound Service Destroyed", ToastLength.Long).Show();
- base.OnDestroy();
- }
- }
MyBoundServiceBinder.cs
- class MyBoundServiceBinder : Binder
- {
- BoundService service;
- public MyBoundServiceBinder(BoundService service)
- {
- this.service = service;
- }
- }
BoundServiceConnection.cs
- class BoundServiceConnection : Java.Lang.Object, IServiceConnection
- {
- public void OnServiceConnected(ComponentName name, IBinder service)
- {
- Console.WriteLine("OnServiceConnected() Method called");
- }
- public void OnServiceDisconnected(ComponentName name)
- {
- Console.WriteLine("OnServiceDisConnected() Method called");
- }
- }
Step 3 Layout of Main.axml and how to Bind and Unbind Bound Service
I have used the same layout as used in previous articles. I have just added two more buttons to start and stop Bound Service in Main.axml. The updated Main.axml is shown below.
- <?xmlversionxmlversion = "1.0"encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:text="Started Service"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnstartstartedservice" />
- <Button
- android:text="Stop Started Service"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnstopstartedservice" />
- <Button
- android:text="Start Bound Service"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnstartboundservice" />
- <Button
- android:text="Stop Bound Service"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnstopboundservice" />
- </LinearLayout>
Screenshot of Updated Layout is shown below.
Now, when user clicks on “Start Bound Service” button, “BoundService” starts using BindService() method and when user clicks on “Stop Bound Service”, “BoundService” is terminated by Unbindservice() method. Snipped code of OnCreate() method of MainActivity is given below.
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView (Resource.Layout.Main);
- Button btnstartstartedservice = (Button)FindViewById(Resource.Id.btnstartstartedservice);
- Button btnstopstartedservice = (Button)FindViewById(Resource.Id.btnstopstartedservice);
- Button btnstartboundservice = (Button)FindViewById(Resource.Id.btnstartboundservice);
- Button btnstopboundservice = (Button)FindViewById(Resource.Id.btnstopboundservice);
-
- btnstartstartedservice.Click += Btnstartstartedservice_Click;
- btnstopstartedservice.Click += Btnstopstartedservice_Click;
- btnstartboundservice.Click += Btnstartboundservice_Click;
- btnstopboundservice.Click += Btnstopboundservice_Click;
- }
-
- private void Btnstopboundservice_Click(object sender, System.EventArgs e)
- {
- UnbindService(MyBoundServiceConnection);
- }
-
- private void Btnstartboundservice_Click(object sender, System.EventArgs e)
- {
- var BoundServiceIntent = new Intent(this,typeof(BoundService));
- BindService(BoundServiceIntent, MyBoundServiceConnection, Bind.AutoCreate);
- }
-
- private void Btnstopstartedservice_Click(object sender, System.EventArgs e)
- {
- StopService(new Intent(this, typeof(MyServices)));
- }
-
- private void Btnstartstartedservice_Click(object sender, System.EventArgs e)
- {
- StartService(new Intent(this, typeof(MyServices)));
- }
Output
Summary
In this article, we have learned about the life cycle of Bound Service, how to create, bind, and unbind Bound type of Service in Xamarin using Visual Studio 2015.