Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Services in Xamarin

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 275
    Comment on it

    Hi All,

    In this blog we will discuss about another one of the most important part of the Xamarin, and that is Service.

    Service in Xamrin.Android is actually very smiler to concept of service in windows and by that i mean a service is really just a way to handle a long running process

    go on in the background taking care of some task that you don't want to manage on the main thread or on the UI thread and when its completed, we want to notify us by some message or provide a way to interact with it.

    In Xamarin.Android, we can do all these thing with the help of services which are mainly of two types

    1. Started Service
    2. Bounded Service

    Both these service are basically a service and we can create one single service and it can function in both ways but we can keep them separated as well.

    Started Service is a service that you would typically and you don't actually care about what it is doing and if you want to interact with it, you can put a logic by which it will notify you when it's done processing. So you will use it in a situation when want to run any long running process like downloading some information and want you to notify when it is done.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using System.Threading.Tasks;
    
    
    namespace TaskDroid
    {
        [Service]
        class StartedService : Service
        {
            public override IBinder OnBind(Intent intent)
            {
                throw new NotImplementedException();
            }
    
            public override void OnCreate()
            {
                base.OnCreate();
            }
    
            public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
            {
                //Task.Factory.StartNew();
                Thread.Sleep(3000);
                Toast.MakeText(this, "Download Service Executed", ToastLength.Long).Show();
                return base.OnStartCommand(intent, flags, startId);
            }
        }
    }
    

    In the above code, we are creating a service which basically a class that inherits from the Service class and is decorate with service attribute. Here we have one required implantation that we have to implement that is OnBind() method, which is present as a abstract method in Service base class. this OnBind() actually a main part of the Bound service, but we will leave it as unused here.

    This Service class has some similarities to the Activity class in some ways as the code structure is quite smiler and it also has the same life cycle events but those are different by there functionalities.

    In our service we also have to implement in OnStartCommand() method which returns the StartCommandResult, and this is mainly the function where we put out process that needs to run on the background.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: