Evgeniy Shinkarenko
Evgeniy Shinkarenko

Reputation: 119

How to create a bound service for background work (Xamarin)

I need a service which will work on background mode.Im successful create a Start service, but he dies when im close the app. Im trying to create a bound service to resolve this problem

   var demoServiceIntent = new Intent (this, typeof(MyService));
    var demoServiceConnection = new MyBinder (this);
    ApplicationContext.BindService (demoServiceIntent, demoServiceConnection, Bind.AutoCreate)

What i need to write in this method:

public override IBinder OnBind (Intent intent)
        {

            return null;
        }

Here is the full code:

[Activity (Label = "really", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        public Communicator communicator;

        protected override void OnCreate (Bundle bundle)
        {   
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.BlueTooth);
            communicator = new Communicator (this);
            var messageButton = FindViewById<Button> (Resource.Id.messageButton);
            messageButton.Click += delegate(object sender, EventArgs e) {
                communicator.SendMessage ("time: " + DateTime.Now.ToString ("T"));
            };
            communicator.MessageReceived += message => RunOnUiThread (() => messageButton.Text = message);
            var dataButton = FindViewById<Button> (Resource.Id.dataButton);
            dataButton.Click += delegate {
                var dataMap = new DataMap ();
                dataMap.PutString ("time", DateTime.Now.ToString ("T"));
                communicator.SendData (dataMap);
            };
            communicator.DataReceived += dataMap => RunOnUiThread (() => dataButton.Text = dataMap.ToString ());

            var demoServiceIntent = new Intent (this, typeof(MyService));
            var demoServiceConnection = new MyBinder (this);
            ApplicationContext.BindService (demoServiceIntent, demoServiceConnection, Bind.AutoCreate);
        }

        protected override void OnResume ()
        {
            base.OnResume ();

            communicator.Resume ();
        }

        protected override void OnPause ()
        {
            communicator.Pause ();

            base.OnPause ();
        }

        protected override void OnStart ()
        {
            base.OnStart ();

        }



    }

    [Service]
    [IntentFilter (new String[]{ "com.xamarin.MyService" })]
    public class MyService : Service
    {
        Context mContext;

        public MyService ()
        {
            mContext = Android.App.Application.Context;
        }

        [Obsolete ("deprecated")]
        public override  StartCommandResult OnStartCommand (Intent intent, StartCommandFlags flags, int startId)
        {
            return StartCommandResult.Sticky;
        }

        #region implemented abstract members of Service

        public override IBinder OnBind (Intent intent)
        {
            return null;
        }

        #endregion

        public override void OnDestroy ()
        {
            base.OnDestroy ();
            // cleanup code
        }

    }

    public class MyBinder : Java.Lang.Object, IServiceConnection
    {
        private Context mnContext;

        public MyBinder ()
        {
            mnContext = Android.App.Application.Context;
        }

        public MyBinder (Context context)
        {
            mnContext = context;
        }

        #region IServiceConnection implementation

        public void OnServiceConnected (ComponentName name, IBinder service)
        {
        }

        public void OnServiceDisconnected (ComponentName name)
        {
        }

        #endregion
    }

Upvotes: 2

Views: 4212

Answers (1)

Trevor Balcom
Trevor Balcom

Reputation: 3888

OnBind should return an instance of the binder, or MyBinder in your case. Your binder is implementing a ServiceConnection when it should be inheriting from Binder instead. The ServiceConnection is another piece of the puzzle. Your binder should look something like this:

public class MyServiceBinder : Binder
{
    private readonly MyService _service;

    public DemoServiceBinder(MyService service)
    {
        _service = service;
    }

    public MyService GetMyService()
    {
        return _service;
    }
}

I suggest reading this 3 part tutorial at Xamarin which covers the Android Bound Services topic very well.

Upvotes: 4

Related Questions