Click here to Skip to main content
Click here to Skip to main content
Go to top

MonoAndroid: Using bound Services

, 21 Nov 2013
Rate this:
Please Sign up or sign in to vote.
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
    class BoundServiceBinder : Binder
    {
        Service _boundService = null;
     
        public BoundServiceBinder (Service boundServiceCls)
        {
            _boundService = boundServiceCls;
        }
     
        public Service GetBoundService ()
        {
            return _boundService;
        }
    } 
    


  •  

  • BoundServiceConnection
    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.

[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 :-

[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 :-

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)

Share

About the Author

ThatsAlok
Software Developer (Senior)
India India
He used to have biography here Smile | :) , but now he will hire someone (for free offcourse Big Grin | :-D ), Who writes his biography on his behalf Smile | :)
 
He is Great Fan of Mr. Johan Rosengren (his idol),Lim Bio Liong, Nishant S and DavidCrow and Believes that, he will EXCEL in his life by following there steps!!!
 

For good 8 years he was Visual CPP MSMVP!

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 22 Nov 2013
Article Copyright 2013 by ThatsAlok
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid