You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$10
For Premium members:
In this article, we will learn about the Hybrid Service lifecycle and how to create, start, bind and unbind Hybrid Service in Xamarin with Visual Studio 2015.
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.
The steps are given below to create Hybrid Service in an Android Applications.
Step 1
I have selected “Blank App(Android)” from template for this article.
Step 2
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
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.
MyBoundServiceBinder.cs
BoundServiceConnection.cs
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
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.
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.
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.
Xamarin.Forms For Beginners