Introduction
In this article, we will learn the lifecycle of Hybrid Service, and how to create, bind and unbind Hybrid Service, using Xamarin with Visual Studio 2015.
Hybrid Service
A hybrid service is a combination of started Service and bound Service. A hybrid Service can be started by any application component. It can bind to the Application component or be started by some event. Hybrid Service will keep running, until it is clearly told to stop or until no clients are bound to it.
Lifecycle of Hybrid Service
Lifecycle of Hybrid Service is shown in the figure given below. Hybrid Service class content has six methods described in the table given below.
Method | Use |
OnCreate() | This method is used to initialize the Service. It can be used for initializing the variable, object required by the Service. |
OnBind() | This method must be implemented by all bound Services. It will first call when any client tries to connect with bound Service. |
OnUnbind() | This method calls when all the clients are unbound. |
OnRebind() | This method calls when new clients have connected to the Service. Afterwards, it will be disconneted by OnUnbind(). |
OnStartCommand() | This method must be implemented by all started Services. It calls first, when type of Service is started Service. |
OnDestroy() | This method calls when the Service is destroyed by StopService() or StopSelf() method from the Application component. |
The steps are given below to create Hybrid Service in an Android Applications.
Step 1
Create new Project for Android Application
I have selected “Blank App(Android)” from template for this article.
Step 2
Creating HybridService Class
Add new class in project by right clicking on project directory, select “Add->New Item”. Select “Class” from the template. I have given the name as “HybridService” as the file name, as shown below.
Step 3
Declaring “HybridService” class with Override Methods
Now, we need to extend (inherit) “HybridService” class from base class “Service” and also implement base method required by Service. Here, I am implementing Hybrid Service, so required compulsory methods are OnStartCommand(), OnDestroy() and OnBind(). We can add OnCreate() but it is optional. Here, as I said, I am implementing Hybrid Service, so we need to create Binder and Service Connection class. Here, I used a previous article's “Understand IntentService using Xamarin with Visual Studio 2015” project structure, so in that “BoundServiceBinder” and “BoundServiceConnection” is already created, which is Binder and Service Connection class respectively. The code snippet is given below.
HybridService.cs
- [Service]
- class HybridService : Service
- {
- private MyBoundServiceBinder binder_hybrid;
- public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, intstartId)
- {
- Toast.MakeText(this, "Hybrid Service is start", ToastLength.Long).Show();
- return base.OnStartCommand(intent, flags, startId);
- }
- public override IBinder OnBind(Intent intent)
- {
- binder_hybrid = newMyBoundServiceBinder(this);
- Toast.MakeText(this, "OnBind() method start from HybridService", ToastLength.Long).Show();
- Toast.MakeText(this, "Hybrid Service is started", ToastLength.Long).Show();
- return binder_hybrid;
- }
- public override bool OnUnbind(Intent intent)
- {
- Toast.MakeText(this, "OnUnBind() Method Called from HybridService.cs", ToastLength.Long).Show();
- return base.OnUnbind(intent);
- }
- public override void OnDestroy()
- {
- base.OnDestroy();
- Toast.MakeText(this, "Hybrid Service is Destroyed", ToastLength.Long).Show();
- }
- }
MyBoundServiceBinder.cs
- class MyBoundServiceBinder : Binder
- {
- HybridService hybridService;
- BoundService service;
-
- public MyBoundServiceBinder(HybridService hybridService)
- {
- this.hybridService = hybridService;
- }
- 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");
- }
- }
As I explained previously, lifecycle of Hybrid Service starts from “OnStartCommand()” method. When the Service given above starts by an Android component (here is the activity), it will show the message that “Hybrid Service is started” on the screen. We can see this in an output section of this article. Similarly, when this Service is terminated, it will show the message “Hybrid Service is Destroyed”. Hybrid Service requires bound due to which I am returning binder object in OnBind()method.
Step 4
Layout of the Main.axml
I have used same project as used in my previous article on “Understand Intent Service using Xamarin with Visual Studio 2015”. I just added two buttons for starting and stopping Hybrid Service as “Start Hybrid Service” and “Stop Hybrid Service”. Final Main.axmlfile looks, as shown below.
- <?xml version="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" />
- <Button
- android:text="Start Intent Service"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnstartintentservice" />
- <Button
- android:text="Start Hybrid Service"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnstarthybridservice" />
- <Button
- android:text="Stop Hybrid Service"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/btnstophybridservice" />
- </LinearLayout>
Screenshot of Layout is shown
below.
Now, when the user clicks on “StartHybridService” button, “HybridService” Service starts using StartService() method and is bound by BindService() method. The code snippet 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);
- Button btnstartintentservice = (Button)FindViewById(Resource.Id.btnstartintentservice);
- Button btnstarthybridservice = (Button)FindViewById(Resource.Id.btnstarthybridservice);
- Button btnstophybridservice = (Button)FindViewById(Resource.Id.btnstophybridservice);
-
- btnstartstartedservice.Click += Btnstartstartedservice_Click;
- btnstopstartedservice.Click += Btnstopstartedservice_Click;
- btnstartboundservice.Click += Btnstartboundservice_Click;
- btnstopboundservice.Click += Btnstopboundservice_Click;
- btnstartintentservice.Click += Btnstartintentservice_Click;
- btnstarthybridservice.Click += Btnstarthybridservice_Click;
- btnstophybridservice.Click += Btnstophybridservice_Click;
- }
-
- private void Btnstophybridservice_Click(object sender, System.EventArgs e)
- {
- UnbindService(MyBoundServiceConnection);
- StopService(new Intent(this, typeof(HybridService)));
- }
-
- private void Btnstarthybridservice_Click(object sender, System.EventArgs e)
- {
- StartService(new Intent(this, typeof(HybridService)));
- BindService((new Intent(this, typeof(HybridService))), MyBoundServiceConnection, Bind.AutoCreate);
- }
-
- private void Btnstartintentservice_Click(object sender, System.EventArgs e)
- {
- Intent intent = new Intent(this, typeof(IntenService));
- intent.PutExtra("Test_Message", "Hello, ");
- StartService(intent);
- }
-
- 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 learned about the Hybrid Service lifecycle and how to create, start, bind and unbind Hybrid Service in Xamarin with Visual Studio 2015.