Hello all,
In this blog we will discuss about Bound service in Xamarin.Android.
A Bound service at its core is just a service like a started service and a Bound service allows you to get a reference to that service which is within your activity (ex. MainActivity.cs) and than be able to interact with it and may call the methods of it or get properties from that service just as you would do with any other class or part of your application.
Basic code structure for the Bound service is as follows :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace TaskDroid
{
[Service]
[IntentFilter(new string[] { "com.myTask.BoundService" })]
public class BoundService : Service
{
public override IBinder OnBind(Intent intent)
{
return new MyServiceBinder(this);
}
public string GetUser()
{
return "Gaurav";
}
}
public class MyServiceBinder : Binder
{
BoundService _boundService;
public MyServiceBinder(BoundService _myBoundService)
{
_boundService = _myBoundService;
}
public BoundService GetServiceInstance()
{
return _boundService;
}
}
public class MyBoundServiceConnection : Java.Lang.Object, IServiceConnection
{
MainActivity mainActivity;
public MyBoundServiceConnection(MainActivity activity)
{
mainActivity = activity;
}
public void OnServiceConnected(ComponentName name, IBinder service)
{
//throw new NotImplementedException();
var binder = service as MyServiceBinder;
if (binder != null)
{
mainActivity.Binder = binder;
mainActivity.IsBound = true;
}
}
public void OnServiceDisconnected(ComponentName name)
{
//throw new NotImplementedException();
mainActivity.Binder = null;
mainActivity.IsBound = false;
}
}
}
Here in the above code structure we have to implement the OnBind() method which is an abstract method present in the Service class as we are inheriting from Service class. To wire up our Bound Service to our main activity, we have to create some helper classes.
Now in Bond Service a Binder (ex. MyServiceBinder class) is really just a mechanism of providing the
The Binder basically gives the access to our Bound Service. Now to allow our activity to actually bind to our bound Service, we have to create a Service Connection (ex. MyBoundServiceConnection class) which will inherits from Java.Lang.Object and ServiceConnection and we will implement two methods that is
- OnServiceConnected(ComponentName name, IBinder service)
- OnServiceDisconnected(ComponentName name)
Now, To call the Bound Service for the main activity class we have the following block of code :
protected override void OnStart()
{
base.OnStart();
try
{
var intent = new Intent("com.taskDroid.MyBoundService");
var serviceConnection = new MyBoundServiceConnection(this);
BindService(intent, serviceConnection, Bind.AutoCreate);
string name = Binder.GetServiceInstance().GetUser();
}
catch (Exception ex)
{
Toast.MakeText(this, ex.Message.ToString(), ToastLength.Long).Show();
}
}
0 Comment(s)