Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Xamarin

MonoAndroid: Using bound Services

4.89/5 (3 votes)
21 Nov 2013CPOL2 min read 13.6K   112  
Demonstration of Bound Service!

Introduction

Day before yesterday I have posted article regarding started services, this write-up is talk about Bound Services. Since difference is so minute, I prefer to post it as tip instead as full fledged article.

The difference between Started Services and Bound Services is that Bound Service bound to lifetime of invoking activity and Started Service is not.

For starting Started Service we have to use StartService method and for Bound Service we have to use BindService method. Similarly for stopping the Started Service we either have to depend on StopService from outside and StopSelf within the service code. In case of bound service we either call mentioned in StartedService or just exit the main activity.

Using the code 

Now basically for creating Bound services, we require to create IServiceConnection derived class which will act as connection between Service and Activity and Binder derived class which provide means for Interprocess communication see following code :-

  • BoundServiceBinder
    C#
    class BoundServiceBinder : Binder
    {
        Service _boundService = null;
     
        public BoundServiceBinder (Service boundServiceCls)
        {
            _boundService = boundServiceCls;
        }
     
        public Service GetBoundService ()
        {
            return _boundService;
        }
    } 


  •  

  • BoundServiceConnection
    C#
    class BoundServiceConnection: Java.Lang.Object,IServiceConnection
    {
        Activity _activity;
        public BoundServiceConnection (Activity activity){
            _activity = activity;            
        }
     
        #region IServiceConnection implementation
        void IServiceConnection.OnServiceConnected (ComponentName name, 
                                                    IBinder service){        
        }
     
        void IServiceConnection.OnServiceDisconnected (ComponentName name)  {        
        }
        #endregion
    }


Now our service code looks like to this, only change from StartedService is that instead of passing the NULL from OnBind method we are going to return the binder object, class code of which available above. And in OnCreate and OnDestroy function we are logging created and destroy message in console.

C#
[Service]
class BoundServiceCls: Service
{
	BoundServiceBinder binder = null;
	#region implemented abstract members of Service
 
	public override IBinder OnBind (Intent intent){
		binder = new BoundServiceBinder (this);
		return binder;		
	}
 
	#endregion
 
	public override void OnCreate (){
		base.OnCreate ();
		Log.Debug ("BoundServiceBinder", "BoundServiceBinder Created");
	}
 
	public override void OnDestroy (){
		base.OnDestroy ();
		Log.Debug ("BoundServiceBinder", "BoundServiceBinder Stopped");
	}
}

Now in Main.axml file I have added two buttons, one for starting the service and other for stopping the service.

In MainActivity.cs, the code looks like this :-

C#
[Activity (Label = "BoundServiceTest", MainLauncher = true)]
public class MainActivity : Activity{	
	public Binder _binder=null;
	Intent _intentServie;
	IServiceConnection _conn;
 
	protected override void OnCreate (Bundle bundle){
 
		base.OnCreate (bundle);
		SetContentView (Resource.Layout.Main);
 
		Button button = FindViewById<Button> (Resource.Id.myButton);
		Button button1 = FindViewById<Button> (Resource.Id.myButton1);
		
		button.Click += delegate {
			_intentServie = new Intent(this,typeof(BoundServiceCls));
			_conn = new BoundServiceConnection(this);
			this.BindService(_intentServie,_conn,Bind.AutoCreate);
		};
 
		button1.Click += delegate {
			this.UnbindService(_conn);
		};		
	}
}

For creating the service, we create service intent, and then connection and using BindService method bind connection to started service. For stopping the service we just need to call UnbindService

Build and Run the application. Click on each button, you can see following output :-

Image 1

Articles in this Series     

Tips/Tricks in this Series   

History 

  • 06-Sept-2013 : First version.
  • 22-Nov-2013: Updated other article section

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)