Regular Time Backgroung Process in Xamarin Forms - Android Code Specific RRS feed

  • Question

  • User89515 posted

    Hello, Opened a topic in Xamarin Forms Forums and was advised to repost here.

    My problem is that Im trying to do a background process that sends the geo location position of the user on a constant time interval, and get notifications if there are any, with Xamarin.Forms.

    To solve this on Android Im trying to follow this tutorial and make it work with forms...

    However Im having some problems:

    My LocationServiceClass:

    [Service]public class LocationService : Service, ILocationListener
        IBinder binder;
        public override IBinder OnBind (Intent intent)
            binder = new LocationServiceBinder (this);
            return binder;
        public override StartCommandResult OnStartCommand (Intent intent, StartCommandFlags flags, int startId)
            return StartCommandResult.Sticky;
        protected LocationManager locMgr = Android.App.Application.Context.GetSystemService ("location") as LocationManager;
        public void StartLocationUpdates () {       
            var locationCriteria = new Criteria();                    
            locationCriteria.Accuracy = Accuracy.NoRequirement;        
            locationCriteria.PowerRequirement = Power.NoRequirement;                    
            var locationProvider = locMgr.GetBestProvider(locationCriteria, true);
            locMgr.RequestLocationUpdates(locationProvider, 2000, 0, this);
        public event EventHandler<LocationChangedEventArgs> LocationChanged = delegate { };
        public void OnLocationChanged (Android.Locations.Location location)
            this.LocationChanged (this, new LocationChangedEventArgs (location));
            //Do stuff I need to do every time interval set on RequestLocationUpdates

    my LocationServiceBinder:

    public class LocationServiceBinder : Binder
            public LocationService Service
                get { return this.service; }
            } protected LocationService service;
            public bool IsBound { get; set; }            
            public LocationServiceBinder (LocationService service) { this.service = service; }

    my LocationServiceConnection

        public class LocationServiceConnection : Java.Lang.Object, IServiceConnection
            public LocationServiceConnection (LocationServiceBinder binder)
                if (binder != null) {
                    this.binder = binder;
            public void OnServiceConnected (ComponentName name, IBinder service)
                LocationServiceBinder serviceBinder = service as 
                if (serviceBinder != null) {
                    this.binder = serviceBinder;
                    this.binder.IsBound = true;
                    // raise the service bound event
                    this.ServiceConnected(this, new ServiceConnectedEventArgs () { Binder = service } );
                    // begin updating the location in the Service
            public void OnServiceDisconnected (ComponentName name) { this.binder.IsBound = false; }

    my problem begins here! it does not have this.binder and ServiceConnected.... I could declare a binder this way to solve the this.binder:

        public LocationServiceBinder Binder
            get { return this.binder; }
            set { this.binder = value; }
        protected LocationServiceBinder binder;

    but could not manage to solve the ServiceConnection... Found on gitHub an generic example but didnt have success...

    After it I tried to declare in my shared code App Class this:

        public App ()
            #if __ANDROID__     
            new System.Threading.Tasks.Task ( () > { 
                Android.App.Application.Context.StartService (new Intent (Android.App.Application.Context, typeof(LocationService)));
                this.LocationServiceConnection = new LocationServiceConnection (null);
                this.locationServiceConnection.ServiceConnected += (object sender, ServiceConnectedEventArgs e) => {
                    this.LocationServiceConnected ( this, e );
                Android.Content.Intent locationServiceIntent = new Android.Content.Intent (Android.App.Application.Context, typeof(LocationService));
                Android.App.Application.Context.BindService (locationServiceIntent, App.Droid.LocationServiceConnection, Android.Content.Bind.AutoCreate);    
            } ).Start ();
            // The root page of your application
            MainPage = GetMainPage ();

    So I could do the step 10 of the tutorial in my mainActivity on Android project but I had lots of errors...

    Someone could give some advise?

    Tuesday, June 16, 2015 9:52 PM

All replies

  • User89515 posted

    Anyone have ever implemented any type of background service in android with forms??

    Wednesday, June 17, 2015 5:24 PM